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