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