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