Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / blog / tags.php
blob2f133b6a0181f958b1b0c27492d19e707326a9fd
1 <?php // $Id$
2 require_once('../config.php');
4 /// main switch for form processing to perform, add/delete etc
5 $action = optional_param('action','',PARAM_ALPHA);
7 require_login();
9 /// blogs could be disabled altogether
10 if (empty($CFG->bloglevel)) {
11 error('Blogging is disabled!');
14 if (isguest()) {
15 error(get_string('noguestpost', 'blog'));
18 /// blogs are site level
19 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
21 $error = '';
23 $script = '';
24 switch ($action) {
25 /// Adding an official tag from submitted value
26 case 'addofficial':
27 // Double check to make sure user has capability
28 if (!has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
29 error('Can not add official tags tags');
31 if (data_submitted() and confirm_sesskey()) {
33 $otag = trim(required_param('otag', PARAM_NOTAGS));
34 // When adding ofical tag, we see if there's already a personal tag
35 // With the same Name, if there is, we just change the type
36 if ($tag = get_record('tags', 'text', $otag)) {
37 if ($tag->type == 'official') {
38 // official tag already exist
39 $error = get_string('tagalready');
40 break;
41 } else {
42 $tag->type = 'official';
43 update_record('tags', $tag);
44 $tagid = $tag->id;
47 } else { // Brand new offical tag
49 $tag = new object();
50 $tag->userid = $USER->id;
51 $tag->text = $otag;
52 $tag->type = 'official';
54 if (!$tagid = insert_record('tags', $tag)) {
55 error('Can not create tag!');
59 /// Write newly added tags back into window opener.
60 $script = '<script type="text/javascript">
61 //<![CDATA[
62 var o = opener.document.createElement("option");
63 o.innerHTML = "<option>'.$tag->text.'</option>";
64 o.value = '.$tagid.';
65 opener.document.entry[\'otags[]\'].insertBefore(o, null);
66 //]]>
67 </script>';
70 break;
72 /// Deletes a tag.
73 case 'delete':
75 if (data_submitted() and confirm_sesskey()) {
76 $tagids = optional_param('tags', array(), PARAM_INT);
78 if (empty($tagids) or !is_array($tagids)) {
79 // TODO add error message here
80 // $error = 'no data selected';
81 break;
84 foreach ($tagids as $tagid) {
86 if (!$tag = get_record('tags', 'id', $tagid)) {
87 continue; // page refreshed?
90 if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
91 //can not delete
92 continue;
95 if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
96 //can not delete
97 continue;
100 // Delete the tag itself
101 if (!delete_records('tags', 'id', $tagid)) {
102 error('Can not delete tag');
105 // Deleteing all references to this tag
106 if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
107 error('Can not delete blog tag instances');
110 /// Remove parent window option via javascript.
111 $script = '<script>
112 //<![CDATA[
113 var i=0;
114 while (i < window.opener.document.entry[\'otags[]\'].length) {
115 if (window.opener.document.entry[\'otags[]\'].options[i].value == '.$tagid.') {
116 window.opener.document.entry[\'otags[]\'].removeChild(opener.document.entry[\'otags[]\'].options[i]);
118 i++;
121 var i=0;
122 while (i < window.opener.document.entry[\'ptags[]\'].length) {
123 if (window.opener.document.entry[\'ptags[]\'].options[i].value == '.$tagid.') {
124 window.opener.document.entry[\'ptags[]\'].removeChild(opener.document.entry[\'ptags[]\'].options[i]);
126 i++;
128 //]]>
129 </script>';
133 break;
135 default:
136 /// Just display the tags form.
137 break;
141 /// Print the table.
142 print_header (get_string('tagmanagement'), '', '', '', $script);
143 include_once('tags.html');
144 print_footer();