some cleaning, removed config.html for admin
[moodle.git] / blog / tags.php
1 <?php
2 require_once('../config.php');
4 //form process
5 $mode = optional_param('mode','',PARAM_ALPHA);
7 switch ($mode) {
8     case 'addofficial':
9     
10         if (!isadmin() || !confirm_sesskey()) {
11             die('you can not add official tags');
12         }
13         
14         if (($otag = optional_param('otag', '', PARAM_ALPHA)) && (!get_record('tags','text',$otag))) {
15             $tag->userid = $USER->id;
16             $tag->text = $otag;
17             $tag->type = 'official';
18             $tagid = insert_record('tags', $tag);
19             echo '<script language="JavaScript" type="text/javascript">
20             var o = opener.document.createElement("option");
21             o.innerHTML = "<option>'.$tag->text.'</option>";
22             o.value = '.$tagid.';
23             opener.document.entry[\'otags[]\'].insertBefore(o, null);
24             </script>';
25         } else {
26             error ('there is already a tag with this name!');
27         }
29     break;
30     
31     case 'addpersonal':    /// everyone can add
32         if (!confirm_sesskey() || isguest() || !isset($USER->id)) {
33             error ('you can not add tags');
34         }
35         
36         if (($ptag = optional_param('ptag', '', PARAM_ALPHA)) && (!get_record('tags','text',$ptag))) {
37             $tag->userid = $USER->id;
38             $tag->text = $ptag;
39             $tag->type = 'personal';
40             $tagid = insert_record('tags', $tag);
41             echo '<script language="JavaScript" type="text/javascript">
42             var o = opener.document.createElement("option");
43             o.innerHTML = "<option>'.$tag->text.'</option>";
44             o.value = '.$tagid.';
45             opener.document.entry[\'ptags[]\'].insertBefore(o, null);
46             </script>';
47         } else {
48             error ('there is already a tag with this name!');
49         }
50         //write back to window.opener
51     break;
52     
53     case 'delete':
54         if (!confirm_sesskey()) {
55             error('you can not delete tags');
56         }
57         
58         if ($tags = optional_param('tags', 0, PARAM_INT)) {
59         
60             foreach ($tags as $tag) {
62                 $blogtag = get_record('tags','id',$tag);
64                 if (!isadmin() and $USER->id != $blogtag->userid) {
65                     notify('no right to delete');
66                     continue;
67                 }
69                 /// Only admin can delete tags that are referenced
70                 if (!isadmin() && get_records('blog_tag_instance','tagid', $tag)) {
71                     notify('tag is used by other users, can not delete!');
72                     continue;
73                 }
75                 delete_records('tags','id',$tag);
76                 delete_records('blog_tag_instance', 'tagid', $tag);
78                 /// remove parent window option via javascript
79                 echo '<script>
80                 var i=0;
81                 while (i < window.opener.document.entry[\'otags[]\'].length) {
82                     if (window.opener.document.entry[\'otags[]\'].options[i].value == '.$tag.') {
83                         window.opener.document.entry[\'otags[]\'].removeChild(opener.document.entry[\'otags[]\'].options[i]);
84                     }
85                     i++;
86                 }
88                 var i=0;
89                 while (i < window.opener.document.entry[\'ptags[]\'].length) {
90                     if (window.opener.document.entry[\'ptags[]\'].options[i].value == '.$tag.') {
91                         window.opener.document.entry[\'ptags[]\'].removeChild(opener.document.entry[\'ptags[]\'].options[i]);
92                     }
93                     i++;
94                 }
96                 </script>';
97             }
98         }
99         //write back to window.opener
100     break;
101     
102     default:
103     break;
106 //print the table
108 print_header();
110 include_once('tags.html');
112 print_footer();
115 ?>