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