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