]>
git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
cfc20e4192fa7f38c7150a5034e8f67604d21387
2 class Pref_Filters
extends Handler_Protected
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule",
6 "newaction", "savefilterorder");
8 return array_search($method, $csrf_ignored) !== false;
11 function filtersortreset() {
12 $this->dbh
->query("UPDATE ttrss_filters2
13 SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
17 function savefilterorder() {
18 $data = json_decode($_POST['payload'], true);
20 #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
21 #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
23 if (!is_array($data['items']))
24 $data['items'] = json_decode($data['items'], true);
28 if (is_array($data) && is_array($data['items'])) {
29 foreach ($data['items'][0]['items'] as $item) {
30 $filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
34 $this->dbh
->query("UPDATE ttrss_filters2 SET
35 order_id = $index WHERE id = '$filter_id' AND
36 owner_uid = " .$_SESSION["uid"]);
47 function testFilter() {
50 $filter["enabled"] = true;
51 $filter["match_any_rule"] = sql_bool_to_bool(
52 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"])));
53 $filter["inverse"] = sql_bool_to_bool(
54 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"])));
56 $filter["rules"] = array();
58 $result = $this->dbh
->query("SELECT id,name FROM ttrss_filter_types");
60 $filter_types = array();
61 while ($line = $this->dbh
->fetch_assoc($result)) {
62 $filter_types[$line["id"]] = $line["name"];
66 foreach ($_REQUEST["rule"] AS $r) {
67 $rule = json_decode($r, true);
69 if ($rule && $rctr < 5) {
70 $rule["type"] = $filter_types[$rule["filter_type"]];
71 unset($rule["filter_type"]);
73 if (strpos($rule["feed_id"], "CAT:") === 0) {
74 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
75 unset($rule["feed_id"]);
78 array_push($filter["rules"], $rule);
86 $qfh_ret = queryFeedHeadlines(-4, 30, "", false, false, false,
87 "date_entered DESC", 0, $_SESSION["uid"], $filter);
89 $result = $qfh_ret[0];
93 print __("Articles matching this filter:");
95 print "<div class=\"filterTestHolder\">";
96 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
98 while ($line = $this->dbh
->fetch_assoc($result)) {
99 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
101 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
102 $line = $p->hook_query_headlines($line, 100);
105 $content_preview = $line["content_preview"];
107 if ($line["feed_title"])
108 $feed_title = $line["feed_title"];
112 print "<td width='5%' align='center'><input
113 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
114 disabled=\"1\" type=\"checkbox\"></td>";
117 print $line["title"];
119 print "<b>" . $feed_title . "</b>";
121 print "<span class=\"insensitive\">" . $content_preview . "</span>";
122 print " " . mb_substr($line["date_entered"], 0, 16);
130 print "<tr><td align='center'>" .
131 __("No recent articles matching this filter have been found.");
133 print "</td></tr><tr><td class='insensitive' align='center'>";
135 print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
141 print "</table></div>";
143 print "<div style='text-align : center'>";
144 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
145 __('Close this window')."</button>";
150 private function getfilterrules_concise($filter_id) {
151 $result = $this->dbh
->query("SELECT reg_exp,
156 ttrss_filter_types.description AS field
158 ttrss_filters2_rules, ttrss_filter_types
160 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id");
164 while ($line = $this->dbh
->fetch_assoc($result)) {
166 $where = sql_bool_to_bool($line["cat_filter"]) ?
167 getCategoryTitle($line["cat_id"]) : getFeedTitle($line["feed_id"]);
169 # $where = $line["cat_id"] . "/" . $line["feed_id"];
171 $inverse = sql_bool_to_bool($line["inverse"]) ?
"inverse" : "";
173 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
174 strip_tags($line["reg_exp"]),
177 sql_bool_to_bool($line["inverse"]) ?
__("(inverse)") : "") . "</span>";
183 function getfiltertree() {
185 $root['id'] = 'root';
186 $root['name'] = __('Filters');
187 $root['items'] = array();
189 $filter_search = $_SESSION["prefs_filter_search"];
191 $result = $this->dbh
->query("SELECT *,
192 (SELECT action_param FROM ttrss_filters2_actions
193 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
194 (SELECT action_id FROM ttrss_filters2_actions
195 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
196 (SELECT description FROM ttrss_filter_actions
197 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
198 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
199 (SELECT reg_exp FROM ttrss_filters2_rules
200 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
201 FROM ttrss_filters2 WHERE
202 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
206 $folder['items'] = array();
208 while ($line = $this->dbh
->fetch_assoc($result)) {
210 $name = $this->getFilterName($line["id"]);
213 if ($filter_search) {
214 $rules_result = $this->dbh
->query(
215 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
217 while ($rule_line = $this->dbh
->fetch_assoc($rules_result)) {
218 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
225 if ($line['action_id'] == 7) {
226 $label_result = $this->dbh
->query("SELECT fg_color, bg_color
227 FROM ttrss_labels2 WHERE caption = '".$this->dbh
->escape_string($line['action_param'])."' AND
228 owner_uid = " . $_SESSION["uid"]);
230 if ($this->dbh
->num_rows($label_result) > 0) {
231 $fg_color = $this->dbh
->fetch_result($label_result, 0, "fg_color");
232 $bg_color = $this->dbh
->fetch_result($label_result, 0, "bg_color");
234 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
239 $filter['id'] = 'FILTER:' . $line['id'];
240 $filter['bare_id'] = $line['id'];
241 $filter['name'] = $name[0];
242 $filter['param'] = $name[1];
243 $filter['checkbox'] = false;
244 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
245 $filter['rules'] = $this->getfilterrules_concise($line['id']);
247 if (!$filter_search ||
$match_ok) {
248 array_push($folder['items'], $filter);
252 /* if (count($folder['items']) > 0) {
253 array_push($root['items'], $folder);
256 $root['items'] = $folder['items'];
259 $fl['identifier'] = 'id';
260 $fl['label'] = 'name';
261 $fl['items'] = array($root);
263 print json_encode($fl);
269 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
271 $result = $this->dbh
->query(
272 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
274 $enabled = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "enabled"));
275 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
276 $inverse = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "inverse"));
277 $title = htmlspecialchars($this->dbh
->fetch_result($result, 0, "title"));
279 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
281 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
282 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
283 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
284 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
286 print "<div class=\"dlgSec\">".__("Caption")."</div>";
288 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
292 print "<div class=\"dlgSec\">".__("Match")."</div>";
294 print "<div dojoType=\"dijit.Toolbar\">";
296 print "<div dojoType=\"dijit.form.DropDownButton\">".
297 "<span>" . __('Select')."</span>";
298 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
299 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
300 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
301 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
302 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
303 print "</div></div>";
305 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
306 __('Add')."</button> ";
308 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
309 __('Delete')."</button> ";
313 print "<ul id='filterDlg_Matches'>";
315 $rules_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
316 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
318 while ($line = $this->dbh
->fetch_assoc($rules_result)) {
319 if (sql_bool_to_bool($line["cat_filter"])) {
320 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
323 unset($line["cat_filter"]);
324 unset($line["cat_id"]);
325 unset($line["filter_id"]);
327 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
329 $data = htmlspecialchars(json_encode($line));
331 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
332 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
333 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
340 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
342 print "<div dojoType=\"dijit.Toolbar\">";
344 print "<div dojoType=\"dijit.form.DropDownButton\">".
345 "<span>" . __('Select')."</span>";
346 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
347 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
348 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
349 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
350 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
351 print "</div></div>";
353 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
354 __('Add')."</button> ";
356 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
357 __('Delete')."</button> ";
361 print "<ul id='filterDlg_Actions'>";
363 $actions_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
364 WHERE filter_id = '$filter_id' ORDER BY id");
366 while ($line = $this->dbh
->fetch_assoc($actions_result)) {
367 $line["action_param_label"] = $line["action_param"];
369 unset($line["filter_id"]);
372 $data = htmlspecialchars(json_encode($line));
374 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
375 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
376 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
384 $checked = "checked=\"1\"";
389 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
390 <label for=\"enabled\">".__('Enabled')."</label>";
392 if ($match_any_rule) {
393 $checked = "checked=\"1\"";
398 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
399 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
402 $checked = "checked=\"1\"";
407 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
408 <label for=\"inverse\">".__('Inverse matching')."</label>";
412 print "<div class=\"dlgButtons\">";
414 print "<div style=\"float : left\">";
415 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
416 __('Remove')."</button>";
419 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
420 __('Test')."</button> ";
422 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
423 __('Save')."</button> ";
425 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
426 __('Cancel')."</button>";
431 private function getRuleName($rule) {
432 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
434 $feed_id = $rule["feed_id"];
436 if (strpos($feed_id, "CAT:") === 0) {
437 $feed_id = (int) substr($feed_id, 4);
438 $feed = getCategoryTitle($feed_id);
440 $feed_id = (int) $feed_id;
442 if ($rule["feed_id"])
443 $feed = getFeedTitle((int)$rule["feed_id"]);
445 $feed = __("All feeds");
448 $result = $this->dbh
->query("SELECT description FROM ttrss_filter_types
449 WHERE id = ".(int)$rule["filter_type"]);
450 $filter_type = $this->dbh
->fetch_result($result, 0, "description");
452 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
454 return "<span class='filterRule $inverse'>" .
455 T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
456 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
459 function printRuleName() {
460 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
463 private function getActionName($action) {
464 $result = $this->dbh
->query("SELECT description FROM
465 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
467 $title = __($this->dbh
->fetch_result($result, 0, "description"));
469 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
470 $action["action_id"] == 7)
471 $title .= ": " . $action["action_param"];
476 function printActionName() {
477 print $this->getActionName(json_decode($_REQUEST["action"], true));
480 function editSave() {
481 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
482 return $this->testFilter();
485 # print_r($_REQUEST);
487 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
488 $enabled = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["enabled"]));
489 $match_any_rule = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"]));
490 $inverse = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"]));
491 $title = $this->dbh
->escape_string($_REQUEST["title"]);
493 $this->dbh
->query("UPDATE ttrss_filters2 SET enabled = $enabled,
494 match_any_rule = $match_any_rule,
497 WHERE id = '$filter_id'
498 AND owner_uid = ". $_SESSION["uid"]);
500 $this->saveRulesAndActions($filter_id);
506 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
508 foreach ($ids as $id) {
509 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
513 private function saveRulesAndActions($filter_id) {
515 $this->dbh
->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
516 $this->dbh
->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
524 foreach ($_REQUEST["rule"] as $rule) {
525 $rule = json_decode($rule, true);
528 if (array_search($rule, $rules) === false) {
529 array_push($rules, $rule);
533 foreach ($_REQUEST["action"] as $action) {
534 $action = json_decode($action, true);
535 unset($action["id"]);
537 if (array_search($action, $actions) === false) {
538 array_push($actions, $action);
542 foreach ($rules as $rule) {
545 $reg_exp = strip_tags($this->dbh
->escape_string(trim($rule["reg_exp"])));
546 $inverse = isset($rule["inverse"]) ?
"true" : "false";
548 $filter_type = (int) $this->dbh
->escape_string(trim($rule["filter_type"]));
549 $feed_id = $this->dbh
->escape_string(trim($rule["feed_id"]));
551 if (strpos($feed_id, "CAT:") === 0) {
553 $cat_filter = bool_to_sql_bool(true);
554 $cat_id = (int) substr($feed_id, 4);
557 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
559 $cat_filter = bool_to_sql_bool(false);
560 $feed_id = (int) $feed_id;
563 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
566 $query = "INSERT INTO ttrss_filters2_rules
567 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
568 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
570 $this->dbh
->query($query);
574 foreach ($actions as $action) {
577 $action_id = (int) $this->dbh
->escape_string($action["action_id"]);
578 $action_param = $this->dbh
->escape_string($action["action_param"]);
579 $action_param_label = $this->dbh
->escape_string($action["action_param_label"]);
581 if ($action_id == 7) {
582 $action_param = $action_param_label;
585 if ($action_id == 6) {
586 $action_param = (int) str_replace("+", "", $action_param);
589 $query = "INSERT INTO ttrss_filters2_actions
590 (filter_id, action_id, action_param) VALUES
591 ('$filter_id', '$action_id', '$action_param')";
593 $this->dbh
->query($query);
602 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
603 return $this->testFilter();
606 # print_r($_REQUEST);
608 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
609 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
610 $title = $this->dbh
->escape_string($_REQUEST["title"]);
611 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
613 $this->dbh
->query("BEGIN");
615 /* create base filter */
617 $result = $this->dbh
->query("INSERT INTO ttrss_filters2
618 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
619 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
621 $result = $this->dbh
->query("SELECT MAX(id) AS id FROM ttrss_filters2
622 WHERE owner_uid = ".$_SESSION["uid"]);
624 $filter_id = $this->dbh
->fetch_result($result, 0, "id");
626 $this->saveRulesAndActions($filter_id);
628 $this->dbh
->query("COMMIT");
633 $sort = $this->dbh
->escape_string($_REQUEST["sort"]);
635 if (!$sort ||
$sort == "undefined") {
639 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
641 if (array_key_exists("search", $_REQUEST)) {
642 $_SESSION["prefs_filter_search"] = $filter_search;
644 $filter_search = $_SESSION["prefs_filter_search"];
647 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
648 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
649 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
651 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
653 if (array_key_exists("search", $_REQUEST)) {
654 $_SESSION["prefs_filter_search"] = $filter_search;
656 $filter_search = $_SESSION["prefs_filter_search"];
659 print "<div style='float : right; padding-right : 4px;'>
660 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
661 value=\"$filter_search\">
662 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
663 __('Search')."</button>
666 print "<div dojoType=\"dijit.form.DropDownButton\">".
667 "<span>" . __('Select')."</span>";
668 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
669 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
670 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
671 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
672 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
673 print "</div></div>";
675 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
676 __('Create filter')."</button> ";
678 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
679 __('Combine')."</button> ";
681 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
682 __('Edit')."</button> ";
684 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
685 __('Reset sort order')."</button> ";
688 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
689 __('Remove')."</button> ";
691 if (defined('_ENABLE_FEED_DEBUGGING')) {
692 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
693 __('Rescore articles')."</button> ";
696 print "</div>"; # toolbar
697 print "</div>"; # toolbar-frame
698 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
700 print "<div id=\"filterlistLoading\">
701 <img src='images/indicator_tiny.gif'>".
702 __("Loading, please wait...")."</div>";
704 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
705 url=\"backend.php?op=pref-filters&method=getfiltertree\">
707 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
708 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
709 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
711 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
712 dndController=\"dijit.tree.dndSource\"
713 betweenThreshold=\"5\"
714 model=\"filterModel\" openOnClick=\"true\">
715 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
716 Element.hide(\"filterlistLoading\");
718 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
719 var id = String(item.id);
720 var bare_id = id.substr(id.indexOf(':')+1);
722 if (id.match('FILTER:')) {
729 print "</div>"; #pane
731 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
732 "hook_prefs_tab", "prefFilters");
734 print "</div>"; #container
738 function newfilter() {
740 print "<form name='filter_new_form' id='filter_new_form'>";
742 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
743 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
744 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
746 print "<div class=\"dlgSec\">".__("Caption")."</div>";
748 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
750 print "<div class=\"dlgSec\">".__("Match")."</div>";
752 print "<div dojoType=\"dijit.Toolbar\">";
754 print "<div dojoType=\"dijit.form.DropDownButton\">".
755 "<span>" . __('Select')."</span>";
756 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
757 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
758 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
759 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
760 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
761 print "</div></div>";
763 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
764 __('Add')."</button> ";
766 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
767 __('Delete')."</button> ";
771 print "<ul id='filterDlg_Matches'>";
772 # print "<li>No rules</li>";
777 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
779 print "<div dojoType=\"dijit.Toolbar\">";
781 print "<div dojoType=\"dijit.form.DropDownButton\">".
782 "<span>" . __('Select')."</span>";
783 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
784 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
785 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
786 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
787 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
788 print "</div></div>";
790 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
791 __('Add')."</button> ";
793 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
794 __('Delete')."</button> ";
798 print "<ul id='filterDlg_Actions'>";
799 # print "<li>No actions</li>";
802 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
803 print "<div class=\"dlgSecCont\">"; */
805 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
806 <label for=\"enabled\">".__('Enabled')."</label>";
808 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
809 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
811 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
812 <label for=\"inverse\">".__('Inverse matching')."</label>";
816 print "<div class=\"dlgButtons\">";
818 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
819 __('Test')."</button> ";
821 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
822 __('Create')."</button> ";
824 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
825 __('Cancel')."</button>";
832 $rule = json_decode($_REQUEST["rule"], true);
835 $reg_exp = htmlspecialchars($rule["reg_exp"]);
836 $filter_type = $rule["filter_type"];
837 $feed_id = $rule["feed_id"];
838 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
843 $inverse_checked = "";
846 if (strpos($feed_id, "CAT:") === 0) {
847 $feed_id = substr($feed_id, 4);
854 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
856 $result = $this->dbh
->query("SELECT id,description
857 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
859 $filter_types = array();
861 while ($line = $this->dbh
->fetch_assoc($result)) {
862 $filter_types[$line["id"]] = __($line["description"]);
865 print "<div class=\"dlgSec\">".__("Match")."</div>";
867 print "<div class=\"dlgSecCont\">";
869 print "<input dojoType=\"dijit.form.ValidationTextBox\"
870 required=\"true\" id=\"filterDlg_regExp\"
871 style=\"font-size : 16px; width : 20em;\"
872 name=\"reg_exp\" value=\"$reg_exp\"/>";
875 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
876 name=\"inverse\" $inverse_checked/>";
877 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
879 print "<hr/>" . __("on field") . " ";
880 print_select_hash("filter_type", $filter_type, $filter_types,
881 'dojoType="dijit.form.Select"');
885 print __("in") . " ";
887 print "<span id='filterDlg_feeds'>";
888 print_feed_select("feed_id",
889 $cat_filter ?
"CAT:$feed_id" : $feed_id,
890 'dojoType="dijit.form.FilteringSelect"');
895 print "<div class=\"dlgButtons\">";
897 print "<div style=\"float : left\">
898 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
902 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
903 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
905 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
906 __('Cancel')."</button>";
913 function newaction() {
914 $action = json_decode($_REQUEST["action"], true);
917 $action_param = $this->dbh
->escape_string($action["action_param"]);
918 $action_id = (int)$action["action_id"];
924 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
926 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
928 print "<div class=\"dlgSecCont\">";
930 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
931 onchange=\"filterDlgCheckAction(this)\">";
933 $result = $this->dbh
->query("SELECT id,description FROM ttrss_filter_actions
936 while ($line = $this->dbh
->fetch_assoc($result)) {
937 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
938 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
943 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6) ?
944 "" : "display : none";
946 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
947 "" : "display : none";
949 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
951 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
952 print " " . __("with parameters:") . " ";
953 print "<input dojoType=\"dijit.form.TextBox\"
954 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
955 name=\"action_param\" value=\"$action_param\">";
957 print_label_select("action_param_label", $action_param,
958 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
959 dojoType=\"dijit.form.Select\"");
963 print " "; // tiny layout hack
967 print "<div class=\"dlgButtons\">";
969 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
970 ($action ?
__("Save action") : __('Add action'))."</button> ";
972 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
973 __('Cancel')."</button>";
980 private function getFilterName($id) {
982 $result = $this->dbh
->query(
983 "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
984 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
985 ON (r.filter_id = f.id)
986 LEFT JOIN ttrss_filters2_actions AS a
987 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
989 $title = $this->dbh
->fetch_result($result, 0, "title");
990 $num_rules = $this->dbh
->fetch_result($result, 0, "num_rules");
991 $num_actions = $this->dbh
->fetch_result($result, 0, "num_actions");
993 if (!$title) $title = __("[No caption]");
995 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
997 $result = $this->dbh
->query(
998 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
1002 if ($this->dbh
->num_rows($result) > 0) {
1003 $line = $this->dbh
->fetch_assoc($result);
1004 $actions = $this->getActionName($line);
1009 if ($num_actions > 0)
1010 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
1012 return array($title, $actions);
1016 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
1018 if (count($ids) > 1) {
1019 $base_id = array_shift($ids);
1020 $ids_str = join(",", $ids);
1022 $this->dbh
->query("BEGIN");
1023 $this->dbh
->query("UPDATE ttrss_filters2_rules
1024 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1025 $this->dbh
->query("UPDATE ttrss_filters2_actions
1026 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1028 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1029 $this->dbh
->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1031 $this->dbh
->query("COMMIT");
1033 $this->optimizeFilter($base_id);
1038 private function optimizeFilter($id) {
1039 $this->dbh
->query("BEGIN");
1040 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
1041 WHERE filter_id = '$id'");
1044 $dupe_ids = array();
1046 while ($line = $this->dbh
->fetch_assoc($result)) {
1050 if (array_search($line, $tmp) === false) {
1051 array_push($tmp, $line);
1053 array_push($dupe_ids, $id);
1057 if (count($dupe_ids) > 0) {
1058 $ids_str = join(",", $dupe_ids);
1059 $this->dbh
->query("DELETE FROM ttrss_filters2_actions
1060 WHERE id IN ($ids_str)");
1063 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
1064 WHERE filter_id = '$id'");
1067 $dupe_ids = array();
1069 while ($line = $this->dbh
->fetch_assoc($result)) {
1073 if (array_search($line, $tmp) === false) {
1074 array_push($tmp, $line);
1076 array_push($dupe_ids, $id);
1080 if (count($dupe_ids) > 0) {
1081 $ids_str = join(",", $dupe_ids);
1082 $this->dbh
->query("DELETE FROM ttrss_filters2_rules
1083 WHERE id IN ($ids_str)");
1086 $this->dbh
->query("COMMIT");