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