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