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