]>
git.wh0rd.org - tt-rss.git/blob - classes/pref_filters.php
2 class Pref_Filters
extends Protected_Handler
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getfiltertree", "edit");
7 return array_search($method, $csrf_ignored) !== false;
10 function filter_test($filter_type, $reg_exp,
11 $action_id, $action_param, $filter_param, $inverse, $feed_id) {
13 $result = db_query($this->link
, "SELECT name FROM ttrss_filter_types WHERE
14 id = " . $filter_type);
15 $type_name = db_fetch_result($result, 0, "name");
17 $result = db_query($this->link
, "SELECT name FROM ttrss_filter_actions WHERE
19 $action_name = db_fetch_result($result, 0, "name");
21 $filter["reg_exp"] = $reg_exp;
22 $filter["action"] = $action_name;
23 $filter["type"] = $type_name;
24 $filter["action_param"] = $action_param;
25 $filter["filter_param"] = $filter_param;
26 $filter["inverse"] = $inverse;
28 $filters[$type_name] = array($filter);
35 $feed_title = getFeedTitle($this->link
, $feed);
37 $qfh_ret = queryFeedHeadlines($this->link
, $feed,
38 30, "", false, false, false,
39 false, "date_entered DESC", 0, $_SESSION["uid"], $filter);
41 $result = $qfh_ret[0];
46 print __("Articles matching this filter:");
48 print "<div class=\"inactiveFeedHolder\">";
49 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
51 while ($line = db_fetch_assoc($result)) {
53 $entry_timestamp = strtotime($line["updated"]);
54 $entry_tags = get_article_tags($this->link
, $line["id"], $_SESSION["uid"]);
56 $content_preview = truncate_string(
57 strip_tags($line["content_preview"]), 100, '...');
59 if ($line["feed_title"])
60 $feed_title = $line["feed_title"];
64 print "<td width='5%' align='center'><input
65 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
66 disabled=\"1\" type=\"checkbox\"></td>";
71 print "<b>" . $feed_title . "</b>";
73 print "<span class=\"insensitive\">" . $content_preview . "</span>";
74 print " " . mb_substr($line["date_entered"], 0, 16);
82 print "<tr><td align='center'>" .
83 __("No articles matching this filter has been found.") . "</td></tr>";
91 function getfiltertree() {
94 $root['name'] = __('Filters');
95 $root['items'] = array();
97 $result = db_query($this->link
, "SELECT
98 ttrss_filters.id AS id,reg_exp,
99 ttrss_filter_types.name AS filter_type_name,
100 ttrss_filter_types.description AS filter_type_descr,
107 ttrss_filter_actions.description AS action_description,
108 ttrss_feeds.title AS feed_title,
109 ttrss_filter_actions.name AS action_name,
110 ttrss_filters.action_param AS action_param
112 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
113 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
115 filter_type = ttrss_filter_types.id AND
116 ttrss_filter_actions.id = action_id AND
117 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
118 ORDER by action_description, reg_exp");
121 $cur_action_description = "";
123 if (db_num_rows($result) > 0) {
125 while ($line = db_fetch_assoc($result)) {
126 if ($cur_action_description != $line['action_description']) {
129 array_push($root['items'], $cat);
132 $cat['id'] = 'ACTION:' . $line['action_id'];
133 $cat['name'] = $line['action_description'];
134 $cat['items'] = array();
136 $cur_action_description = $line['action_description'];
139 if (array_search($line["action_name"],
140 array("score", "tag", "label")) === false) {
142 $line["action_param"] = '';
144 if ($line['action_name'] == 'label') {
146 $tmp_result = db_query($this->link
, "SELECT fg_color, bg_color
147 FROM ttrss_labels2 WHERE caption = '".
148 db_escape_string($line["action_param"])."' AND
149 owner_uid = " . $_SESSION["uid"]);
151 if (db_num_rows($tmp_result) != 0) {
152 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
153 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
155 $tmp = "<span class=\"labelColorIndicator\" style='color : $fg_color; background-color : $bg_color'>α</span> " . $line['action_param'];
157 $line['action_param'] = $tmp;
163 $filter['id'] = 'FILTER:' . $line['id'];
164 $filter['bare_id'] = $line['id'];
165 $filter['name'] = $line['reg_exp'];
166 $filter['type'] = $line['filter_type'];
167 $filter['enabled'] = sql_bool_to_bool($line['enabled']);
168 $filter['param'] = $line['action_param'];
169 $filter['inverse'] = sql_bool_to_bool($line['inverse']);
170 $filter['checkbox'] = false;
172 if ($line['feed_id'])
173 $filter['feed'] = $line['feed_title'];
175 array_push($cat['items'], $filter);
178 array_push($root['items'], $cat);
182 $fl['identifier'] = 'id';
183 $fl['label'] = 'name';
184 $fl['items'] = array($root);
186 print json_encode($fl);
192 $filter_id = db_escape_string($_REQUEST["id"]);
194 $result = db_query($this->link
,
195 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
197 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
198 $filter_type = db_fetch_result($result, 0, "filter_type");
199 $feed_id = db_fetch_result($result, 0, "feed_id");
200 $action_id = db_fetch_result($result, 0, "action_id");
201 $action_param = db_fetch_result($result, 0, "action_param");
202 $filter_param = db_fetch_result($result, 0, "filter_param");
204 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
205 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
207 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
209 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
210 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
211 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
212 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
214 $result = db_query($this->link
, "SELECT id,description
215 FROM ttrss_filter_types ORDER BY description");
217 $filter_types = array();
219 while ($line = db_fetch_assoc($result)) {
220 //array_push($filter_types, $line["description"]);
221 $filter_types[$line["id"]] = __($line["description"]);
224 print "<div class=\"dlgSec\">".__("Match")."</div>";
226 print "<div class=\"dlgSecCont\">";
228 if ($filter_type != 5) {
229 $date_ops_invisible = 'style="display : none"';
232 print "<span id=\"filterDlg_dateModBox\" $date_ops_invisible>";
233 print __("Date") . " ";
235 $filter_params = array(
236 "before" => __("before"),
237 "after" => __("after"));
239 print_select_hash("filter_date_modifier", $filter_param,
240 $filter_params, 'dojoType="dijit.form.Select"');
242 print " </span>";
244 print "<input dojoType=\"dijit.form.ValidationTextBox\"
246 name=\"reg_exp\" style=\"font-size : 16px;\" value=\"$reg_exp\">";
248 print "<span id=\"filterDlg_dateChkBox\" $date_ops_invisible>";
249 print " <button dojoType=\"dijit.form.Button\" onclick=\"return filterDlgCheckDate()\">".
250 __('Check it')."</button>";
253 print "<hr/> " . __("on field") . " ";
254 print_select_hash("filter_type", $filter_type, $filter_types,
255 'onchange="filterDlgCheckType(this)" dojoType="dijit.form.Select"');
259 print __("in") . " ";
260 print_feed_select($this->link
, "feed_id", $feed_id,
261 'dojoType="dijit.form.FilteringSelect"');
265 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
267 print "<div class=\"dlgSecCont\">";
269 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
270 onchange=\"filterDlgCheckAction(this)\">";
272 $result = db_query($this->link
, "SELECT id,description FROM ttrss_filter_actions
275 while ($line = db_fetch_assoc($result)) {
276 $is_sel = ($line["id"] == $action_id) ?
"selected=\"1\"" : "";
277 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
282 $param_hidden = ($action_id == 4 ||
$action_id == 6 ||
$action_id == 7) ?
"" : "display : none";
284 print "<span id=\"filterDlg_paramBox\" style=\"$param_hidden\">";
285 print " " . __("with parameters:") . " ";
287 $param_int_hidden = ($action_id != 7) ?
"" : "display : none";
289 print "<input style=\"$param_int_hidden\"
290 dojoType=\"dijit.form.TextBox\" id=\"filterDlg_actionParam\"
291 name=\"action_param\" value=\"$action_param\">";
293 $param_int_hidden = ($action_id == 7) ?
"" : "display : none";
295 print_label_select($this->link
, "action_param_label", $action_param,
296 "style=\"$param_int_hidden\"" .
297 'id="filterDlg_actionParamLabel" dojoType="dijit.form.Select"');
301 print " "; // tiny layout hack
305 print "<div class=\"dlgSec\">".__("Options")."</div>";
306 print "<div class=\"dlgSecCont\">";
308 print "<div style=\"line-height : 100%\">";
311 $checked = "checked=\"1\"";
316 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
317 <label for=\"enabled\">".__('Enabled')."</label><hr/>";
320 $checked = "checked=\"1\"";
325 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
326 <label for=\"inverse\">".__('Inverse match')."</label>";
331 print "<div class=\"dlgButtons\">";
333 print "<div style=\"float : left\">";
334 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
335 __('Remove')."</button>";
338 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
339 __('Test')."</button> ";
341 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
342 __('Save')."</button> ";
344 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
345 __('Cancel')."</button>";
350 function editSave() {
354 if ($memcache) $memcache->flush();
356 $savemode = db_escape_string($_REQUEST["savemode"]);
357 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
358 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
359 $filter_id = db_escape_string($_REQUEST["id"]);
360 $feed_id = db_escape_string($_REQUEST["feed_id"]);
361 $action_id = db_escape_string($_REQUEST["action_id"]);
362 $action_param = db_escape_string($_REQUEST["action_param"]);
363 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
364 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
365 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
367 # for the time being, no other filters use params anyway...
368 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
373 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
376 /* When processing 'assign label' filters, action_param_label dropbox
377 * overrides action_param */
379 if ($action_id == 7) {
380 $action_param = $action_param_label;
383 if ($action_id == 6) {
384 $action_param = (int) str_replace("+", "", $action_param);
387 if ($savemode != "test") {
388 $result = db_query($this->link
, "UPDATE ttrss_filters SET
389 reg_exp = '$reg_exp',
391 action_id = '$action_id',
392 filter_type = '$filter_type',
395 action_param = '$action_param',
396 filter_param = '$filter_param'
397 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
400 $this->filter_test($filter_type, $reg_exp,
401 $action_id, $action_param, $filter_param, sql_bool_to_bool($inverse),
402 (int) $_REQUEST["feed_id"]);
404 print "<div align='center'>";
405 print "<button dojoType=\"dijit.form.Button\"
406 onclick=\"return dijit.byId('filterTestDlg').hide()\">".
407 __('Close this window')."</button>";
415 if ($memcache) $memcache->flush();
417 $ids = split(",", db_escape_string($_REQUEST["ids"]));
419 foreach ($ids as $id) {
420 db_query($this->link
, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
426 if ($memcache) $memcache->flush();
428 $savemode = db_escape_string($_REQUEST["savemode"]);
429 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
430 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
431 $feed_id = db_escape_string($_REQUEST["feed_id"]);
432 $action_id = db_escape_string($_REQUEST["action_id"]);
433 $action_param = db_escape_string($_REQUEST["action_param"]);
434 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
435 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
437 # for the time being, no other filters use params anyway...
438 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
440 if (!$regexp) return;
445 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
448 /* When processing 'assign label' filters, action_param_label dropbox
449 * overrides action_param */
451 if ($action_id == 7) {
452 $action_param = $action_param_label;
455 if ($action_id == 6) {
456 $action_param = (int) str_replace("+", "", $action_param);
459 if ($savemode != "test") {
460 $result = db_query($this->link
,
461 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
462 action_id, action_param, inverse, filter_param)
464 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
465 $feed_id, '$action_id', '$action_param', $inverse,
468 if (db_affected_rows($this->link
, $result) != 0) {
469 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
474 $this->filter_test($filter_type, $regexp,
475 $action_id, $action_param, $filter_param, sql_bool_to_bool($inverse),
476 (int) $_REQUEST["feed_id"]);
478 print "<div align='center'>";
479 print "<button dojoType=\"dijit.form.Button\"
480 onclick=\"return dijit.byId('filterTestDlg').hide()\">".
481 __('Close this window')."</button>";
489 $sort = db_escape_string($_REQUEST["sort"]);
491 if (!$sort ||
$sort == "undefined") {
495 $result = db_query($this->link
, "SELECT id,description
496 FROM ttrss_filter_types ORDER BY description");
498 $filter_types = array();
500 while ($line = db_fetch_assoc($result)) {
501 //array_push($filter_types, $line["description"]);
502 $filter_types[$line["id"]] = $line["description"];
506 $filter_search = db_escape_string($_REQUEST["search"]);
508 if (array_key_exists("search", $_REQUEST)) {
509 $_SESSION["prefs_filter_search"] = $filter_search;
511 $filter_search = $_SESSION["prefs_filter_search"];
514 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
515 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
516 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
518 print "<div dojoType=\"dijit.form.DropDownButton\">".
519 "<span>" . __('Select')."</span>";
520 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
521 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
522 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
523 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
524 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
525 print "</div></div>";
527 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
528 __('Create filter')."</button> ";
530 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
531 __('Edit')."</button> ";
533 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
534 __('Remove')."</button> ";
536 if (defined('_ENABLE_FEED_DEBUGGING')) {
537 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
538 __('Rescore articles')."</button> ";
541 print "</div>"; # toolbar
542 print "</div>"; # toolbar-frame
543 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
545 print "<div id=\"filterlistLoading\">
546 <img src='images/indicator_tiny.gif'>".
547 __("Loading, please wait...")."</div>";
549 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
550 url=\"backend.php?op=pref-filters&method=getfiltertree\">
552 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
553 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
554 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
556 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
557 model=\"filterModel\" openOnClick=\"true\">
558 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
559 Element.hide(\"filterlistLoading\");
561 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
562 var id = String(item.id);
563 var bare_id = id.substr(id.indexOf(':')+1);
565 if (id.match('FILTER:')) {
572 print "</div>"; #pane
573 print "</div>"; #container