]>
git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
43da6d04dd996c5fb229f7eef2189b91b3111896
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];
94 print __("Articles matching this filter:");
96 print "<div class=\"filterTestHolder\">";
97 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
99 while ($line = $this->dbh
->fetch_assoc($result)) {
100 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
102 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
103 $line = $p->hook_query_headlines($line, 100);
106 $entry_timestamp = strtotime($line["updated"]);
107 $entry_tags = get_article_tags($line["id"], $_SESSION["uid"]);
109 $content_preview = $line["content_preview"];
111 if ($line["feed_title"])
112 $feed_title = $line["feed_title"];
116 print "<td width='5%' align='center'><input
117 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
118 disabled=\"1\" type=\"checkbox\"></td>";
121 print $line["title"];
123 print "<b>" . $feed_title . "</b>";
125 print "<span class=\"insensitive\">" . $content_preview . "</span>";
126 print " " . mb_substr($line["date_entered"], 0, 16);
134 print "<tr><td align='center'>" .
135 __("No recent articles matching this filter have been found.");
137 print "</td></tr><tr><td class='insensitive' align='center'>";
139 print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
145 print "</table></div>";
147 print "<div style='text-align : center'>";
148 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
149 __('Close this window')."</button>";
155 function getfiltertree() {
157 $root['id'] = 'root';
158 $root['name'] = __('Filters');
159 $root['items'] = array();
161 $filter_search = $_SESSION["prefs_filter_search"];
163 $result = $this->dbh
->query("SELECT *,
164 (SELECT action_param FROM ttrss_filters2_actions
165 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
166 (SELECT action_id FROM ttrss_filters2_actions
167 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
168 (SELECT description FROM ttrss_filter_actions
169 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
170 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
171 (SELECT reg_exp FROM ttrss_filters2_rules
172 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
173 FROM ttrss_filters2 WHERE
174 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
179 $folder['items'] = array();
181 while ($line = $this->dbh
->fetch_assoc($result)) {
183 /* if ($action_id != $line["action_id"]) {
184 if (count($folder['items']) > 0) {
185 array_push($root['items'], $folder);
189 $folder['id'] = $line["action_id"];
190 $folder['name'] = __($line["action_name"]);
191 $folder['items'] = array();
192 $action_id = $line["action_id"];
195 $name = $this->getFilterName($line["id"]);
198 if ($filter_search) {
199 $rules_result = $this->dbh
->query(
200 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
202 while ($rule_line = $this->dbh
->fetch_assoc($rules_result)) {
203 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
210 if ($line['action_id'] == 7) {
211 $label_result = $this->dbh
->query("SELECT fg_color, bg_color
212 FROM ttrss_labels2 WHERE caption = '".$this->dbh
->escape_string($line['action_param'])."' AND
213 owner_uid = " . $_SESSION["uid"]);
215 if ($this->dbh
->num_rows($label_result) > 0) {
216 $fg_color = $this->dbh
->fetch_result($label_result, 0, "fg_color");
217 $bg_color = $this->dbh
->fetch_result($label_result, 0, "bg_color");
219 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
224 $filter['id'] = 'FILTER:' . $line['id'];
225 $filter['bare_id'] = $line['id'];
226 $filter['name'] = $name[0];
227 $filter['param'] = $name[1];
228 $filter['checkbox'] = false;
229 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
231 if (!$filter_search ||
$match_ok) {
232 array_push($folder['items'], $filter);
236 /* if (count($folder['items']) > 0) {
237 array_push($root['items'], $folder);
240 $root['items'] = $folder['items'];
243 $fl['identifier'] = 'id';
244 $fl['label'] = 'name';
245 $fl['items'] = array($root);
247 print json_encode($fl);
253 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
255 $result = $this->dbh
->query(
256 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
258 $enabled = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "enabled"));
259 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
260 $inverse = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "inverse"));
261 $title = htmlspecialchars($this->dbh
->fetch_result($result, 0, "title"));
263 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
265 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
266 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
267 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
268 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
270 print "<div class=\"dlgSec\">".__("Caption")."</div>";
272 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
276 print "<div class=\"dlgSec\">".__("Match")."</div>";
278 print "<div dojoType=\"dijit.Toolbar\">";
280 print "<div dojoType=\"dijit.form.DropDownButton\">".
281 "<span>" . __('Select')."</span>";
282 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
283 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
284 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
285 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
286 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
287 print "</div></div>";
289 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
290 __('Add')."</button> ";
292 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
293 __('Delete')."</button> ";
297 print "<ul id='filterDlg_Matches'>";
299 $rules_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
300 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
302 while ($line = $this->dbh
->fetch_assoc($rules_result)) {
303 if (sql_bool_to_bool($line["cat_filter"])) {
304 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
307 unset($line["cat_filter"]);
308 unset($line["cat_id"]);
309 unset($line["filter_id"]);
311 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
313 $data = htmlspecialchars(json_encode($line));
315 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
316 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
317 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
324 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
326 print "<div dojoType=\"dijit.Toolbar\">";
328 print "<div dojoType=\"dijit.form.DropDownButton\">".
329 "<span>" . __('Select')."</span>";
330 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
331 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
332 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
333 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
334 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
335 print "</div></div>";
337 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
338 __('Add')."</button> ";
340 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
341 __('Delete')."</button> ";
345 print "<ul id='filterDlg_Actions'>";
347 $actions_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
348 WHERE filter_id = '$filter_id' ORDER BY id");
350 while ($line = $this->dbh
->fetch_assoc($actions_result)) {
351 $line["action_param_label"] = $line["action_param"];
353 unset($line["filter_id"]);
356 $data = htmlspecialchars(json_encode($line));
358 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
359 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
360 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
368 $checked = "checked=\"1\"";
373 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
374 <label for=\"enabled\">".__('Enabled')."</label>";
376 if ($match_any_rule) {
377 $checked = "checked=\"1\"";
382 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
383 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
386 $checked = "checked=\"1\"";
391 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
392 <label for=\"inverse\">".__('Inverse matching')."</label>";
396 print "<div class=\"dlgButtons\">";
398 print "<div style=\"float : left\">";
399 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
400 __('Remove')."</button>";
403 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
404 __('Test')."</button> ";
406 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
407 __('Save')."</button> ";
409 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
410 __('Cancel')."</button>";
415 private function getRuleName($rule) {
416 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
418 $feed_id = $rule["feed_id"];
420 if (strpos($feed_id, "CAT:") === 0) {
421 $feed_id = (int) substr($feed_id, 4);
422 $feed = getCategoryTitle($feed_id);
424 $feed_id = (int) $feed_id;
426 if ($rule["feed_id"])
427 $feed = getFeedTitle((int)$rule["feed_id"]);
429 $feed = __("All feeds");
432 $result = $this->dbh
->query("SELECT description FROM ttrss_filter_types
433 WHERE id = ".(int)$rule["filter_type"]);
434 $filter_type = $this->dbh
->fetch_result($result, 0, "description");
436 return T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
437 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "");
440 function printRuleName() {
441 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
444 private function getActionName($action) {
445 $result = $this->dbh
->query("SELECT description FROM
446 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
448 $title = __($this->dbh
->fetch_result($result, 0, "description"));
450 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
451 $action["action_id"] == 7)
452 $title .= ": " . $action["action_param"];
457 function printActionName() {
458 print $this->getActionName(json_decode($_REQUEST["action"], true));
461 function editSave() {
462 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
463 return $this->testFilter();
466 # print_r($_REQUEST);
468 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
469 $enabled = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["enabled"]));
470 $match_any_rule = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"]));
471 $inverse = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"]));
472 $title = $this->dbh
->escape_string($_REQUEST["title"]);
474 $result = $this->dbh
->query("UPDATE ttrss_filters2 SET enabled = $enabled,
475 match_any_rule = $match_any_rule,
478 WHERE id = '$filter_id'
479 AND owner_uid = ". $_SESSION["uid"]);
481 $this->saveRulesAndActions($filter_id);
487 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
489 foreach ($ids as $id) {
490 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
494 private function saveRulesAndActions($filter_id) {
496 $this->dbh
->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
497 $this->dbh
->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
505 foreach ($_REQUEST["rule"] as $rule) {
506 $rule = json_decode($rule, true);
509 if (array_search($rule, $rules) === false) {
510 array_push($rules, $rule);
514 foreach ($_REQUEST["action"] as $action) {
515 $action = json_decode($action, true);
516 unset($action["id"]);
518 if (array_search($action, $actions) === false) {
519 array_push($actions, $action);
523 foreach ($rules as $rule) {
526 $reg_exp = strip_tags($this->dbh
->escape_string(trim($rule["reg_exp"])));
527 $inverse = isset($rule["inverse"]) ?
"true" : "false";
529 $filter_type = (int) $this->dbh
->escape_string(trim($rule["filter_type"]));
530 $feed_id = $this->dbh
->escape_string(trim($rule["feed_id"]));
532 if (strpos($feed_id, "CAT:") === 0) {
534 $cat_filter = bool_to_sql_bool(true);
535 $cat_id = (int) substr($feed_id, 4);
538 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
540 $cat_filter = bool_to_sql_bool(false);
541 $feed_id = (int) $feed_id;
544 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
547 $query = "INSERT INTO ttrss_filters2_rules
548 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
549 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
551 $this->dbh
->query($query);
555 foreach ($actions as $action) {
558 $action_id = (int) $this->dbh
->escape_string($action["action_id"]);
559 $action_param = $this->dbh
->escape_string($action["action_param"]);
560 $action_param_label = $this->dbh
->escape_string($action["action_param_label"]);
562 if ($action_id == 7) {
563 $action_param = $action_param_label;
566 if ($action_id == 6) {
567 $action_param = (int) str_replace("+", "", $action_param);
570 $query = "INSERT INTO ttrss_filters2_actions
571 (filter_id, action_id, action_param) VALUES
572 ('$filter_id', '$action_id', '$action_param')";
574 $this->dbh
->query($query);
583 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
584 return $this->testFilter();
587 # print_r($_REQUEST);
589 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
590 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
591 $title = $this->dbh
->escape_string($_REQUEST["title"]);
592 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
594 $this->dbh
->query("BEGIN");
596 /* create base filter */
598 $result = $this->dbh
->query("INSERT INTO ttrss_filters2
599 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
600 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
602 $result = $this->dbh
->query("SELECT MAX(id) AS id FROM ttrss_filters2
603 WHERE owner_uid = ".$_SESSION["uid"]);
605 $filter_id = $this->dbh
->fetch_result($result, 0, "id");
607 $this->saveRulesAndActions($filter_id);
609 $this->dbh
->query("COMMIT");
614 $sort = $this->dbh
->escape_string($_REQUEST["sort"]);
616 if (!$sort ||
$sort == "undefined") {
620 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
622 if (array_key_exists("search", $_REQUEST)) {
623 $_SESSION["prefs_filter_search"] = $filter_search;
625 $filter_search = $_SESSION["prefs_filter_search"];
628 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
629 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
630 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
632 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
634 if (array_key_exists("search", $_REQUEST)) {
635 $_SESSION["prefs_filter_search"] = $filter_search;
637 $filter_search = $_SESSION["prefs_filter_search"];
640 print "<div style='float : right; padding-right : 4px;'>
641 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
642 value=\"$filter_search\">
643 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
644 __('Search')."</button>
647 print "<div dojoType=\"dijit.form.DropDownButton\">".
648 "<span>" . __('Select')."</span>";
649 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
650 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
651 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
652 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
653 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
654 print "</div></div>";
656 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
657 __('Create filter')."</button> ";
659 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
660 __('Combine')."</button> ";
662 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
663 __('Edit')."</button> ";
665 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
666 __('Reset sort order')."</button> ";
669 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
670 __('Remove')."</button> ";
672 if (defined('_ENABLE_FEED_DEBUGGING')) {
673 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
674 __('Rescore articles')."</button> ";
677 print "</div>"; # toolbar
678 print "</div>"; # toolbar-frame
679 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
681 print "<div id=\"filterlistLoading\">
682 <img src='images/indicator_tiny.gif'>".
683 __("Loading, please wait...")."</div>";
685 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
686 url=\"backend.php?op=pref-filters&method=getfiltertree\">
688 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
689 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
690 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
692 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
693 dndController=\"dijit.tree.dndSource\"
694 betweenThreshold=\"5\"
695 model=\"filterModel\" openOnClick=\"true\">
696 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
697 Element.hide(\"filterlistLoading\");
699 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
700 var id = String(item.id);
701 var bare_id = id.substr(id.indexOf(':')+1);
703 if (id.match('FILTER:')) {
710 print "</div>"; #pane
712 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
713 "hook_prefs_tab", "prefFilters");
715 print "</div>"; #container
719 function newfilter() {
721 print "<form name='filter_new_form' id='filter_new_form'>";
723 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
724 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
725 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
727 print "<div class=\"dlgSec\">".__("Caption")."</div>";
729 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
731 print "<div class=\"dlgSec\">".__("Match")."</div>";
733 print "<div dojoType=\"dijit.Toolbar\">";
735 print "<div dojoType=\"dijit.form.DropDownButton\">".
736 "<span>" . __('Select')."</span>";
737 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
738 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
739 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
740 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
741 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
742 print "</div></div>";
744 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
745 __('Add')."</button> ";
747 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
748 __('Delete')."</button> ";
752 print "<ul id='filterDlg_Matches'>";
753 # print "<li>No rules</li>";
758 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
760 print "<div dojoType=\"dijit.Toolbar\">";
762 print "<div dojoType=\"dijit.form.DropDownButton\">".
763 "<span>" . __('Select')."</span>";
764 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
765 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
766 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
767 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
768 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
769 print "</div></div>";
771 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
772 __('Add')."</button> ";
774 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
775 __('Delete')."</button> ";
779 print "<ul id='filterDlg_Actions'>";
780 # print "<li>No actions</li>";
783 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
784 print "<div class=\"dlgSecCont\">"; */
786 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
787 <label for=\"enabled\">".__('Enabled')."</label>";
789 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
790 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
792 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
793 <label for=\"inverse\">".__('Inverse matching')."</label>";
797 print "<div class=\"dlgButtons\">";
799 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
800 __('Test')."</button> ";
802 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
803 __('Create')."</button> ";
805 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
806 __('Cancel')."</button>";
813 $rule = json_decode($_REQUEST["rule"], true);
816 $reg_exp = htmlspecialchars($rule["reg_exp"]);
817 $filter_type = $rule["filter_type"];
818 $feed_id = $rule["feed_id"];
819 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
824 $inverse_checked = "";
827 if (strpos($feed_id, "CAT:") === 0) {
828 $feed_id = substr($feed_id, 4);
835 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
837 $result = $this->dbh
->query("SELECT id,description
838 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
840 $filter_types = array();
842 while ($line = $this->dbh
->fetch_assoc($result)) {
843 $filter_types[$line["id"]] = __($line["description"]);
846 print "<div class=\"dlgSec\">".__("Match")."</div>";
848 print "<div class=\"dlgSecCont\">";
850 print "<input dojoType=\"dijit.form.ValidationTextBox\"
851 required=\"true\" id=\"filterDlg_regExp\"
852 style=\"font-size : 16px; width : 20em;\"
853 name=\"reg_exp\" value=\"$reg_exp\"/>";
856 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
857 name=\"inverse\" $inverse_checked/>";
858 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
860 print "<hr/>" . __("on field") . " ";
861 print_select_hash("filter_type", $filter_type, $filter_types,
862 'dojoType="dijit.form.Select"');
866 print __("in") . " ";
868 print "<span id='filterDlg_feeds'>";
869 print_feed_select("feed_id",
870 $cat_filter ?
"CAT:$feed_id" : $feed_id,
871 'dojoType="dijit.form.FilteringSelect"');
876 print "<div class=\"dlgButtons\">";
878 print "<div style=\"float : left\">
879 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
883 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
884 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
886 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
887 __('Cancel')."</button>";
894 function newaction() {
895 $action = json_decode($_REQUEST["action"], true);
898 $action_param = $this->dbh
->escape_string($action["action_param"]);
899 $action_id = (int)$action["action_id"];
905 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
907 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
909 print "<div class=\"dlgSecCont\">";
911 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
912 onchange=\"filterDlgCheckAction(this)\">";
914 $result = $this->dbh
->query("SELECT id,description FROM ttrss_filter_actions
917 while ($line = $this->dbh
->fetch_assoc($result)) {
918 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
919 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
924 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6) ?
925 "" : "display : none";
927 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
928 "" : "display : none";
930 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
932 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
933 print " " . __("with parameters:") . " ";
934 print "<input dojoType=\"dijit.form.TextBox\"
935 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
936 name=\"action_param\" value=\"$action_param\">";
938 print_label_select("action_param_label", $action_param,
939 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
940 dojoType=\"dijit.form.Select\"");
944 print " "; // tiny layout hack
948 print "<div class=\"dlgButtons\">";
950 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
951 ($action ?
__("Save action") : __('Add action'))."</button> ";
953 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
954 __('Cancel')."</button>";
961 private function getFilterName($id) {
963 $result = $this->dbh
->query(
964 "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
965 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
966 ON (r.filter_id = f.id)
967 LEFT JOIN ttrss_filters2_actions AS a
968 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
970 $title = $this->dbh
->fetch_result($result, 0, "title");
971 $num_rules = $this->dbh
->fetch_result($result, 0, "num_rules");
972 $num_actions = $this->dbh
->fetch_result($result, 0, "num_actions");
974 if (!$title) $title = __("[No caption]");
976 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
978 $result = $this->dbh
->query(
979 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
983 if ($this->dbh
->num_rows($result) > 0) {
984 $line = $this->dbh
->fetch_assoc($result);
985 $actions = $this->getActionName($line);
990 if ($num_actions > 0)
991 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
993 return array($title, $actions);
997 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
999 if (count($ids) > 1) {
1000 $base_id = array_shift($ids);
1001 $ids_str = join(",", $ids);
1003 $this->dbh
->query("BEGIN");
1004 $this->dbh
->query("UPDATE ttrss_filters2_rules
1005 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1006 $this->dbh
->query("UPDATE ttrss_filters2_actions
1007 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1009 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1010 $this->dbh
->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1012 $this->dbh
->query("COMMIT");
1014 $this->optimizeFilter($base_id);
1019 private function optimizeFilter($id) {
1020 $this->dbh
->query("BEGIN");
1021 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
1022 WHERE filter_id = '$id'");
1025 $dupe_ids = array();
1027 while ($line = $this->dbh
->fetch_assoc($result)) {
1031 if (array_search($line, $tmp) === false) {
1032 array_push($tmp, $line);
1034 array_push($dupe_ids, $id);
1038 if (count($dupe_ids) > 0) {
1039 $ids_str = join(",", $dupe_ids);
1040 $this->dbh
->query("DELETE FROM ttrss_filters2_actions
1041 WHERE id IN ($ids_str)");
1044 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
1045 WHERE filter_id = '$id'");
1048 $dupe_ids = array();
1050 while ($line = $this->dbh
->fetch_assoc($result)) {
1054 if (array_search($line, $tmp) === false) {
1055 array_push($tmp, $line);
1057 array_push($dupe_ids, $id);
1061 if (count($dupe_ids) > 0) {
1062 $ids_str = join(",", $dupe_ids);
1063 $this->dbh
->query("DELETE FROM ttrss_filters2_rules
1064 WHERE id IN ($ids_str)");
1067 $this->dbh
->query("COMMIT");