]>
git.wh0rd.org - tt-rss.git/blob - modules/pref-filters.php
2 function module_pref_filters($link) {
3 $subop = $_REQUEST["subop"];
4 $quiet = $_REQUEST["quiet"];
6 if ($subop == "getfiltertree") {
9 $root['name'] = __('Filters');
10 $root['items'] = array();
12 $result = db_query($link, "SELECT
13 ttrss_filters.id AS id,reg_exp,
14 ttrss_filter_types.name AS filter_type_name,
15 ttrss_filter_types.description AS filter_type_descr,
22 ttrss_filter_actions.description AS action_description,
23 ttrss_feeds.title AS feed_title,
24 ttrss_filter_actions.name AS action_name,
25 ttrss_filters.action_param AS action_param
27 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
28 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
30 filter_type = ttrss_filter_types.id AND
31 ttrss_filter_actions.id = action_id AND
32 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
33 ORDER by action_description, reg_exp");
36 $cur_action_description = "";
38 while ($line = db_fetch_assoc($result)) {
39 if ($cur_action_description != $line['action_description']) {
42 array_push($root['items'], $cat);
45 $cat['id'] = 'ACTION:' . $line['action_id'];
46 $cat['name'] = $line['action_description'];
47 $cat['items'] = array();
49 $cur_action_description = $line['action_description'];
52 if (array_search($line["action_name"],
53 array("score", "tag", "label")) === false) {
55 $line["action_param"] = '';
57 if ($line['action_name'] == 'label') {
59 $tmp_result = db_query($link, "SELECT fg_color, bg_color
60 FROM ttrss_labels2 WHERE caption = '".
61 db_escape_string($line["action_param"])."' AND
62 owner_uid = " . $_SESSION["uid"]);
64 if (db_num_rows($tmp_result) != 0) {
65 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
66 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
68 $tmp = "<span class=\"labelColorIndicator\" style='color : $fg_color; background-color : $bg_color'>α</span> " . $line['action_param'];
70 $line['action_param'] = $tmp;
76 $filter['id'] = 'FILTER:' . $line['id'];
77 $filter['bare_id'] = $line['id'];
78 $filter['name'] = $line['reg_exp'];
79 $filter['type'] = $line['filter_type'];
80 $filter['enabled'] = sql_bool_to_bool($line['enabled']);
81 $filter['param'] = $line['action_param'];
82 $filter['inverse'] = sql_bool_to_bool($line['inverse']);
83 $filter['checkbox'] = false;
86 $filter['feed'] = $line['feed_title'];
88 array_push($cat['items'], $filter);
91 array_push($root['items'], $cat);
94 $fl['identifier'] = 'id';
95 $fl['label'] = 'name';
96 $fl['items'] = array($root);
98 print json_encode($fl);
102 if ($subop == "edit") {
104 $filter_id = db_escape_string($_REQUEST["id"]);
106 $result = db_query($link,
107 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
109 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
110 $filter_type = db_fetch_result($result, 0, "filter_type");
111 $feed_id = db_fetch_result($result, 0, "feed_id");
112 $action_id = db_fetch_result($result, 0, "action_id");
113 $action_param = db_fetch_result($result, 0, "action_param");
114 $filter_param = db_fetch_result($result, 0, "filter_param");
116 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
117 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
119 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
121 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
122 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
123 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"subop\" value=\"editSave\">";
125 $result = db_query($link, "SELECT id,description
126 FROM ttrss_filter_types ORDER BY description");
128 $filter_types = array();
130 while ($line = db_fetch_assoc($result)) {
131 //array_push($filter_types, $line["description"]);
132 $filter_types[$line["id"]] = __($line["description"]);
135 print "<div class=\"dlgSec\">".__("Match")."</div>";
137 print "<div class=\"dlgSecCont\">";
139 if ($filter_type != 5) {
140 $date_ops_invisible = 'style="display : none"';
143 print "<span id=\"filterDlg_dateModBox\" $date_ops_invisible>";
144 print __("Date") . " ";
146 $filter_params = array(
147 "before" => __("before"),
148 "after" => __("after"));
150 print_select_hash("filter_date_modifier", $filter_param,
151 $filter_params, 'dojoType="dijit.form.Select"');
153 print " </span>";
155 print "<input dojoType=\"dijit.form.ValidationTextBox\"
157 name=\"reg_exp\" style=\"font-size : 16px;\" value=\"$reg_exp\">";
159 print "<span id=\"filterDlg_dateChkBox\" $date_ops_invisible>";
160 print " <button dojoType=\"dijit.form.Button\" onclick=\"return filterDlgCheckDate()\">".
161 __('Check it')."</button>";
164 print "<br/> " . __("on field") . " ";
165 print_select_hash("filter_type", $filter_type, $filter_types,
166 'onchange="filterDlgCheckType(this)" dojoType="dijit.form.Select"');
170 print __("in") . " ";
171 print_feed_select($link, "feed_id", $feed_id,
172 'dojoType="dijit.form.FilteringSelect"');
176 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
178 print "<div class=\"dlgSecCont\">";
180 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
181 onchange=\"filterDlgCheckAction(this)\">";
183 $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions
186 while ($line = db_fetch_assoc($result)) {
187 $is_sel = ($line["id"] == $action_id) ?
"selected=\"1\"" : "";
188 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
193 $param_hidden = ($action_id == 4 ||
$action_id == 6 ||
$action_id == 7) ?
"" : "display : none";
195 print "<span id=\"filterDlg_paramBox\" style=\"$param_hidden\">";
196 print " " . __("with parameters:") . " ";
198 $param_int_hidden = ($action_id != 7) ?
"" : "display : none";
200 print "<input style=\"$param_int_hidden\"
201 dojoType=\"dijit.form.TextBox\" id=\"filterDlg_actionParam\"
202 name=\"action_param\" value=\"$action_param\">";
204 $param_int_hidden = ($action_id == 7) ?
"" : "display : none";
206 print_label_select($link, "action_param_label", $action_param,
207 "style=\"$param_int_hidden\"" .
208 'id="filterDlg_actionParamLabel" dojoType="dijit.form.Select"');
212 print " "; // tiny layout hack
216 print "<div class=\"dlgSec\">".__("Options")."</div>";
217 print "<div class=\"dlgSecCont\">";
219 print "<div style=\"line-height : 100%\">";
222 $checked = "checked=\"1\"";
227 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
228 <label for=\"enabled\">".__('Enabled')."</label><br/>";
231 $checked = "checked=\"1\"";
236 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
237 <label for=\"inverse\">".__('Inverse match')."</label>";
242 print "<div class=\"dlgButtons\">";
244 $reg_exp = htmlspecialchars($reg_exp, ENT_QUOTES
); // second escaping seems to be needed for javascript
246 print "<div style=\"float : left\">";
247 print "<button dojoType=\"dijit.form.Button\" onclick='return removeFilter($filter_id, \"$reg_exp\")'>".
248 __('Remove')."</button>";
251 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
252 __('Save')."</button> ";
254 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
255 __('Cancel')."</button>";
263 if ($subop == "editSave") {
267 if ($memcache) $memcache->flush();
269 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
270 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
271 $filter_id = db_escape_string($_REQUEST["id"]);
272 $feed_id = db_escape_string($_REQUEST["feed_id"]);
273 $action_id = db_escape_string($_REQUEST["action_id"]);
274 $action_param = db_escape_string($_REQUEST["action_param"]);
275 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
276 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
277 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
279 # for the time being, no other filters use params anyway...
280 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
285 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
288 /* When processing 'assign label' filters, action_param_label dropbox
289 * overrides action_param */
291 if ($action_id == 7) {
292 $action_param = $action_param_label;
295 $result = db_query($link, "UPDATE ttrss_filters SET
296 reg_exp = '$reg_exp',
298 action_id = '$action_id',
299 filter_type = '$filter_type',
302 action_param = '$action_param',
303 filter_param = '$filter_param'
304 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
307 if ($subop == "remove") {
309 if ($memcache) $memcache->flush();
311 $ids = split(",", db_escape_string($_REQUEST["ids"]));
313 foreach ($ids as $id) {
314 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
318 if ($subop == "add") {
320 if ($memcache) $memcache->flush();
322 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
323 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
324 $feed_id = db_escape_string($_REQUEST["feed_id"]);
325 $action_id = db_escape_string($_REQUEST["action_id"]);
326 $action_param = db_escape_string($_REQUEST["action_param"]);
327 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
328 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
330 # for the time being, no other filters use params anyway...
331 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
333 if (!$regexp) return;
338 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
341 /* When processing 'assign label' filters, action_param_label dropbox
342 * overrides action_param */
344 if ($action_id == 7) {
345 $action_param = $action_param_label;
348 $result = db_query($link,
349 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
350 action_id, action_param, inverse, filter_param)
352 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
353 $feed_id, '$action_id', '$action_param', $inverse, '$filter_param')");
355 if (db_affected_rows($link, $result) != 0) {
356 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
364 $sort = db_escape_string($_REQUEST["sort"]);
366 if (!$sort ||
$sort == "undefined") {
370 $result = db_query($link, "SELECT id,description
371 FROM ttrss_filter_types ORDER BY description");
373 $filter_types = array();
375 while ($line = db_fetch_assoc($result)) {
376 //array_push($filter_types, $line["description"]);
377 $filter_types[$line["id"]] = $line["description"];
381 $filter_search = db_escape_string($_REQUEST["search"]);
383 if (array_key_exists("search", $_REQUEST)) {
384 $_SESSION["prefs_filter_search"] = $filter_search;
386 $filter_search = $_SESSION["prefs_filter_search"];
389 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
390 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
391 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
393 print "<div dojoType=\"dijit.form.DropDownButton\">".
394 "<span>" . __('Select')."</span>";
395 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
396 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
397 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
398 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
399 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
400 print "</div></div>";
402 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
403 __('Create filter')."</button dojoType=\"dijit.form.Button\"> ";
405 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
406 __('Edit')."</button dojoType=\"dijit.form.Button\"> ";
408 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
409 __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
411 if (defined('_ENABLE_FEED_DEBUGGING')) {
412 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
413 __('Rescore articles')."</button dojoType=\"dijit.form.Button\"> ";
416 print "</div>"; # toolbar
417 print "</div>"; # toolbar-frame
418 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
420 print "<div id=\"filterlistLoading\">
421 <img src='images/indicator_tiny.gif'>".
422 __("Loading, please wait...")."</div>";
424 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
425 url=\"backend.php?op=pref-filters&subop=getfiltertree\">
427 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
428 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
429 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
431 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
432 model=\"filterModel\" openOnClick=\"true\">
433 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
434 Element.hide(\"filterlistLoading\");
438 print "</div>"; #pane
439 print "</div>"; #container