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