]> git.wh0rd.org - tt-rss.git/blame - classes/pref/filters.php
Revert "filters: support matching on multiple feeds/categories"
[tt-rss.git] / classes / pref / filters.php
CommitLineData
8e17d663 1<?php
369dbc19 2class Pref_Filters extends Handler_Protected {
8e17d663 3
8484ce22 4 function csrf_ignore($method) {
6aff7845 5 $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule",
a86ba0a6 6 "newaction", "savefilterorder");
8484ce22
AD
7
8 return array_search($method, $csrf_ignored) !== false;
9 }
10
a86ba0a6 11 function filtersortreset() {
d9c85e0f 12 $this->dbh->query("UPDATE ttrss_filters2
a86ba0a6
AD
13 SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
14 return;
15 }
16
17 function savefilterorder() {
18 $data = json_decode($_POST['payload'], true);
19
20 #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
21 #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
22
23 if (!is_array($data['items']))
24 $data['items'] = json_decode($data['items'], true);
25
26 $index = 0;
27
28 if (is_array($data) && is_array($data['items'])) {
29 foreach ($data['items'][0]['items'] as $item) {
30 $filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
31
32 if ($filter_id > 0) {
33
d9c85e0f 34 $this->dbh->query("UPDATE ttrss_filters2 SET
a86ba0a6
AD
35 order_id = $index WHERE id = '$filter_id' AND
36 owner_uid = " .$_SESSION["uid"]);
37
38 ++$index;
39 }
40 }
41 }
42
43 return;
44 }
45
a007c18d 46 function testFilterDo() {
a007c18d 47 $offset = (int) db_escape_string($_REQUEST["offset"]);
261226a3 48 $limit = (int) db_escape_string($_REQUEST["limit"]);
a86ba0a6 49
4e02f582 50 $filter = array();
8e17d663 51
4e02f582
AD
52 $filter["enabled"] = true;
53 $filter["match_any_rule"] = sql_bool_to_bool(
d9c85e0f 54 checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"])));
a3a896a1 55 $filter["inverse"] = sql_bool_to_bool(
d9c85e0f 56 checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"])));
a3a896a1 57
4e02f582 58 $filter["rules"] = array();
c939dfc3 59 $filter["actions"] = array("dummy-action");
8e17d663 60
d9c85e0f 61 $result = $this->dbh->query("SELECT id,name FROM ttrss_filter_types");
8e17d663 62
4e02f582 63 $filter_types = array();
d9c85e0f 64 while ($line = $this->dbh->fetch_assoc($result)) {
4e02f582
AD
65 $filter_types[$line["id"]] = $line["name"];
66 }
8e17d663 67
c939dfc3
AD
68 $scope_qparts = array();
69
4e02f582
AD
70 $rctr = 0;
71 foreach ($_REQUEST["rule"] AS $r) {
72 $rule = json_decode($r, true);
8e17d663 73
4e02f582
AD
74 if ($rule && $rctr < 5) {
75 $rule["type"] = $filter_types[$rule["filter_type"]];
76 unset($rule["filter_type"]);
8e17d663 77
f5d174bd
AD
78 if (strpos($rule["feed_id"], "CAT:") === 0) {
79 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
80 unset($rule["feed_id"]);
81 }
0bf7e007 82
f5d174bd
AD
83 if (isset($rule["feed_id"]) && $rule['feed_id'] > 0) {
84 array_push($scope_qparts, "feed_id = " . $rule["feed_id"]);
85 } else if (isset($rule["cat_id"])) {
86 array_push($scope_qparts, "cat_id = " . $rule["cat_id"]);
87 } else {
88 array_push($scope_qparts, "true");
89 }
c939dfc3 90
4e02f582 91 array_push($filter["rules"], $rule);
8e17d663 92
4e02f582
AD
93 ++$rctr;
94 } else {
95 break;
96 }
97 }
8e17d663 98
314956d4 99 $glue = $filter['match_any_rule'] ? " OR " : " AND ";
c939dfc3 100 $scope_qpart = join($glue, $scope_qparts);
74175e6a 101
9638ec51
AD
102 if (!$scope_qpart) $scope_qpart = "true";
103
a007c18d
AD
104 $rv = array();
105
106 //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
8e17d663 107
c939dfc3
AD
108 $result = db_query("SELECT ttrss_entries.id,
109 ttrss_entries.title,
110 ttrss_feeds.id AS feed_id,
4f9a3c0e 111 ttrss_feeds.title AS feed_title,
c939dfc3
AD
112 ttrss_feed_categories.id AS cat_id,
113 content,
261226a3 114 date_entered,
c939dfc3
AD
115 link,
116 author,
117 tag_cache
118 FROM
119 ttrss_entries, ttrss_user_entries
120 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
121 LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id)
122 WHERE
123 ref_id = ttrss_entries.id AND
124 ($scope_qpart) AND
125 ttrss_user_entries.owner_uid = " . $_SESSION["uid"] . "
261226a3 126 ORDER BY date_entered DESC LIMIT $limit OFFSET $offset");
56fbb82c 127
c939dfc3 128 while ($line = db_fetch_assoc($result)) {
56fbb82c 129
e6c886bf 130 $rc = RSSUtils::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
7b55001e 131 $line['author'], explode(",", $line['tag_cache']));
56fbb82c 132
c939dfc3 133 if (count($rc) > 0) {
56fbb82c 134
f48f292d 135 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '&hellip;');
56fbb82c 136
c939dfc3
AD
137 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
138 $line = $p->hook_query_headlines($line, 100);
139 }
56fbb82c 140
c939dfc3 141 $content_preview = $line["content_preview"];
8e17d663 142
dbeef0ca
AD
143 $tmp = "<tr style='margin-top : 5px'>";
144
145 #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
146 # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>";
c939dfc3 147
f48f292d
AD
148 $id = $line['id'];
149 $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."'
150 src='images/information.png' onclick='openArticlePopup($id)'></td><td>";
c939dfc3 151
9a35ed67 152 /*foreach ($filter['rules'] as $rule) {
c939dfc3 153 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
5cb17306 154
c939dfc3
AD
155 $line["title"] = preg_replace("/($reg_exp)/i",
156 "<span class=\"highlight\">$1</span>", $line["title"]);
5cb17306 157
c939dfc3
AD
158 $content_preview = preg_replace("/($reg_exp)/i",
159 "<span class=\"highlight\">$1</span>", $content_preview);
9a35ed67 160 }*/
261226a3 161
f48f292d
AD
162 $tmp .= "<strong>" . $line["title"] . "</strong><br/>";
163 $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16);
164 $tmp .= "<div class='insensitive'>" . $content_preview . "</div>";
261226a3 165 $tmp .= "</td></tr>";
5cb17306 166
261226a3 167 array_push($rv, $tmp);
5cb17306 168
261226a3
AD
169 /*array_push($rv, array("title" => $line["title"],
170 "content" => $content_preview,
171 "date" => $line["date_entered"],
172 "feed" => $line["feed_title"])); */
c939dfc3 173
c939dfc3
AD
174 }
175 }
176
a007c18d
AD
177 //$offset += $limit;
178 //}
c939dfc3 179
a007c18d 180 /*if ($found == 0) {
c939dfc3
AD
181 print "<tr><td align='center'>" .
182 __("No recent articles matching this filter have been found.");
a007c18d
AD
183 }*/
184
185 print json_encode($rv);
186 }
187
188 function testFilter() {
189
190 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
191
192 //print __("Articles matching this filter:");
193
261226a3 194 print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'>&nbsp;<span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
8e17d663 195
a007c18d
AD
196 print "<br/><div class=\"filterTestHolder\">";
197 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
4e02f582
AD
198 print "</table></div>";
199
200 print "<div style='text-align : center'>";
201 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
202 __('Close this window')."</button>";
8e17d663
AD
203 print "</div>";
204
4e02f582
AD
205 }
206
50e04efd
AD
207 private function getfilterrules_concise($filter_id) {
208 $result = $this->dbh->query("SELECT reg_exp,
209 inverse,
210 feed_id,
211 cat_id,
212 cat_filter,
213 ttrss_filter_types.description AS field
214 FROM
215 ttrss_filters2_rules, ttrss_filter_types
216 WHERE
4f100ece
AD
217 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id
218 ORDER BY reg_exp");
50e04efd
AD
219
220 $rv = "";
221
222 while ($line = $this->dbh->fetch_assoc($result)) {
223
f5d174bd
AD
224 $where = sql_bool_to_bool($line["cat_filter"]) ?
225 Feeds::getCategoryTitle($line["cat_id"]) :
226 ($line["feed_id"] ?
227 Feeds::getFeedTitle($line["feed_id"]) : __("All feeds"));
50e04efd
AD
228
229# $where = $line["cat_id"] . "/" . $line["feed_id"];
230
231 $inverse = sql_bool_to_bool($line["inverse"]) ? "inverse" : "";
232
233 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
154f14d0 234 htmlspecialchars($line["reg_exp"]),
50e04efd
AD
235 $line["field"],
236 $where,
237 sql_bool_to_bool($line["inverse"]) ? __("(inverse)") : "") . "</span>";
238 }
239
240 return $rv;
241 }
8e17d663
AD
242
243 function getfiltertree() {
244 $root = array();
245 $root['id'] = 'root';
246 $root['name'] = __('Filters');
247 $root['items'] = array();
248
69c6e94d
AD
249 $filter_search = $_SESSION["prefs_filter_search"];
250
d9c85e0f 251 $result = $this->dbh->query("SELECT *,
84d9750e
AD
252 (SELECT action_param FROM ttrss_filters2_actions
253 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
6aff7845
AD
254 (SELECT action_id FROM ttrss_filters2_actions
255 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
37f78940
AD
256 (SELECT description FROM ttrss_filter_actions
257 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
258 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
6aff7845
AD
259 (SELECT reg_exp FROM ttrss_filters2_rules
260 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
261 FROM ttrss_filters2 WHERE
f0419320 262 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
8e17d663 263
69c6e94d 264
37f78940
AD
265 $folder = array();
266 $folder['items'] = array();
267
d9c85e0f 268 while ($line = $this->dbh->fetch_assoc($result)) {
8e17d663 269
6aff7845 270 $name = $this->getFilterName($line["id"]);
8e17d663 271
69c6e94d
AD
272 $match_ok = false;
273 if ($filter_search) {
d9c85e0f 274 $rules_result = $this->dbh->query(
69c6e94d
AD
275 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
276
d9c85e0f 277 while ($rule_line = $this->dbh->fetch_assoc($rules_result)) {
69c6e94d
AD
278 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
279 $match_ok = true;
280 break;
281 }
282 }
283 }
284
84d9750e 285 if ($line['action_id'] == 7) {
d9c85e0f
AD
286 $label_result = $this->dbh->query("SELECT fg_color, bg_color
287 FROM ttrss_labels2 WHERE caption = '".$this->dbh->escape_string($line['action_param'])."' AND
84d9750e
AD
288 owner_uid = " . $_SESSION["uid"]);
289
d9c85e0f
AD
290 if ($this->dbh->num_rows($label_result) > 0) {
291 $fg_color = $this->dbh->fetch_result($label_result, 0, "fg_color");
292 $bg_color = $this->dbh->fetch_result($label_result, 0, "bg_color");
84d9750e 293
da46d21e 294 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
84d9750e
AD
295 }
296 }
297
6aff7845
AD
298 $filter = array();
299 $filter['id'] = 'FILTER:' . $line['id'];
300 $filter['bare_id'] = $line['id'];
301 $filter['name'] = $name[0];
302 $filter['param'] = $name[1];
303 $filter['checkbox'] = false;
c2ee5846 304 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
50e04efd 305 $filter['rules'] = $this->getfilterrules_concise($line['id']);
8e17d663 306
69c6e94d
AD
307 if (!$filter_search || $match_ok) {
308 array_push($folder['items'], $filter);
309 }
8e17d663
AD
310 }
311
da46d21e 312 /* if (count($folder['items']) > 0) {
e8b70387 313 array_push($root['items'], $folder);
da46d21e
AD
314 } */
315
316 $root['items'] = $folder['items'];
e8b70387 317
8e17d663
AD
318 $fl = array();
319 $fl['identifier'] = 'id';
320 $fl['label'] = 'name';
321 $fl['items'] = array($root);
322
323 print json_encode($fl);
324 return;
325 }
326
327 function edit() {
328
d9c85e0f 329 $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
8e17d663 330
d9c85e0f 331 $result = $this->dbh->query(
6aff7845 332 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
8e17d663 333
d9c85e0f
AD
334 $enabled = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "enabled"));
335 $match_any_rule = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "match_any_rule"));
336 $inverse = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "inverse"));
337 $title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
8e17d663
AD
338
339 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
340
328118d1
AD
341 print_hidden("op", "pref-filters");
342 print_hidden("id", "$filter_id");
343 print_hidden("method", "editSave");
344 print_hidden("csrf_token", $_SESSION['csrf_token']);
8e17d663 345
18ad09af
AD
346 print "<div class=\"dlgSec\">".__("Caption")."</div>";
347
442bca27 348 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
18ad09af
AD
349
350 print "</div>";
351
8e17d663
AD
352 print "<div class=\"dlgSec\">".__("Match")."</div>";
353
6aff7845 354 print "<div dojoType=\"dijit.Toolbar\">";
8e17d663 355
6aff7845
AD
356 print "<div dojoType=\"dijit.form.DropDownButton\">".
357 "<span>" . __('Select')."</span>";
358 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
359 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
360 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
361 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
362 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
363 print "</div></div>";
8e17d663 364
6aff7845
AD
365 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
366 __('Add')."</button> ";
8e17d663 367
6aff7845
AD
368 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
369 __('Delete')."</button> ";
8e17d663 370
6aff7845 371 print "</div>";
8e17d663 372
6aff7845 373 print "<ul id='filterDlg_Matches'>";
8e17d663 374
d9c85e0f 375 $rules_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
56b2a409 376 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
8e17d663 377
d9c85e0f 378 while ($line = $this->dbh->fetch_assoc($rules_result)) {
f5d174bd
AD
379 if (sql_bool_to_bool($line["cat_filter"])) {
380 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
381 }
382
383 unset($line["cat_filter"]);
384 unset($line["cat_id"]);
385 unset($line["filter_id"]);
386 unset($line["id"]);
387 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
388
389 $data = htmlspecialchars(json_encode($line));
8e17d663 390
04e41840 391 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
6aff7845
AD
392 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
393 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
394 }
ba975b2e 395
6aff7845 396 print "</ul>";
ba975b2e 397
8e17d663
AD
398 print "</div>";
399
6aff7845 400 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
8e17d663 401
6aff7845 402 print "<div dojoType=\"dijit.Toolbar\">";
8e17d663 403
6aff7845
AD
404 print "<div dojoType=\"dijit.form.DropDownButton\">".
405 "<span>" . __('Select')."</span>";
406 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
407 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
408 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
409 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
410 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
411 print "</div></div>";
8e17d663 412
6aff7845
AD
413 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
414 __('Add')."</button> ";
8e17d663 415
6aff7845
AD
416 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
417 __('Delete')."</button> ";
8e17d663 418
6aff7845 419 print "</div>";
8e17d663 420
6aff7845 421 print "<ul id='filterDlg_Actions'>";
8e17d663 422
d9c85e0f 423 $actions_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
6aff7845 424 WHERE filter_id = '$filter_id' ORDER BY id");
8e17d663 425
d9c85e0f 426 while ($line = $this->dbh->fetch_assoc($actions_result)) {
6aff7845 427 $line["action_param_label"] = $line["action_param"];
4e02f582
AD
428
429 unset($line["filter_id"]);
430 unset($line["id"]);
431
6aff7845 432 $data = htmlspecialchars(json_encode($line));
8e17d663 433
04e41840 434 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
6aff7845
AD
435 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
436 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
437 }
8e17d663 438
6aff7845 439 print "</ul>";
8e17d663
AD
440
441 print "</div>";
442
8e17d663
AD
443 if ($enabled) {
444 $checked = "checked=\"1\"";
445 } else {
446 $checked = "";
447 }
448
449 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
6aff7845 450 <label for=\"enabled\">".__('Enabled')."</label>";
8e17d663 451
6aff7845 452 if ($match_any_rule) {
8e17d663
AD
453 $checked = "checked=\"1\"";
454 } else {
455 $checked = "";
456 }
457
6aff7845
AD
458 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
459 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
8e17d663 460
a3a896a1
AD
461 if ($inverse) {
462 $checked = "checked=\"1\"";
463 } else {
464 $checked = "";
465 }
466
467 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
468 <label for=\"inverse\">".__('Inverse matching')."</label>";
469
6aff7845 470 print "<p/>";
8e17d663
AD
471
472 print "<div class=\"dlgButtons\">";
473
474 print "<div style=\"float : left\">";
475 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
476 __('Remove')."</button>";
477 print "</div>";
478
4e02f582
AD
479 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
480 __('Test')."</button> ";
8e17d663
AD
481
482 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
483 __('Save')."</button> ";
484
485 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
486 __('Cancel')."</button>";
487
488 print "</div>";
489 }
490
6aff7845
AD
491 private function getRuleName($rule) {
492 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
8e17d663 493
f5d174bd 494 $feed_id = $rule["feed_id"];
8e17d663 495
f5d174bd
AD
496 if (strpos($feed_id, "CAT:") === 0) {
497 $feed_id = (int) substr($feed_id, 4);
498 $feed = Feeds::getCategoryTitle($feed_id);
499 } else {
500 $feed_id = (int) $feed_id;
0bf7e007 501
f5d174bd
AD
502 if ($rule["feed_id"])
503 $feed = Feeds::getFeedTitle((int)$rule["feed_id"]);
504 else
505 $feed = __("All feeds");
506 }
8e17d663 507
d9c85e0f 508 $result = $this->dbh->query("SELECT description FROM ttrss_filter_types
6aff7845 509 WHERE id = ".(int)$rule["filter_type"]);
d9c85e0f 510 $filter_type = $this->dbh->fetch_result($result, 0, "description");
4c9d0490 511
50e04efd
AD
512 $inverse = isset($rule["inverse"]) ? "inverse" : "";
513
514 return "<span class='filterRule $inverse'>" .
154f14d0 515 T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
50e04efd 516 $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>";
6aff7845 517 }
ba975b2e 518
6aff7845
AD
519 function printRuleName() {
520 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
521 }
8e17d663 522
6aff7845 523 private function getActionName($action) {
d9c85e0f 524 $result = $this->dbh->query("SELECT description FROM
6aff7845 525 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
8e17d663 526
d9c85e0f 527 $title = __($this->dbh->fetch_result($result, 0, "description"));
8e17d663 528
6aff7845
AD
529 if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
530 $action["action_id"] == 7)
531 $title .= ": " . $action["action_param"];
8e17d663 532
b8774453
AD
533 if ($action["action_id"] == 9) {
534 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
535
536 $filter_actions = PluginHost::getInstance()->get_filter_actions();
537
538 foreach ($filter_actions as $fclass => $factions) {
539 foreach ($factions as $faction) {
540 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
541 $title .= ": " . $fclass . ": " . $faction["description"];
542 break;
543 }
544 }
545 }
546 }
547
6aff7845
AD
548 return $title;
549 }
8e17d663 550
6aff7845
AD
551 function printActionName() {
552 print $this->getActionName(json_decode($_REQUEST["action"], true));
553 }
554
555 function editSave() {
4e02f582
AD
556 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
557 return $this->testFilter();
558 }
6aff7845
AD
559
560# print_r($_REQUEST);
561
d9c85e0f
AD
562 $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
563 $enabled = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["enabled"]));
564 $match_any_rule = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"]));
565 $inverse = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"]));
566 $title = $this->dbh->escape_string($_REQUEST["title"]);
6aff7845 567
4a80c57c 568 $this->dbh->query("UPDATE ttrss_filters2 SET enabled = $enabled,
a3a896a1 569 match_any_rule = $match_any_rule,
18ad09af
AD
570 inverse = $inverse,
571 title = '$title'
6aff7845
AD
572 WHERE id = '$filter_id'
573 AND owner_uid = ". $_SESSION["uid"]);
574
575 $this->saveRulesAndActions($filter_id);
8e17d663 576
8e17d663
AD
577 }
578
579 function remove() {
580
d9c85e0f 581 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
8e17d663
AD
582
583 foreach ($ids as $id) {
d9c85e0f 584 $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
8e17d663
AD
585 }
586 }
587
6aff7845 588 private function saveRulesAndActions($filter_id) {
8e17d663 589
d9c85e0f
AD
590 $this->dbh->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
591 $this->dbh->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
8e17d663 592
6aff7845
AD
593 if ($filter_id) {
594 /* create rules */
8e17d663 595
6aff7845
AD
596 $rules = array();
597 $actions = array();
8e17d663 598
6aff7845
AD
599 foreach ($_REQUEST["rule"] as $rule) {
600 $rule = json_decode($rule, true);
601 unset($rule["id"]);
8e17d663 602
6aff7845
AD
603 if (array_search($rule, $rules) === false) {
604 array_push($rules, $rule);
605 }
606 }
4c9d0490 607
6aff7845
AD
608 foreach ($_REQUEST["action"] as $action) {
609 $action = json_decode($action, true);
610 unset($action["id"]);
ba975b2e 611
6aff7845
AD
612 if (array_search($action, $actions) === false) {
613 array_push($actions, $action);
614 }
615 }
8e17d663 616
6aff7845
AD
617 foreach ($rules as $rule) {
618 if ($rule) {
8e17d663 619
154f14d0 620 $reg_exp = $this->dbh->escape_string(trim($rule["reg_exp"]), false);
a3a896a1
AD
621 $inverse = isset($rule["inverse"]) ? "true" : "false";
622
d9c85e0f 623 $filter_type = (int) $this->dbh->escape_string(trim($rule["filter_type"]));
f5d174bd 624 $feed_id = $this->dbh->escape_string(trim($rule["feed_id"]));
6aff7845 625
f5d174bd 626 if (strpos($feed_id, "CAT:") === 0) {
5451903c 627
6aff7845
AD
628 $cat_filter = bool_to_sql_bool(true);
629 $cat_id = (int) substr($feed_id, 4);
630 $feed_id = "NULL";
8e17d663 631
6aff7845
AD
632 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
633 } else {
634 $cat_filter = bool_to_sql_bool(false);
635 $feed_id = (int) $feed_id;
636 $cat_id = "NULL";
637
638 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
f5d174bd 639 }
6aff7845
AD
640
641 $query = "INSERT INTO ttrss_filters2_rules
f5d174bd
AD
642 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
643 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
6aff7845 644
d9c85e0f 645 $this->dbh->query($query);
6aff7845 646 }
8e17d663
AD
647 }
648
6aff7845
AD
649 foreach ($actions as $action) {
650 if ($action) {
651
d9c85e0f
AD
652 $action_id = (int) $this->dbh->escape_string($action["action_id"]);
653 $action_param = $this->dbh->escape_string($action["action_param"]);
654 $action_param_label = $this->dbh->escape_string($action["action_param_label"]);
6aff7845
AD
655
656 if ($action_id == 7) {
657 $action_param = $action_param_label;
658 }
8e17d663 659
6aff7845
AD
660 if ($action_id == 6) {
661 $action_param = (int) str_replace("+", "", $action_param);
662 }
8e17d663 663
6aff7845
AD
664 $query = "INSERT INTO ttrss_filters2_actions
665 (filter_id, action_id, action_param) VALUES
666 ('$filter_id', '$action_id', '$action_param')";
8e17d663 667
d9c85e0f 668 $this->dbh->query($query);
6aff7845
AD
669 }
670 }
8e17d663 671 }
6aff7845
AD
672
673
674 }
675
676 function add() {
4e02f582
AD
677 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
678 return $this->testFilter();
679 }
680
6aff7845
AD
681# print_r($_REQUEST);
682
683 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
684 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
d9c85e0f 685 $title = $this->dbh->escape_string($_REQUEST["title"]);
afa1a260 686 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
6aff7845 687
d9c85e0f 688 $this->dbh->query("BEGIN");
6aff7845
AD
689
690 /* create base filter */
691
d9c85e0f 692 $result = $this->dbh->query("INSERT INTO ttrss_filters2
afa1a260
AD
693 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
694 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
6aff7845 695
d9c85e0f 696 $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2
6aff7845
AD
697 WHERE owner_uid = ".$_SESSION["uid"]);
698
d9c85e0f 699 $filter_id = $this->dbh->fetch_result($result, 0, "id");
6aff7845
AD
700
701 $this->saveRulesAndActions($filter_id);
702
d9c85e0f 703 $this->dbh->query("COMMIT");
8e17d663
AD
704 }
705
706 function index() {
707
d9c85e0f 708 $sort = $this->dbh->escape_string($_REQUEST["sort"]);
8e17d663
AD
709
710 if (!$sort || $sort == "undefined") {
711 $sort = "reg_exp";
712 }
713
d9c85e0f 714 $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
8e17d663
AD
715
716 if (array_key_exists("search", $_REQUEST)) {
717 $_SESSION["prefs_filter_search"] = $filter_search;
718 } else {
719 $filter_search = $_SESSION["prefs_filter_search"];
720 }
721
722 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
723 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
724 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
725
d9c85e0f 726 $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
310fa2dd
AD
727
728 if (array_key_exists("search", $_REQUEST)) {
729 $_SESSION["prefs_filter_search"] = $filter_search;
730 } else {
731 $filter_search = $_SESSION["prefs_filter_search"];
732 }
733
734 print "<div style='float : right; padding-right : 4px;'>
735 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
736 value=\"$filter_search\">
737 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
738 __('Search')."</button>
739 </div>";
740
8e17d663
AD
741 print "<div dojoType=\"dijit.form.DropDownButton\">".
742 "<span>" . __('Select')."</span>";
743 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
744 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
745 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
746 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
747 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
748 print "</div></div>";
749
750 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
751 __('Create filter')."</button> ";
752
6aff7845
AD
753 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
754 __('Combine')."</button> ";
755
8e17d663
AD
756 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
757 __('Edit')."</button> ";
758
a86ba0a6
AD
759 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
760 __('Reset sort order')."</button> ";
761
762
8e17d663
AD
763 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
764 __('Remove')."</button> ";
765
766 if (defined('_ENABLE_FEED_DEBUGGING')) {
767 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
768 __('Rescore articles')."</button> ";
769 }
770
771 print "</div>"; # toolbar
772 print "</div>"; # toolbar-frame
773 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
774
775 print "<div id=\"filterlistLoading\">
776 <img src='images/indicator_tiny.gif'>".
777 __("Loading, please wait...")."</div>";
778
a86ba0a6 779 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
8e17d663
AD
780 url=\"backend.php?op=pref-filters&method=getfiltertree\">
781 </div>
782 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
a86ba0a6 783 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
8e17d663
AD
784 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
785 </div>
786 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
a86ba0a6
AD
787 dndController=\"dijit.tree.dndSource\"
788 betweenThreshold=\"5\"
8e17d663
AD
789 model=\"filterModel\" openOnClick=\"true\">
790 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
791 Element.hide(\"filterlistLoading\");
792 </script>
793 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
794 var id = String(item.id);
795 var bare_id = id.substr(id.indexOf(':')+1);
796
797 if (id.match('FILTER:')) {
798 editFilter(bare_id);
799 }
800 </script>
801
802 </div>";
803
804 print "</div>"; #pane
6065f3ad 805
1ffe3391 806 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
6065f3ad
AD
807 "hook_prefs_tab", "prefFilters");
808
8e17d663
AD
809 print "</div>"; #container
810
811 }
6aff7845
AD
812
813 function newfilter() {
814
815 print "<form name='filter_new_form' id='filter_new_form'>";
816
328118d1
AD
817 print_hidden("op", "pref-filters");
818 print_hidden("method", "add");
819 print_hidden("csrf_token", $_SESSION['csrf_token']);
6aff7845 820
18ad09af
AD
821 print "<div class=\"dlgSec\">".__("Caption")."</div>";
822
442bca27 823 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
18ad09af 824
6aff7845
AD
825 print "<div class=\"dlgSec\">".__("Match")."</div>";
826
827 print "<div dojoType=\"dijit.Toolbar\">";
828
829 print "<div dojoType=\"dijit.form.DropDownButton\">".
830 "<span>" . __('Select')."</span>";
831 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
832 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
833 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
834 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
835 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
836 print "</div></div>";
837
838 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
839 __('Add')."</button> ";
840
841 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
842 __('Delete')."</button> ";
843
844 print "</div>";
845
846 print "<ul id='filterDlg_Matches'>";
847# print "<li>No rules</li>";
848 print "</ul>";
849
850 print "</div>";
851
852 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
853
854 print "<div dojoType=\"dijit.Toolbar\">";
855
856 print "<div dojoType=\"dijit.form.DropDownButton\">".
857 "<span>" . __('Select')."</span>";
858 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
859 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
860 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
861 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
862 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
863 print "</div></div>";
864
865 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
866 __('Add')."</button> ";
867
868 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
869 __('Delete')."</button> ";
870
871 print "</div>";
872
873 print "<ul id='filterDlg_Actions'>";
874# print "<li>No actions</li>";
875 print "</ul>";
876
877/* print "<div class=\"dlgSec\">".__("Options")."</div>";
878 print "<div class=\"dlgSecCont\">"; */
879
880 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
881 <label for=\"enabled\">".__('Enabled')."</label>";
882
883 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
884 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
885
a3a896a1
AD
886 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
887 <label for=\"inverse\">".__('Inverse matching')."</label>";
6aff7845
AD
888
889// print "</div>";
890
891 print "<div class=\"dlgButtons\">";
892
4e02f582
AD
893 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
894 __('Test')."</button> ";
6aff7845
AD
895
896 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
897 __('Create')."</button> ";
898
899 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
900 __('Cancel')."</button>";
901
902 print "</div>";
903
904 }
905
906 function newrule() {
907 $rule = json_decode($_REQUEST["rule"], true);
908
909 if ($rule) {
910 $reg_exp = htmlspecialchars($rule["reg_exp"]);
911 $filter_type = $rule["filter_type"];
912 $feed_id = $rule["feed_id"];
a3a896a1 913 $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
6aff7845
AD
914 } else {
915 $reg_exp = "";
916 $filter_type = 1;
f5d174bd 917 $feed_id = 0;
a3a896a1 918 $inverse_checked = "";
6aff7845
AD
919 }
920
f5d174bd 921 if (strpos($feed_id, "CAT:") === 0) {
6aff7845
AD
922 $feed_id = substr($feed_id, 4);
923 $cat_filter = true;
924 } else {
925 $cat_filter = false;
f5d174bd
AD
926 }
927
6aff7845
AD
928
929 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
930
d9c85e0f 931 $result = $this->dbh->query("SELECT id,description
37f78940 932 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
6aff7845
AD
933
934 $filter_types = array();
935
d9c85e0f 936 while ($line = $this->dbh->fetch_assoc($result)) {
6aff7845
AD
937 $filter_types[$line["id"]] = __($line["description"]);
938 }
939
940 print "<div class=\"dlgSec\">".__("Match")."</div>";
941
942 print "<div class=\"dlgSecCont\">";
943
944 print "<input dojoType=\"dijit.form.ValidationTextBox\"
945 required=\"true\" id=\"filterDlg_regExp\"
ff34bac2 946 style=\"font-size : 16px; width : 20em;\"
6aff7845
AD
947 name=\"reg_exp\" value=\"$reg_exp\"/>";
948
ec1f8a3d
AD
949 print "<hr/>";
950 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
951 name=\"inverse\" $inverse_checked/>";
952 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
953
6aff7845
AD
954 print "<hr/>" . __("on field") . " ";
955 print_select_hash("filter_type", $filter_type, $filter_types,
956 'dojoType="dijit.form.Select"');
957
958 print "<hr/>";
959
960 print __("in") . " ";
961
962 print "<span id='filterDlg_feeds'>";
f5d174bd
AD
963 print_feed_select("feed_id",
964 $cat_filter ? "CAT:$feed_id" : $feed_id,
965 'dojoType="dijit.form.FilteringSelect"');
6aff7845
AD
966 print "</span>";
967
968 print "</div>";
969
970 print "<div class=\"dlgButtons\">";
971
e7a40490
AD
972 print "<div style=\"float : left\">
973 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
974 </div>";
975
976
6aff7845
AD
977 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
978 ($rule ? __("Save rule") : __('Add rule'))."</button> ";
979
980 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
981 __('Cancel')."</button>";
982
983 print "</div>";
984
985 print "</form>";
986 }
987
988 function newaction() {
989 $action = json_decode($_REQUEST["action"], true);
990
991 if ($action) {
d9c85e0f 992 $action_param = $this->dbh->escape_string($action["action_param"]);
6aff7845
AD
993 $action_id = (int)$action["action_id"];
994 } else {
995 $action_param = "";
996 $action_id = 0;
997 }
998
999 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
1000
1001 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1002
1003 print "<div class=\"dlgSecCont\">";
1004
1005 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1006 onchange=\"filterDlgCheckAction(this)\">";
1007
d9c85e0f 1008 $result = $this->dbh->query("SELECT id,description FROM ttrss_filter_actions
6aff7845
AD
1009 ORDER BY name");
1010
d9c85e0f 1011 while ($line = $this->dbh->fetch_assoc($result)) {
6aff7845
AD
1012 $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
1013 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1014 }
1015
1016 print "</select>";
1017
b8774453 1018 $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6 || $action_id == 9) ?
6aff7845
AD
1019 "" : "display : none";
1020
1021 $param_hidden = ($action_id == 4 || $action_id == 6) ?
1022 "" : "display : none";
1023
1024 $label_param_hidden = ($action_id == 7) ? "" : "display : none";
b8774453 1025 $plugin_param_hidden = ($action_id == 9) ? "" : "display : none";
6aff7845
AD
1026
1027 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
b8774453
AD
1028 print " ";
1029 //print " " . __("with parameters:") . " ";
6aff7845
AD
1030 print "<input dojoType=\"dijit.form.TextBox\"
1031 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1032 name=\"action_param\" value=\"$action_param\">";
1033
a42c55f0 1034 print_label_select("action_param_label", $action_param,
6aff7845
AD
1035 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1036 dojoType=\"dijit.form.Select\"");
1037
b8774453
AD
1038 $filter_actions = PluginHost::getInstance()->get_filter_actions();
1039 $filter_action_hash = array();
1040
1041 foreach ($filter_actions as $fclass => $factions) {
1042 foreach ($factions as $faction) {
1043
1044 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1045 $fclass . ": " . $faction["description"];
1046 }
b028da0a
AD
1047 }
1048
1049 if (count($filter_action_hash) == 0) {
1050 $filter_plugin_disabled = "disabled";
b8774453 1051
b028da0a
AD
1052 $filter_action_hash["no-data"] = __("No actions available");
1053
1054 } else {
1055 $filter_plugin_disabled = "";
b8774453
AD
1056 }
1057
1058 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
b028da0a 1059 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
b8774453
AD
1060 "action_param_plugin");
1061
6aff7845
AD
1062 print "</span>";
1063
1064 print "&nbsp;"; // tiny layout hack
1065
1066 print "</div>";
1067
1068 print "<div class=\"dlgButtons\">";
1069
1070 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1071 ($action ? __("Save action") : __('Add action'))."</button> ";
1072
1073 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1074 __('Cancel')."</button>";
1075
1076 print "</div>";
1077
1078 print "</form>";
1079 }
1080
1081 private function getFilterName($id) {
18ad09af 1082
d9c85e0f 1083 $result = $this->dbh->query(
e17079dc 1084 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1a293f10
AD
1085 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1086 ON (r.filter_id = f.id)
1087 LEFT JOIN ttrss_filters2_actions AS a
e17079dc 1088 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title, f.match_any_rule");
18ad09af 1089
d9c85e0f
AD
1090 $title = $this->dbh->fetch_result($result, 0, "title");
1091 $num_rules = $this->dbh->fetch_result($result, 0, "num_rules");
1092 $num_actions = $this->dbh->fetch_result($result, 0, "num_actions");
e17079dc 1093 $match_any_rule = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "match_any_rule"));
18ad09af 1094
1a293f10 1095 if (!$title) $title = __("[No caption]");
18ad09af 1096
967f0619 1097 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
6aff7845 1098
e17079dc 1099
d9c85e0f 1100 $result = $this->dbh->query(
1a293f10 1101 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
bcd4d240 1102
1a293f10 1103 $actions = "";
da46d21e 1104
d9c85e0f
AD
1105 if ($this->dbh->num_rows($result) > 0) {
1106 $line = $this->dbh->fetch_assoc($result);
1a293f10 1107 $actions = $this->getActionName($line);
da46d21e 1108
1a293f10 1109 $num_actions -= 1;
6aff7845
AD
1110 }
1111
e17079dc
AD
1112 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1113
1a293f10 1114 if ($num_actions > 0)
967f0619 1115 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
6aff7845 1116
1a293f10 1117 return array($title, $actions);
6aff7845
AD
1118 }
1119
1120 function join() {
d9c85e0f 1121 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
6aff7845
AD
1122
1123 if (count($ids) > 1) {
1124 $base_id = array_shift($ids);
1125 $ids_str = join(",", $ids);
1126
d9c85e0f
AD
1127 $this->dbh->query("BEGIN");
1128 $this->dbh->query("UPDATE ttrss_filters2_rules
6aff7845 1129 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
d9c85e0f 1130 $this->dbh->query("UPDATE ttrss_filters2_actions
6aff7845
AD
1131 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1132
d9c85e0f
AD
1133 $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1134 $this->dbh->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
6aff7845 1135
d9c85e0f 1136 $this->dbh->query("COMMIT");
6aff7845
AD
1137
1138 $this->optimizeFilter($base_id);
1139
1140 }
1141 }
1142
1143 private function optimizeFilter($id) {
d9c85e0f
AD
1144 $this->dbh->query("BEGIN");
1145 $result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
6aff7845
AD
1146 WHERE filter_id = '$id'");
1147
1148 $tmp = array();
1149 $dupe_ids = array();
1150
d9c85e0f 1151 while ($line = $this->dbh->fetch_assoc($result)) {
6aff7845
AD
1152 $id = $line["id"];
1153 unset($line["id"]);
1154
1155 if (array_search($line, $tmp) === false) {
1156 array_push($tmp, $line);
1157 } else {
1158 array_push($dupe_ids, $id);
1159 }
1160 }
1161
1162 if (count($dupe_ids) > 0) {
1163 $ids_str = join(",", $dupe_ids);
d9c85e0f 1164 $this->dbh->query("DELETE FROM ttrss_filters2_actions
6aff7845
AD
1165 WHERE id IN ($ids_str)");
1166 }
1167
d9c85e0f 1168 $result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
6aff7845
AD
1169 WHERE filter_id = '$id'");
1170
1171 $tmp = array();
1172 $dupe_ids = array();
1173
d9c85e0f 1174 while ($line = $this->dbh->fetch_assoc($result)) {
6aff7845
AD
1175 $id = $line["id"];
1176 unset($line["id"]);
1177
1178 if (array_search($line, $tmp) === false) {
1179 array_push($tmp, $line);
1180 } else {
1181 array_push($dupe_ids, $id);
1182 }
1183 }
1184
1185 if (count($dupe_ids) > 0) {
1186 $ids_str = join(",", $dupe_ids);
d9c85e0f 1187 $this->dbh->query("DELETE FROM ttrss_filters2_rules
6aff7845
AD
1188 WHERE id IN ($ids_str)");
1189 }
1190
d9c85e0f 1191 $this->dbh->query("COMMIT");
6aff7845 1192 }
ea79a0e0 1193}