]> git.wh0rd.org - tt-rss.git/blob - classes/pref/labels.php
make pluginhost a singleton
[tt-rss.git] / classes / pref / labels.php
1 <?php
2 class Pref_Labels extends Handler_Protected {
3
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getlabeltree", "edit");
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
10 function edit() {
11 $label_id = $this->dbh->escape_string($_REQUEST['id']);
12
13 $result = $this->dbh->query("SELECT * FROM ttrss_labels2 WHERE
14 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
15
16 $line = $this->dbh->fetch_assoc($result);
17
18 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$label_id\">";
19 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-labels\">";
20 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
21
22 print "<div class=\"dlgSec\">".__("Caption")."</div>";
23
24 print "<div class=\"dlgSecCont\">";
25
26 $fg_color = $line['fg_color'];
27 $bg_color = $line['bg_color'];
28
29 print "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-bottom : 4px; margin-right : 4px'>&alpha;</span>";
30
31 print "<input style=\"font-size : 16px\" name=\"caption\"
32 dojoType=\"dijit.form.ValidationTextBox\"
33 required=\"true\"
34 value=\"".htmlspecialchars($line['caption'])."\">";
35
36 print "</div>";
37 print "<div class=\"dlgSec\">" . __("Colors") . "</div>";
38 print "<div class=\"dlgSecCont\">";
39
40 print "<table cellspacing=\"0\">";
41
42 print "<tr><td>".__("Foreground:")."</td><td>".__("Background:").
43 "</td></tr>";
44
45 print "<tr><td style='padding-right : 10px'>";
46
47 print "<input dojoType=\"dijit.form.TextBox\"
48 style=\"display : none\" id=\"labelEdit_fgColor\"
49 name=\"fg_color\" value=\"$fg_color\">";
50 print "<input dojoType=\"dijit.form.TextBox\"
51 style=\"display : none\" id=\"labelEdit_bgColor\"
52 name=\"bg_color\" value=\"$bg_color\">";
53
54 print "<div dojoType=\"dijit.ColorPalette\">
55 <script type=\"dojo/method\" event=\"onChange\" args=\"fg_color\">
56 dijit.byId(\"labelEdit_fgColor\").attr('value', fg_color);
57 $('label-editor-indicator').setStyle({color: fg_color});
58 </script>
59 </div>";
60 print "</div>";
61
62 print "</td><td>";
63
64 print "<div dojoType=\"dijit.ColorPalette\">
65 <script type=\"dojo/method\" event=\"onChange\" args=\"bg_color\">
66 dijit.byId(\"labelEdit_bgColor\").attr('value', bg_color);
67 $('label-editor-indicator').setStyle({backgroundColor: bg_color});
68 </script>
69 </div>";
70 print "</div>";
71
72 print "</td></tr></table>";
73 print "</div>";
74
75 # print "</form>";
76
77 print "<div class=\"dlgButtons\">";
78 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('labelEditDlg').execute()\">".
79 __('Save')."</button>";
80 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('labelEditDlg').hide()\">".
81 __('Cancel')."</button>";
82 print "</div>";
83
84 return;
85 }
86
87 function getlabeltree() {
88 $root = array();
89 $root['id'] = 'root';
90 $root['name'] = __('Labels');
91 $root['items'] = array();
92
93 $result = $this->dbh->query("SELECT *
94 FROM ttrss_labels2
95 WHERE owner_uid = ".$_SESSION["uid"]."
96 ORDER BY caption");
97
98 while ($line = $this->dbh->fetch_assoc($result)) {
99 $label = array();
100 $label['id'] = 'LABEL:' . $line['id'];
101 $label['bare_id'] = $line['id'];
102 $label['name'] = $line['caption'];
103 $label['fg_color'] = $line['fg_color'];
104 $label['bg_color'] = $line['bg_color'];
105 $label['type'] = 'label';
106 $label['checkbox'] = false;
107
108 array_push($root['items'], $label);
109 }
110
111 $fl = array();
112 $fl['identifier'] = 'id';
113 $fl['label'] = 'name';
114 $fl['items'] = array($root);
115
116 print json_encode($fl);
117 return;
118 }
119
120 function colorset() {
121 $kind = $this->dbh->escape_string($_REQUEST["kind"]);
122 $ids = explode(',', $this->dbh->escape_string($_REQUEST["ids"]));
123 $color = $this->dbh->escape_string($_REQUEST["color"]);
124 $fg = $this->dbh->escape_string($_REQUEST["fg"]);
125 $bg = $this->dbh->escape_string($_REQUEST["bg"]);
126
127 foreach ($ids as $id) {
128
129 if ($kind == "fg" || $kind == "bg") {
130 $this->dbh->query("UPDATE ttrss_labels2 SET
131 ${kind}_color = '$color' WHERE id = '$id'
132 AND owner_uid = " . $_SESSION["uid"]);
133 } else {
134 $this->dbh->query("UPDATE ttrss_labels2 SET
135 fg_color = '$fg', bg_color = '$bg' WHERE id = '$id'
136 AND owner_uid = " . $_SESSION["uid"]);
137 }
138
139 $caption = $this->dbh->escape_string(label_find_caption($id, $_SESSION["uid"]));
140
141 /* Remove cached data */
142
143 $this->dbh->query("UPDATE ttrss_user_entries SET label_cache = ''
144 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
145
146 }
147
148 return;
149 }
150
151 function colorreset() {
152 $ids = explode(',', $this->dbh->escape_string($_REQUEST["ids"]));
153
154 foreach ($ids as $id) {
155 $this->dbh->query("UPDATE ttrss_labels2 SET
156 fg_color = '', bg_color = '' WHERE id = '$id'
157 AND owner_uid = " . $_SESSION["uid"]);
158
159 $caption = $this->dbh->escape_string(label_find_caption($id, $_SESSION["uid"]));
160
161 /* Remove cached data */
162
163 $this->dbh->query("UPDATE ttrss_user_entries SET label_cache = ''
164 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
165 }
166
167 }
168
169 function save() {
170
171 $id = $this->dbh->escape_string($_REQUEST["id"]);
172 $caption = $this->dbh->escape_string(trim($_REQUEST["caption"]));
173
174 $this->dbh->query("BEGIN");
175
176 $result = $this->dbh->query("SELECT caption FROM ttrss_labels2
177 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
178
179 if ($this->dbh->num_rows($result) != 0) {
180 $old_caption = $this->dbh->fetch_result($result, 0, "caption");
181
182 $result = $this->dbh->query("SELECT id FROM ttrss_labels2
183 WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
184
185 if ($this->dbh->num_rows($result) == 0) {
186 if ($caption) {
187 $result = $this->dbh->query("UPDATE ttrss_labels2 SET
188 caption = '$caption' WHERE id = '$id' AND
189 owner_uid = " . $_SESSION["uid"]);
190
191 /* Update filters that reference label being renamed */
192
193 $old_caption = $this->dbh->escape_string($old_caption);
194
195 $this->dbh->query("UPDATE ttrss_filters2_actions SET
196 action_param = '$caption' WHERE action_param = '$old_caption'
197 AND action_id = 7
198 AND filter_id IN (SELECT id FROM ttrss_filters2 WHERE owner_uid = ".$_SESSION["uid"].")");
199
200 print $_REQUEST["value"];
201 } else {
202 print $old_caption;
203 }
204 } else {
205 print $old_caption;
206 }
207 }
208
209 $this->dbh->query("COMMIT");
210
211 return;
212 }
213
214 function remove() {
215
216 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
217
218 foreach ($ids as $id) {
219 label_remove($id, $_SESSION["uid"]);
220 }
221
222 }
223
224 function add() {
225 $caption = $this->dbh->escape_string($_REQUEST["caption"]);
226 $output = $this->dbh->escape_string($_REQUEST["output"]);
227
228 if ($caption) {
229
230 if (label_create($caption)) {
231 if (!$output) {
232 print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
233 }
234 }
235
236 if ($output == "select") {
237 header("Content-Type: text/xml");
238
239 print "<rpc-reply><payload>";
240
241 print_label_select("select_label",
242 $caption, "");
243
244 print "</payload></rpc-reply>";
245 }
246 }
247
248 return;
249 }
250
251 function index() {
252
253 $sort = $this->dbh->escape_string($_REQUEST["sort"]);
254
255 if (!$sort || $sort == "undefined") {
256 $sort = "caption";
257 }
258
259 $label_search = $this->dbh->escape_string($_REQUEST["search"]);
260
261 if (array_key_exists("search", $_REQUEST)) {
262 $_SESSION["prefs_label_search"] = $label_search;
263 } else {
264 $label_search = $_SESSION["prefs_label_search"];
265 }
266
267 print "<div id=\"pref-label-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
268 print "<div id=\"pref-label-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
269 print "<div id=\"pref-label-toolbar\" dojoType=\"dijit.Toolbar\">";
270
271 print "<div dojoType=\"dijit.form.DropDownButton\">".
272 "<span>" . __('Select')."</span>";
273 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
274 print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(true)\"
275 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
276 print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(false)\"
277 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
278 print "</div></div>";
279
280 print"<button dojoType=\"dijit.form.Button\" onclick=\"return addLabel()\">".
281 __('Create label')."</button dojoType=\"dijit.form.Button\"> ";
282
283 print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedLabels()\">".
284 __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
285
286 print "<button dojoType=\"dijit.form.Button\" onclick=\"labelColorReset()\">".
287 __('Clear colors')."</button dojoType=\"dijit.form.Button\">";
288
289
290 print "</div>"; #toolbar
291 print "</div>"; #pane
292 print "<div id=\"pref-label-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
293
294 print "<div id=\"labellistLoading\">
295 <img src='images/indicator_tiny.gif'>".
296 __("Loading, please wait...")."</div>";
297
298 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"labelStore\"
299 url=\"backend.php?op=pref-labels&method=getlabeltree\">
300 </div>
301 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"labelModel\" store=\"labelStore\"
302 query=\"{id:'root'}\" rootId=\"root\"
303 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
304 </div>
305 <div dojoType=\"fox.PrefLabelTree\" id=\"labelTree\"
306 model=\"labelModel\" openOnClick=\"true\">
307 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
308 Element.hide(\"labellistLoading\");
309 </script>
310 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
311 var id = String(item.id);
312 var bare_id = id.substr(id.indexOf(':')+1);
313
314 if (id.match('LABEL:')) {
315 editLabel(bare_id);
316 }
317 </script>
318 </div>";
319
320 print "</div>"; #pane
321
322 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
323 "hook_prefs_tab", "prefLabels");
324
325 print "</div>"; #container
326
327 }
328 }
329
330 ?>