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