]> git.wh0rd.org - tt-rss.git/blame - modules/pref-filters.php
rework label editor to use dijit.form validation
[tt-rss.git] / modules / pref-filters.php
CommitLineData
ef8be8ea
AD
1<?php
2 function module_pref_filters($link) {
b4e75b2a
AD
3 $subop = $_REQUEST["subop"];
4 $quiet = $_REQUEST["quiet"];
ef8be8ea 5
a740f4b5
AD
6 if ($subop == "getfiltertree") {
7 $root = array();
8 $root['id'] = 'root';
9 $root['name'] = __('Filters');
10 $root['items'] = array();
11
12 $result = db_query($link, "SELECT
13 ttrss_filters.id AS id,reg_exp,
14 ttrss_filter_types.name AS filter_type_name,
15 ttrss_filter_types.description AS filter_type_descr,
16 enabled,
17 inverse,
18 feed_id,
19 action_id,
20 filter_param,
21 filter_type,
22 ttrss_filter_actions.description AS action_description,
23 ttrss_feeds.title AS feed_title,
24 ttrss_filter_actions.name AS action_name,
25 ttrss_filters.action_param AS action_param
26 FROM
27 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
28 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
29 WHERE
30 filter_type = ttrss_filter_types.id AND
31 ttrss_filter_actions.id = action_id AND
32 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
33 ORDER by action_description, reg_exp");
34
35 $cat = false;
36 $cur_action_description = "";
37
38 while ($line = db_fetch_assoc($result)) {
39 if ($cur_action_description != $line['action_description']) {
40
41 if ($cat)
42 array_push($root['items'], $cat);
43
44 $cat = array();
45 $cat['id'] = 'ACTION:' . $line['action_id'];
46 $cat['name'] = $line['action_description'];
47 $cat['items'] = array();
48
49 $cur_action_description = $line['action_description'];
50 }
51
52 if (array_search($line["action_name"],
53 array("score", "tag", "label")) === false) {
54
55 $line["action_param"] = '';
56 } else {
57 if ($line['action_name'] == 'label') {
58
59 $tmp_result = db_query($link, "SELECT fg_color, bg_color
60 FROM ttrss_labels2 WHERE caption = '".
61 db_escape_string($line["action_param"])."' AND
62 owner_uid = " . $_SESSION["uid"]);
63
42623d7f
AD
64 if (db_num_rows($tmp_result) != 0) {
65 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
66 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
a740f4b5 67
42623d7f
AD
68 $tmp = "<span class=\"labelColorIndicator\" style='color : $fg_color; background-color : $bg_color'>&alpha;</span> " . $line['action_param'];
69
70 $line['action_param'] = $tmp;
71 }
a740f4b5
AD
72 }
73 }
74
75 $filter = array();
76 $filter['id'] = 'FILTER:' . $line['id'];
77 $filter['bare_id'] = $line['id'];
78 $filter['name'] = $line['reg_exp'];
79 $filter['type'] = $line['filter_type'];
80 $filter['enabled'] = sql_bool_to_bool($line['enabled']);
81 $filter['param'] = $line['action_param'];
82 $filter['inverse'] = sql_bool_to_bool($line['inverse']);
83 $filter['checkbox'] = false;
84
85 if ($line['feed_id'])
86 $filter['feed'] = $line['feed_title'];
87
88 array_push($cat['items'], $filter);
89 }
90
91 array_push($root['items'], $cat);
92
93 $fl = array();
94 $fl['identifier'] = 'id';
95 $fl['label'] = 'name';
96 $fl['items'] = array($root);
97
98 print json_encode($fl);
99 return;
100 }
101
ef8be8ea
AD
102 if ($subop == "edit") {
103
b4e75b2a 104 $filter_id = db_escape_string($_REQUEST["id"]);
ef8be8ea 105
951906dc
AD
106 header("Content-Type: text/xml");
107 print "<dlg id=\"$subop\">";
108 print "<title>".__('Filter Editor')."</title>";
109 print "<content><![CDATA[";
110
ef8be8ea
AD
111 $result = db_query($link,
112 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
113
47439031 114 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
ef8be8ea
AD
115 $filter_type = db_fetch_result($result, 0, "filter_type");
116 $feed_id = db_fetch_result($result, 0, "feed_id");
117 $action_id = db_fetch_result($result, 0, "action_id");
073ca0e6 118 $action_param = db_fetch_result($result, 0, "action_param");
44d0e774 119 $filter_param = db_fetch_result($result, 0, "filter_param");
ef8be8ea
AD
120
121 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
3f2ff803 122 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
ef8be8ea 123
e6312f6c 124 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
ef8be8ea
AD
125
126 print "<input type=\"hidden\" name=\"op\" value=\"pref-filters\">";
127 print "<input type=\"hidden\" name=\"id\" value=\"$filter_id\">";
128 print "<input type=\"hidden\" name=\"subop\" value=\"editSave\">";
ef8be8ea
AD
129
130 $result = db_query($link, "SELECT id,description
131 FROM ttrss_filter_types ORDER BY description");
132
133 $filter_types = array();
134
135 while ($line = db_fetch_assoc($result)) {
136 //array_push($filter_types, $line["description"]);
89cb787e 137 $filter_types[$line["id"]] = __($line["description"]);
ef8be8ea
AD
138 }
139
10fa6615
AD
140 print "<div class=\"dlgSec\">".__("Match")."</div>";
141
142 print "<div class=\"dlgSecCont\">";
143
6e278744 144 if ($filter_type != 5) {
df00fb80 145 $date_ops_invisible = 'style="display : none"';
6e278744
AD
146 }
147
148 print "<span id=\"filter_dlg_date_mod_box\" $date_ops_invisible>";
149 print __("Date") . " ";
44d0e774
AD
150
151 $filter_params = array(
152 "before" => __("before"),
153 "after" => __("after"));
154
155 print_select_hash("filter_date_modifier", $filter_param,
156 $filter_params);
157
158 print "&nbsp;</span>";
6e278744 159
10fa6615 160 print "<input onkeypress=\"return filterCR(event, filterEditSave)\"
10fa6615
AD
161 name=\"reg_exp\" size=\"30\" value=\"$reg_exp\">";
162
6e278744 163 print "<span id=\"filter_dlg_date_chk_box\" $date_ops_invisible>";
a918f5f9
AD
164 print "&nbsp;<button onclick=\"return filterDlgCheckDate()\">".
165 __('Check it')."</button>";
6e278744
AD
166 print "</span>";
167
168 print "<br/> " . __("on field") . " ";
169 print_select_hash("filter_type", $filter_type, $filter_types,
170 'onchange="filterDlgCheckType(this)"');
10fa6615
AD
171
172 print "<br/>";
173
174 print __("in") . " ";
175 print_feed_select($link, "feed_id", $feed_id);
176
177 print "</div>";
178
ecace165 179 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
10fa6615
AD
180
181 print "<div class=\"dlgSecCont\">";
182
183 print "<select name=\"action_id\"
184 onchange=\"filterDlgCheckAction(this)\">";
185
186 $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions
187 ORDER BY name");
188
189 while ($line = db_fetch_assoc($result)) {
190 $is_sel = ($line["id"] == $action_id) ? "selected" : "";
191 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
192 }
193
194 print "</select>";
195
ceb30ba4 196 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
10fa6615 197
143a4973
AD
198 print "<span id=\"filter_dlg_param_box\" style=\"$param_hidden\">";
199 print " " . __("with parameters:") . " ";
ceb30ba4
AD
200
201 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
202
203 print "<input size=\"20\" style=\"$param_int_hidden\"
074bf20c 204 onkeypress=\"return filterCR(event, filterEditSave)\"
143a4973 205 name=\"action_param\" value=\"$action_param\">";
ceb30ba4
AD
206
207 $param_int_hidden = ($action_id == 7) ? "" : "display : none";
208
209 print_label_select($link, "action_param_label", $action_param,
210 $param_int_hidden);
211
143a4973 212 print "</span>";
10fa6615 213
143a4973 214 print "&nbsp;"; // tiny layout hack
10fa6615
AD
215
216 print "</div>";
217
218 print "<div class=\"dlgSec\">".__("Options")."</div>";
219 print "<div class=\"dlgSecCont\">";
220
ecace165
AD
221 print "<div style=\"line-height : 100%\">";
222
10fa6615
AD
223 if ($enabled) {
224 $checked = "checked";
225 } else {
226 $checked = "";
227 }
228
229 print "<input type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
230 <label for=\"enabled\">".__('Enabled')."</label><br/>";
231
232 if ($inverse) {
233 $checked = "checked";
234 } else {
235 $checked = "";
236 }
237
238 print "<input type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
239 <label for=\"inverse\">".__('Inverse match')."</label>";
240
241 print "</div>";
ecace165 242 print "</div>";
10fa6615
AD
243
244 print "<div class=\"dlgButtons\">";
ef8be8ea 245
ddb61e79 246 $reg_exp = htmlspecialchars($reg_exp, ENT_QUOTES); // second escaping seems to be needed for javascript
143a4973
AD
247
248 print "<div style=\"float : left\">";
577399e8
AD
249 print "<button onclick='return removeFilter($filter_id, \"$reg_exp\")'>".
250 __('Remove')."</button>";
143a4973
AD
251 print "</div>";
252
577399e8
AD
253 print "<button onclick=\"return filterEditSave()\">".
254 __('Save')."</button> ";
ef8be8ea 255
577399e8
AD
256 print "<button onclick=\"return filterEditCancel()\">".
257 __('Cancel')."</button>";
ef8be8ea 258
951906dc 259 print "]]></content></dlg>";
ef8be8ea
AD
260
261 return;
262 }
263
264
265 if ($subop == "editSave") {
266
b8ffa322
AD
267 global $memcache;
268
269 if ($memcache) $memcache->flush();
270
b4e75b2a
AD
271 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
272 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
273 $filter_id = db_escape_string($_REQUEST["id"]);
274 $feed_id = db_escape_string($_REQUEST["feed_id"]);
275 $action_id = db_escape_string($_REQUEST["action_id"]);
276 $action_param = db_escape_string($_REQUEST["action_param"]);
277 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
278 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
279 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
ef8be8ea 280
44d0e774 281 # for the time being, no other filters use params anyway...
b4e75b2a 282 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
44d0e774 283
ef8be8ea
AD
284 if (!$feed_id) {
285 $feed_id = 'NULL';
286 } else {
287 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
288 }
ceb30ba4
AD
289
290 /* When processing 'assign label' filters, action_param_label dropbox
291 * overrides action_param */
292
293 if ($action_id == 7) {
294 $action_param = $action_param_label;
295 }
296
ef8be8ea
AD
297 $result = db_query($link, "UPDATE ttrss_filters SET
298 reg_exp = '$reg_exp',
299 feed_id = $feed_id,
300 action_id = '$action_id',
301 filter_type = '$filter_type',
073ca0e6 302 enabled = $enabled,
3f2ff803 303 inverse = $inverse,
44d0e774
AD
304 action_param = '$action_param',
305 filter_param = '$filter_param'
4dccf1ed 306 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
ef8be8ea
AD
307 }
308
309 if ($subop == "remove") {
a522a767
AD
310
311 if ($memcache) $memcache->flush();
ef8be8ea 312
b4e75b2a 313 $ids = split(",", db_escape_string($_REQUEST["ids"]));
ef8be8ea 314
4dccf1ed
AD
315 foreach ($ids as $id) {
316 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
ef8be8ea
AD
317 }
318 }
319
320 if ($subop == "add") {
a522a767
AD
321
322 if ($memcache) $memcache->flush();
323
b4e75b2a
AD
324 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
325 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
326 $feed_id = db_escape_string($_REQUEST["feed_id"]);
327 $action_id = db_escape_string($_REQUEST["action_id"]);
328 $action_param = db_escape_string($_REQUEST["action_param"]);
329 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
330 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
4dccf1ed 331
44d0e774 332 # for the time being, no other filters use params anyway...
b4e75b2a 333 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
44d0e774 334
4dccf1ed
AD
335 if (!$regexp) return;
336
337 if (!$feed_id) {
338 $feed_id = 'NULL';
339 } else {
340 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
341 }
342
db34a63e
AD
343 /* When processing 'assign label' filters, action_param_label dropbox
344 * overrides action_param */
345
346 if ($action_id == 7) {
347 $action_param = $action_param_label;
348 }
349
4dccf1ed
AD
350 $result = db_query($link,
351 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
a8732d0c 352 action_id, action_param, inverse, filter_param)
4dccf1ed
AD
353 VALUES
354 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
a8732d0c 355 $feed_id, '$action_id', '$action_param', $inverse, '$filter_param')");
4dccf1ed 356
5e6f933a
AD
357 if (db_affected_rows($link, $result) != 0) {
358 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
359 }
360
361 return;
ef8be8ea
AD
362 }
363
364 if ($quiet) return;
365
b4e75b2a 366 $sort = db_escape_string($_REQUEST["sort"]);
ef8be8ea
AD
367
368 if (!$sort || $sort == "undefined") {
369 $sort = "reg_exp";
370 }
371
ef8be8ea
AD
372 $result = db_query($link, "SELECT id,description
373 FROM ttrss_filter_types ORDER BY description");
374
375 $filter_types = array();
376
377 while ($line = db_fetch_assoc($result)) {
378 //array_push($filter_types, $line["description"]);
379 $filter_types[$line["id"]] = $line["description"];
380 }
381
9c87d75c 382
b4e75b2a 383 $filter_search = db_escape_string($_REQUEST["search"]);
9c87d75c 384
b4e75b2a 385 if (array_key_exists("search", $_REQUEST)) {
9c87d75c
AD
386 $_SESSION["prefs_filter_search"] = $filter_search;
387 } else {
388 $filter_search = $_SESSION["prefs_filter_search"];
389 }
8df7184c
AD
390
391 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
392 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
393 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
9c87d75c 394
d69fa6d6
AD
395 print "<div dojoType=\"dijit.form.DropDownButton\">".
396 "<span>" . __('Select')."</span>";
397 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
a740f4b5 398 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
d69fa6d6 399 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
a740f4b5 400 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
d69fa6d6
AD
401 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
402 print "</div></div>";
403
1985a5e0
AD
404 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
405 __('Create filter')."</button dojoType=\"dijit.form.Button\"> ";
0d32b41e 406
1985a5e0
AD
407 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
408 __('Edit')."</button dojoType=\"dijit.form.Button\"> ";
ef8be8ea 409
1985a5e0
AD
410 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
411 __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
1e5548db 412
fe1c99af 413 if (defined('_ENABLE_FEED_DEBUGGING')) {
1985a5e0
AD
414 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
415 __('Rescore articles')."</button dojoType=\"dijit.form.Button\"> ";
fe1c99af 416 }
95a948a5 417
8df7184c
AD
418 print "</div>"; # toolbar
419 print "</div>"; # toolbar-frame
420 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
421
a740f4b5
AD
422 print "<div id=\"filterlistLoading\">
423 <img src='images/indicator_tiny.gif'>".
424 __("Loading, please wait...")."</div>";
425
426 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
427 url=\"backend.php?op=pref-filters&subop=getfiltertree\">
428 </div>
429 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
430 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
431 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
432 </div>
433 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
434 model=\"filterModel\" openOnClick=\"true\">
435 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
436 Element.hide(\"filterlistLoading\");
437 </script>
438 </div>";
8df7184c
AD
439
440 print "</div>"; #pane
441 print "</div>"; #container
ef8be8ea 442 }
ceb30ba4 443
ef8be8ea 444?>