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