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