]> git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
implement filter titles
[tt-rss.git] / classes / pref / filters.php
1 <?php
2 class Pref_Filters extends Handler_Protected {
3
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule",
6 "newaction");
7
8 return array_search($method, $csrf_ignored) !== false;
9 }
10
11 function testFilter() {
12 $filter = array();
13
14 $filter["enabled"] = true;
15 $filter["match_any_rule"] = sql_bool_to_bool(
16 checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"])));
17 $filter["inverse"] = sql_bool_to_bool(
18 checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"])));
19
20 $filter["rules"] = array();
21
22 $result = db_query($this->link, "SELECT id,name FROM ttrss_filter_types");
23
24 $filter_types = array();
25 while ($line = db_fetch_assoc($result)) {
26 $filter_types[$line["id"]] = $line["name"];
27 }
28
29 $rctr = 0;
30 foreach ($_REQUEST["rule"] AS $r) {
31 $rule = json_decode($r, true);
32
33 if ($rule && $rctr < 5) {
34 $rule["type"] = $filter_types[$rule["filter_type"]];
35 unset($rule["filter_type"]);
36
37 if (strpos($rule["feed_id"], "CAT:") === 0) {
38 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
39 unset($rule["feed_id"]);
40 }
41
42 array_push($filter["rules"], $rule);
43
44 ++$rctr;
45 } else {
46 break;
47 }
48 }
49
50 $feed_title = getFeedTitle($this->link, $feed);
51
52 $qfh_ret = queryFeedHeadlines($this->link, -4, 30, "", false, false, false,
53 "date_entered DESC", 0, $_SESSION["uid"], $filter);
54
55 $result = $qfh_ret[0];
56
57 $articles = array();
58 $found = 0;
59
60 print __("Articles matching this filter:");
61
62 print "<div class=\"filterTestHolder\">";
63 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
64
65 while ($line = db_fetch_assoc($result)) {
66
67 $entry_timestamp = strtotime($line["updated"]);
68 $entry_tags = get_article_tags($this->link, $line["id"], $_SESSION["uid"]);
69
70 $content_preview = truncate_string(
71 strip_tags($line["content_preview"]), 100, '...');
72
73 if ($line["feed_title"])
74 $feed_title = $line["feed_title"];
75
76 print "<tr>";
77
78 print "<td width='5%' align='center'><input
79 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
80 disabled=\"1\" type=\"checkbox\"></td>";
81 print "<td>";
82
83 print $line["title"];
84 print "&nbsp;(";
85 print "<b>" . $feed_title . "</b>";
86 print "):&nbsp;";
87 print "<span class=\"insensitive\">" . $content_preview . "</span>";
88 print " " . mb_substr($line["date_entered"], 0, 16);
89
90 print "</td></tr>";
91
92 $found++;
93 }
94
95 if ($found == 0) {
96 print "<tr><td align='center'>" .
97 __("No recent articles matching this filter have been found.");
98
99 print "</td></tr><tr><td class='insensitive' align='center'>";
100
101 print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
102
103 print "</td></tr>";
104
105 }
106
107 print "</table></div>";
108
109 print "<div style='text-align : center'>";
110 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
111 __('Close this window')."</button>";
112 print "</div>";
113
114 }
115
116
117 function getfiltertree() {
118 $root = array();
119 $root['id'] = 'root';
120 $root['name'] = __('Filters');
121 $root['items'] = array();
122
123 $filter_search = $_SESSION["prefs_filter_search"];
124
125 $result = db_query($this->link, "SELECT *,
126 (SELECT action_param FROM ttrss_filters2_actions
127 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
128 (SELECT action_id FROM ttrss_filters2_actions
129 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
130 (SELECT description FROM ttrss_filter_actions
131 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
132 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
133 (SELECT reg_exp FROM ttrss_filters2_rules
134 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
135 FROM ttrss_filters2 WHERE
136 owner_uid = ".$_SESSION["uid"]." ORDER BY action_id,reg_exp");
137
138
139 $action_id = -1;
140 $folder = array();
141 $folder['items'] = array();
142
143 while ($line = db_fetch_assoc($result)) {
144
145 if ($action_id != $line["action_id"]) {
146 if (count($folder['items']) > 0) {
147 array_push($root['items'], $folder);
148 }
149
150 $folder = array();
151 $folder['id'] = $line["action_id"];
152 $folder['name'] = __($line["action_name"]);
153 $folder['items'] = array();
154 $action_id = $line["action_id"];
155 }
156
157 $name = $this->getFilterName($line["id"]);
158
159 $match_ok = false;
160 if ($filter_search) {
161 $rules_result = db_query($this->link,
162 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
163
164 while ($rule_line = db_fetch_assoc($rules_result)) {
165 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
166 $match_ok = true;
167 break;
168 }
169 }
170 }
171
172 if ($line['action_id'] == 7) {
173 $label_result = db_query($this->link, "SELECT fg_color, bg_color
174 FROM ttrss_labels2 WHERE caption = '".db_escape_string($this->link, $line['action_param'])."' AND
175 owner_uid = " . $_SESSION["uid"]);
176
177 if (db_num_rows($label_result) > 0) {
178 $fg_color = db_fetch_result($label_result, 0, "fg_color");
179 $bg_color = db_fetch_result($label_result, 0, "bg_color");
180
181 if ($name[1]) $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
182 }
183 }
184
185 $filter = array();
186 $filter['id'] = 'FILTER:' . $line['id'];
187 $filter['bare_id'] = $line['id'];
188 $filter['name'] = $name[0];
189 $filter['param'] = $name[1];
190 $filter['checkbox'] = false;
191 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
192
193 if (!$filter_search || $match_ok) {
194 array_push($folder['items'], $filter);
195 }
196 }
197
198 if (count($folder['items']) > 0) {
199 array_push($root['items'], $folder);
200 }
201
202 $fl = array();
203 $fl['identifier'] = 'id';
204 $fl['label'] = 'name';
205 $fl['items'] = array($root);
206
207 print json_encode($fl);
208 return;
209 }
210
211 function edit() {
212
213 $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
214
215 $result = db_query($this->link,
216 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
217
218 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
219 $match_any_rule = sql_bool_to_bool(db_fetch_result($result, 0, "match_any_rule"));
220 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
221 $title = htmlspecialchars(db_fetch_result($result, 0, "title"));
222
223 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
224
225 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
226 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
227 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
228 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
229
230 print "<div class=\"dlgSec\">".__("Caption")."</div>";
231
232 print "<input dojoType=\"dijit.form.TextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
233
234 print "</div>";
235
236 print "<div class=\"dlgSec\">".__("Match")."</div>";
237
238 print "<div dojoType=\"dijit.Toolbar\">";
239
240 print "<div dojoType=\"dijit.form.DropDownButton\">".
241 "<span>" . __('Select')."</span>";
242 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
243 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
244 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
245 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
246 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
247 print "</div></div>";
248
249 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
250 __('Add')."</button> ";
251
252 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
253 __('Delete')."</button> ";
254
255 print "</div>";
256
257 print "<ul id='filterDlg_Matches'>";
258
259 $rules_result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
260 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
261
262 while ($line = db_fetch_assoc($rules_result)) {
263 if (sql_bool_to_bool($line["cat_filter"])) {
264 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
265 }
266
267 unset($line["cat_filter"]);
268 unset($line["cat_id"]);
269 unset($line["filter_id"]);
270 unset($line["id"]);
271 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
272
273 $data = htmlspecialchars(json_encode($line));
274
275 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
276 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
277 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
278 }
279
280 print "</ul>";
281
282 print "</div>";
283
284 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
285
286 print "<div dojoType=\"dijit.Toolbar\">";
287
288 print "<div dojoType=\"dijit.form.DropDownButton\">".
289 "<span>" . __('Select')."</span>";
290 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
291 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
292 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
293 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
294 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
295 print "</div></div>";
296
297 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
298 __('Add')."</button> ";
299
300 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
301 __('Delete')."</button> ";
302
303 print "</div>";
304
305 print "<ul id='filterDlg_Actions'>";
306
307 $actions_result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
308 WHERE filter_id = '$filter_id' ORDER BY id");
309
310 while ($line = db_fetch_assoc($actions_result)) {
311 $line["action_param_label"] = $line["action_param"];
312
313 unset($line["filter_id"]);
314 unset($line["id"]);
315
316 $data = htmlspecialchars(json_encode($line));
317
318 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
319 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
320 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
321 }
322
323 print "</ul>";
324
325 print "</div>";
326
327 if ($enabled) {
328 $checked = "checked=\"1\"";
329 } else {
330 $checked = "";
331 }
332
333 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
334 <label for=\"enabled\">".__('Enabled')."</label>";
335
336 if ($match_any_rule) {
337 $checked = "checked=\"1\"";
338 } else {
339 $checked = "";
340 }
341
342 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
343 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
344
345 if ($inverse) {
346 $checked = "checked=\"1\"";
347 } else {
348 $checked = "";
349 }
350
351 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
352 <label for=\"inverse\">".__('Inverse matching')."</label>";
353
354 print "<p/>";
355
356 print "<div class=\"dlgButtons\">";
357
358 print "<div style=\"float : left\">";
359 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
360 __('Remove')."</button>";
361 print "</div>";
362
363 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
364 __('Test')."</button> ";
365
366 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
367 __('Save')."</button> ";
368
369 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
370 __('Cancel')."</button>";
371
372 print "</div>";
373 }
374
375 private function getRuleName($rule) {
376 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
377
378 $feed_id = $rule["feed_id"];
379
380 if (strpos($feed_id, "CAT:") === 0) {
381 $feed_id = (int) substr($feed_id, 4);
382 $feed = getCategoryTitle($this->link, $feed_id);
383 } else {
384 $feed_id = (int) $feed_id;
385
386 if ($rule["feed_id"])
387 $feed = getFeedTitle($this->link, (int)$rule["feed_id"]);
388 else
389 $feed = __("All feeds");
390 }
391
392 $result = db_query($this->link, "SELECT description FROM ttrss_filter_types
393 WHERE id = ".(int)$rule["filter_type"]);
394 $filter_type = db_fetch_result($result, 0, "description");
395
396 return T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
397 $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "");
398 }
399
400 function printRuleName() {
401 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
402 }
403
404 private function getActionName($action) {
405 $result = db_query($this->link, "SELECT description FROM
406 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
407
408 $title = __(db_fetch_result($result, 0, "description"));
409
410 if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
411 $action["action_id"] == 7)
412 $title .= ": " . $action["action_param"];
413
414 return $title;
415 }
416
417 function printActionName() {
418 print $this->getActionName(json_decode($_REQUEST["action"], true));
419 }
420
421 function editSave() {
422 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
423 return $this->testFilter();
424 }
425
426 # print_r($_REQUEST);
427
428 $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
429 $enabled = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["enabled"]));
430 $match_any_rule = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"]));
431 $inverse = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"]));
432 $title = db_escape_string($this->link, $_REQUEST["title"]);
433
434 $result = db_query($this->link, "UPDATE ttrss_filters2 SET enabled = $enabled,
435 match_any_rule = $match_any_rule,
436 inverse = $inverse,
437 title = '$title'
438 WHERE id = '$filter_id'
439 AND owner_uid = ". $_SESSION["uid"]);
440
441 $this->saveRulesAndActions($filter_id);
442
443 }
444
445 function remove() {
446
447 $ids = split(",", db_escape_string($this->link, $_REQUEST["ids"]));
448
449 foreach ($ids as $id) {
450 db_query($this->link, "DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
451 }
452 }
453
454 private function saveRulesAndActions($filter_id) {
455
456 db_query($this->link, "DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
457 db_query($this->link, "DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
458
459 if ($filter_id) {
460 /* create rules */
461
462 $rules = array();
463 $actions = array();
464
465 foreach ($_REQUEST["rule"] as $rule) {
466 $rule = json_decode($rule, true);
467 unset($rule["id"]);
468
469 if (array_search($rule, $rules) === false) {
470 array_push($rules, $rule);
471 }
472 }
473
474 foreach ($_REQUEST["action"] as $action) {
475 $action = json_decode($action, true);
476 unset($action["id"]);
477
478 if (array_search($action, $actions) === false) {
479 array_push($actions, $action);
480 }
481 }
482
483 foreach ($rules as $rule) {
484 if ($rule) {
485
486 $reg_exp = strip_tags(db_escape_string($this->link, trim($rule["reg_exp"])));
487 $inverse = isset($rule["inverse"]) ? "true" : "false";
488
489 $filter_type = (int) db_escape_string($this->link, trim($rule["filter_type"]));
490 $feed_id = db_escape_string($this->link, trim($rule["feed_id"]));
491
492 if (strpos($feed_id, "CAT:") === 0) {
493
494 $cat_filter = bool_to_sql_bool(true);
495 $cat_id = (int) substr($feed_id, 4);
496 $feed_id = "NULL";
497
498 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
499 } else {
500 $cat_filter = bool_to_sql_bool(false);
501 $feed_id = (int) $feed_id;
502 $cat_id = "NULL";
503
504 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
505 }
506
507 $query = "INSERT INTO ttrss_filters2_rules
508 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
509 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
510
511 db_query($this->link, $query);
512 }
513 }
514
515 foreach ($actions as $action) {
516 if ($action) {
517
518 $action_id = (int) db_escape_string($this->link, $action["action_id"]);
519 $action_param = db_escape_string($this->link, $action["action_param"]);
520 $action_param_label = db_escape_string($this->link, $action["action_param_label"]);
521
522 if ($action_id == 7) {
523 $action_param = $action_param_label;
524 }
525
526 if ($action_id == 6) {
527 $action_param = (int) str_replace("+", "", $action_param);
528 }
529
530 $query = "INSERT INTO ttrss_filters2_actions
531 (filter_id, action_id, action_param) VALUES
532 ('$filter_id', '$action_id', '$action_param')";
533
534 db_query($this->link, $query);
535 }
536 }
537 }
538
539
540 }
541
542 function add() {
543 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
544 return $this->testFilter();
545 }
546
547 # print_r($_REQUEST);
548
549 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
550 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
551 $title = db_escape_string($this->link, $_REQUEST["title"]);
552
553 db_query($this->link, "BEGIN");
554
555 /* create base filter */
556
557 $result = db_query($this->link, "INSERT INTO ttrss_filters2
558 (owner_uid, match_any_rule, enabled, title) VALUES
559 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title')");
560
561 $result = db_query($this->link, "SELECT MAX(id) AS id FROM ttrss_filters2
562 WHERE owner_uid = ".$_SESSION["uid"]);
563
564 $filter_id = db_fetch_result($result, 0, "id");
565
566 $this->saveRulesAndActions($filter_id);
567
568 db_query($this->link, "COMMIT");
569 }
570
571 function index() {
572
573 $sort = db_escape_string($this->link, $_REQUEST["sort"]);
574
575 if (!$sort || $sort == "undefined") {
576 $sort = "reg_exp";
577 }
578
579 $filter_search = db_escape_string($this->link, $_REQUEST["search"]);
580
581 if (array_key_exists("search", $_REQUEST)) {
582 $_SESSION["prefs_filter_search"] = $filter_search;
583 } else {
584 $filter_search = $_SESSION["prefs_filter_search"];
585 }
586
587 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
588 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
589 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
590
591 $filter_search = db_escape_string($this->link, $_REQUEST["search"]);
592
593 if (array_key_exists("search", $_REQUEST)) {
594 $_SESSION["prefs_filter_search"] = $filter_search;
595 } else {
596 $filter_search = $_SESSION["prefs_filter_search"];
597 }
598
599 print "<div style='float : right; padding-right : 4px;'>
600 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
601 value=\"$filter_search\">
602 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
603 __('Search')."</button>
604 </div>";
605
606 print "<div dojoType=\"dijit.form.DropDownButton\">".
607 "<span>" . __('Select')."</span>";
608 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
609 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
610 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
611 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
612 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
613 print "</div></div>";
614
615 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
616 __('Create filter')."</button> ";
617
618 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
619 __('Combine')."</button> ";
620
621 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
622 __('Edit')."</button> ";
623
624 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
625 __('Remove')."</button> ";
626
627 if (defined('_ENABLE_FEED_DEBUGGING')) {
628 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
629 __('Rescore articles')."</button> ";
630 }
631
632 print "</div>"; # toolbar
633 print "</div>"; # toolbar-frame
634 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
635
636 print "<div id=\"filterlistLoading\">
637 <img src='images/indicator_tiny.gif'>".
638 __("Loading, please wait...")."</div>";
639
640 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
641 url=\"backend.php?op=pref-filters&method=getfiltertree\">
642 </div>
643 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
644 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
645 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
646 </div>
647 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
648 model=\"filterModel\" openOnClick=\"true\">
649 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
650 Element.hide(\"filterlistLoading\");
651 </script>
652 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
653 var id = String(item.id);
654 var bare_id = id.substr(id.indexOf(':')+1);
655
656 if (id.match('FILTER:')) {
657 editFilter(bare_id);
658 }
659 </script>
660
661 </div>";
662
663 print "</div>"; #pane
664
665 global $pluginhost;
666 $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
667 "hook_prefs_tab", "prefFilters");
668
669 print "</div>"; #container
670
671 }
672
673 function newfilter() {
674
675 print "<form name='filter_new_form' id='filter_new_form'>";
676
677 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
678 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
679 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
680
681 print "<div class=\"dlgSec\">".__("Caption")."</div>";
682
683 print "<input dojoType=\"dijit.form.TextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
684
685 print "<div class=\"dlgSec\">".__("Match")."</div>";
686
687 print "<div dojoType=\"dijit.Toolbar\">";
688
689 print "<div dojoType=\"dijit.form.DropDownButton\">".
690 "<span>" . __('Select')."</span>";
691 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
692 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
693 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
694 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
695 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
696 print "</div></div>";
697
698 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
699 __('Add')."</button> ";
700
701 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
702 __('Delete')."</button> ";
703
704 print "</div>";
705
706 print "<ul id='filterDlg_Matches'>";
707 # print "<li>No rules</li>";
708 print "</ul>";
709
710 print "</div>";
711
712 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
713
714 print "<div dojoType=\"dijit.Toolbar\">";
715
716 print "<div dojoType=\"dijit.form.DropDownButton\">".
717 "<span>" . __('Select')."</span>";
718 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
719 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
720 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
721 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
722 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
723 print "</div></div>";
724
725 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
726 __('Add')."</button> ";
727
728 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
729 __('Delete')."</button> ";
730
731 print "</div>";
732
733 print "<ul id='filterDlg_Actions'>";
734 # print "<li>No actions</li>";
735 print "</ul>";
736
737 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
738 print "<div class=\"dlgSecCont\">"; */
739
740 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
741 <label for=\"enabled\">".__('Enabled')."</label>";
742
743 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
744 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
745
746 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
747 <label for=\"inverse\">".__('Inverse matching')."</label>";
748
749 // print "</div>";
750
751 print "<div class=\"dlgButtons\">";
752
753 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
754 __('Test')."</button> ";
755
756 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
757 __('Create')."</button> ";
758
759 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
760 __('Cancel')."</button>";
761
762 print "</div>";
763
764 }
765
766 function newrule() {
767 $rule = json_decode($_REQUEST["rule"], true);
768
769 if ($rule) {
770 $reg_exp = htmlspecialchars($rule["reg_exp"]);
771 $filter_type = $rule["filter_type"];
772 $feed_id = $rule["feed_id"];
773 $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
774 } else {
775 $reg_exp = "";
776 $filter_type = 1;
777 $feed_id = 0;
778 $inverse_checked = "";
779 }
780
781 if (strpos($feed_id, "CAT:") === 0) {
782 $feed_id = substr($feed_id, 4);
783 $cat_filter = true;
784 } else {
785 $cat_filter = false;
786 }
787
788
789 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
790
791 $result = db_query($this->link, "SELECT id,description
792 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
793
794 $filter_types = array();
795
796 while ($line = db_fetch_assoc($result)) {
797 $filter_types[$line["id"]] = __($line["description"]);
798 }
799
800 print "<div class=\"dlgSec\">".__("Match")."</div>";
801
802 print "<div class=\"dlgSecCont\">";
803
804 print "<input dojoType=\"dijit.form.ValidationTextBox\"
805 required=\"true\" id=\"filterDlg_regExp\"
806 style=\"font-size : 16px; width : 20em;\"
807 name=\"reg_exp\" value=\"$reg_exp\"/>";
808
809 print "<hr/>";
810 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
811 name=\"inverse\" $inverse_checked/>";
812 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
813
814 print "<hr/>" . __("on field") . " ";
815 print_select_hash("filter_type", $filter_type, $filter_types,
816 'dojoType="dijit.form.Select"');
817
818 print "<hr/>";
819
820 print __("in") . " ";
821
822 print "<span id='filterDlg_feeds'>";
823 print_feed_select($this->link, "feed_id",
824 $cat_filter ? "CAT:$feed_id" : $feed_id,
825 'dojoType="dijit.form.FilteringSelect"');
826 print "</span>";
827
828 print "</div>";
829
830 print "<div class=\"dlgButtons\">";
831
832 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
833 ($rule ? __("Save rule") : __('Add rule'))."</button> ";
834
835 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
836 __('Cancel')."</button>";
837
838 print "</div>";
839
840 print "</form>";
841 }
842
843 function newaction() {
844 $action = json_decode($_REQUEST["action"], true);
845
846 if ($action) {
847 $action_param = db_escape_string($this->link, $action["action_param"]);
848 $action_id = (int)$action["action_id"];
849 } else {
850 $action_param = "";
851 $action_id = 0;
852 }
853
854 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
855
856 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
857
858 print "<div class=\"dlgSecCont\">";
859
860 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
861 onchange=\"filterDlgCheckAction(this)\">";
862
863 $result = db_query($this->link, "SELECT id,description FROM ttrss_filter_actions
864 ORDER BY name");
865
866 while ($line = db_fetch_assoc($result)) {
867 $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
868 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
869 }
870
871 print "</select>";
872
873 $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
874 "" : "display : none";
875
876 $param_hidden = ($action_id == 4 || $action_id == 6) ?
877 "" : "display : none";
878
879 $label_param_hidden = ($action_id == 7) ? "" : "display : none";
880
881 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
882 print " " . __("with parameters:") . " ";
883 print "<input dojoType=\"dijit.form.TextBox\"
884 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
885 name=\"action_param\" value=\"$action_param\">";
886
887 print_label_select($this->link, "action_param_label", $action_param,
888 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
889 dojoType=\"dijit.form.Select\"");
890
891 print "</span>";
892
893 print "&nbsp;"; // tiny layout hack
894
895 print "</div>";
896
897 print "<div class=\"dlgButtons\">";
898
899 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
900 ($action ? __("Save action") : __('Add action'))."</button> ";
901
902 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
903 __('Cancel')."</button>";
904
905 print "</div>";
906
907 print "</form>";
908 }
909
910 private function getFilterName($id) {
911
912 $result = db_query($this->link,
913 "SELECT title FROM ttrss_filters2 WHERE id = '$id'");
914
915 $title = db_fetch_result($result, 0, "title");
916
917 if ($title) {
918 return array($title);
919 } else {
920
921 $result = db_query($this->link,
922 "SELECT * FROM ttrss_filters2_rules WHERE filter_id = '$id' ORDER BY id
923 LIMIT 3");
924
925 $titles = array();
926 $count = 0;
927
928 while ($line = db_fetch_assoc($result)) {
929
930 if (sql_bool_to_bool($line["cat_filter"])) {
931 unset($line["cat_filter"]);
932 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
933 unset($line["cat_id"]);
934 }
935
936 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
937
938 if ($count < 2) {
939 array_push($titles, $this->getRuleName($line));
940 } else {
941 array_push($titles, "...");
942 break;
943 }
944 ++$count;
945 }
946
947 $result = db_query($this->link,
948 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 3");
949
950 $actions = array();
951 $count = 0;
952
953 while ($line = db_fetch_assoc($result)) {
954 if ($count < 2) {
955 array_push($actions, $this->getActionName($line));
956 } else {
957 array_push($actions, "...");
958 break;
959 }
960 ++$count;
961 }
962
963 return array(join(", ", $titles), join(", ", $actions));
964
965 }
966 }
967
968 function join() {
969 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
970
971 if (count($ids) > 1) {
972 $base_id = array_shift($ids);
973 $ids_str = join(",", $ids);
974
975 db_query($this->link, "BEGIN");
976 db_query($this->link, "UPDATE ttrss_filters2_rules
977 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
978 db_query($this->link, "UPDATE ttrss_filters2_actions
979 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
980
981 db_query($this->link, "DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
982 db_query($this->link, "UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
983
984 db_query($this->link, "COMMIT");
985
986 $this->optimizeFilter($base_id);
987
988 }
989 }
990
991 private function optimizeFilter($id) {
992 db_query($this->link, "BEGIN");
993 $result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
994 WHERE filter_id = '$id'");
995
996 $tmp = array();
997 $dupe_ids = array();
998
999 while ($line = db_fetch_assoc($result)) {
1000 $id = $line["id"];
1001 unset($line["id"]);
1002
1003 if (array_search($line, $tmp) === false) {
1004 array_push($tmp, $line);
1005 } else {
1006 array_push($dupe_ids, $id);
1007 }
1008 }
1009
1010 if (count($dupe_ids) > 0) {
1011 $ids_str = join(",", $dupe_ids);
1012 db_query($this->link, "DELETE FROM ttrss_filters2_actions
1013 WHERE id IN ($ids_str)");
1014 }
1015
1016 $result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
1017 WHERE filter_id = '$id'");
1018
1019 $tmp = array();
1020 $dupe_ids = array();
1021
1022 while ($line = db_fetch_assoc($result)) {
1023 $id = $line["id"];
1024 unset($line["id"]);
1025
1026 if (array_search($line, $tmp) === false) {
1027 array_push($tmp, $line);
1028 } else {
1029 array_push($dupe_ids, $id);
1030 }
1031 }
1032
1033 if (count($dupe_ids) > 0) {
1034 $ids_str = join(",", $dupe_ids);
1035 db_query($this->link, "DELETE FROM ttrss_filters2_rules
1036 WHERE id IN ($ids_str)");
1037 }
1038
1039 db_query($this->link, "COMMIT");
1040 }
1041 }
1042 ?>