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