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