some cleaning, removed config.html for admin
[moodle.git] / blog / tags.php
CommitLineData
4a173181 1<?php
2require_once('../config.php');
3
4//form process
4a173181 5$mode = optional_param('mode','',PARAM_ALPHA);
6
7switch ($mode) {
8 case 'addofficial':
9154b440 9
10 if (!isadmin() || !confirm_sesskey()) {
11 die('you can not add official tags');
12 }
13
5c625033 14 if (($otag = optional_param('otag', '', PARAM_ALPHA)) && (!get_record('tags','text',$otag))) {
4a173181 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 }
28
29 break;
30
9154b440 31 case 'addpersonal': /// everyone can add
32 if (!confirm_sesskey() || isguest() || !isset($USER->id)) {
33 error ('you can not add tags');
34 }
35
5c625033 36 if (($ptag = optional_param('ptag', '', PARAM_ALPHA)) && (!get_record('tags','text',$ptag))) {
4a173181 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':
9154b440 54 if (!confirm_sesskey()) {
55 error('you can not delete tags');
56 }
57
5c625033 58 if ($tags = optional_param('tags', 0, PARAM_INT)) {
59
60 foreach ($tags as $tag) {
61
62 $blogtag = get_record('tags','id',$tag);
63
64 if (!isadmin() and $USER->id != $blogtag->userid) {
65 notify('no right to delete');
66 continue;
4a173181 67 }
5c625033 68
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;
4a173181 73 }
4a173181 74
5c625033 75 delete_records('tags','id',$tag);
76 delete_records('blog_tag_instance', 'tagid', $tag);
77
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 }
87
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 }
95
96 </script>';
97 }
4a173181 98 }
99 //write back to window.opener
100 break;
101
102 default:
103 break;
104}
105
106//print the table
107
108print_header();
109
110include_once('tags.html');
111
112print_footer();
113
114
115?>