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