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