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