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