]> git.wh0rd.org - tt-rss.git/blob - modules/pref-filters.php
tweak feed-prefs; rework filter prefs to use dijit.Tree
[tt-rss.git] / modules / pref-filters.php
1 <?php
2 function module_pref_filters($link) {
3 $subop = $_REQUEST["subop"];
4 $quiet = $_REQUEST["quiet"];
5
6 if ($subop == "getfiltertree") {
7 $root = array();
8 $root['id'] = 'root';
9 $root['name'] = __('Filters');
10 $root['items'] = array();
11
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,
16 enabled,
17 inverse,
18 feed_id,
19 action_id,
20 filter_param,
21 filter_type,
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
26 FROM
27 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
28 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
29 WHERE
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");
34
35 $cat = false;
36 $cur_action_description = "";
37
38 while ($line = db_fetch_assoc($result)) {
39 if ($cur_action_description != $line['action_description']) {
40
41 if ($cat)
42 array_push($root['items'], $cat);
43
44 $cat = array();
45 $cat['id'] = 'ACTION:' . $line['action_id'];
46 $cat['name'] = $line['action_description'];
47 $cat['items'] = array();
48
49 $cur_action_description = $line['action_description'];
50 }
51
52 if (array_search($line["action_name"],
53 array("score", "tag", "label")) === false) {
54
55 $line["action_param"] = '';
56 } else {
57 if ($line['action_name'] == 'label') {
58
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"]);
63
64 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
65 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
66
67 $tmp = "<span class=\"labelColorIndicator\" style='color : $fg_color; background-color : $bg_color'>&alpha;</span> " . $line['action_param'];
68
69 $line['action_param'] = $tmp;
70 }
71 }
72
73 $filter = array();
74 $filter['id'] = 'FILTER:' . $line['id'];
75 $filter['bare_id'] = $line['id'];
76 $filter['name'] = $line['reg_exp'];
77 $filter['type'] = $line['filter_type'];
78 $filter['enabled'] = sql_bool_to_bool($line['enabled']);
79 $filter['param'] = $line['action_param'];
80 $filter['inverse'] = sql_bool_to_bool($line['inverse']);
81 $filter['checkbox'] = false;
82
83 if ($line['feed_id'])
84 $filter['feed'] = $line['feed_title'];
85
86 array_push($cat['items'], $filter);
87 }
88
89 array_push($root['items'], $cat);
90
91 $fl = array();
92 $fl['identifier'] = 'id';
93 $fl['label'] = 'name';
94 $fl['items'] = array($root);
95
96 print json_encode($fl);
97 return;
98 }
99
100 if ($subop == "edit") {
101
102 $filter_id = db_escape_string($_REQUEST["id"]);
103
104 header("Content-Type: text/xml");
105 print "<dlg id=\"$subop\">";
106 print "<title>".__('Filter Editor')."</title>";
107 print "<content><![CDATA[";
108
109 $result = db_query($link,
110 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
111
112 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
113 $filter_type = db_fetch_result($result, 0, "filter_type");
114 $feed_id = db_fetch_result($result, 0, "feed_id");
115 $action_id = db_fetch_result($result, 0, "action_id");
116 $action_param = db_fetch_result($result, 0, "action_param");
117 $filter_param = db_fetch_result($result, 0, "filter_param");
118
119 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
120 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
121
122 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
123
124 print "<input type=\"hidden\" name=\"op\" value=\"pref-filters\">";
125 print "<input type=\"hidden\" name=\"id\" value=\"$filter_id\">";
126 print "<input type=\"hidden\" name=\"subop\" value=\"editSave\">";
127
128 $result = db_query($link, "SELECT id,description
129 FROM ttrss_filter_types ORDER BY description");
130
131 $filter_types = array();
132
133 while ($line = db_fetch_assoc($result)) {
134 //array_push($filter_types, $line["description"]);
135 $filter_types[$line["id"]] = __($line["description"]);
136 }
137
138 print "<div class=\"dlgSec\">".__("Match")."</div>";
139
140 print "<div class=\"dlgSecCont\">";
141
142 if ($filter_type != 5) {
143 $date_ops_invisible = 'style="display : none"';
144 }
145
146 print "<span id=\"filter_dlg_date_mod_box\" $date_ops_invisible>";
147 print __("Date") . " ";
148
149 $filter_params = array(
150 "before" => __("before"),
151 "after" => __("after"));
152
153 print_select_hash("filter_date_modifier", $filter_param,
154 $filter_params);
155
156 print "&nbsp;</span>";
157
158 print "<input onkeypress=\"return filterCR(event, filterEditSave)\"
159 name=\"reg_exp\" size=\"30\" value=\"$reg_exp\">";
160
161 print "<span id=\"filter_dlg_date_chk_box\" $date_ops_invisible>";
162 print "&nbsp;<button onclick=\"return filterDlgCheckDate()\">".
163 __('Check it')."</button>";
164 print "</span>";
165
166 print "<br/> " . __("on field") . " ";
167 print_select_hash("filter_type", $filter_type, $filter_types,
168 'onchange="filterDlgCheckType(this)"');
169
170 print "<br/>";
171
172 print __("in") . " ";
173 print_feed_select($link, "feed_id", $feed_id);
174
175 print "</div>";
176
177 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
178
179 print "<div class=\"dlgSecCont\">";
180
181 print "<select name=\"action_id\"
182 onchange=\"filterDlgCheckAction(this)\">";
183
184 $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions
185 ORDER BY name");
186
187 while ($line = db_fetch_assoc($result)) {
188 $is_sel = ($line["id"] == $action_id) ? "selected" : "";
189 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
190 }
191
192 print "</select>";
193
194 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
195
196 print "<span id=\"filter_dlg_param_box\" style=\"$param_hidden\">";
197 print " " . __("with parameters:") . " ";
198
199 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
200
201 print "<input size=\"20\" style=\"$param_int_hidden\"
202 onkeypress=\"return filterCR(event, filterEditSave)\"
203 name=\"action_param\" value=\"$action_param\">";
204
205 $param_int_hidden = ($action_id == 7) ? "" : "display : none";
206
207 print_label_select($link, "action_param_label", $action_param,
208 $param_int_hidden);
209
210 print "</span>";
211
212 print "&nbsp;"; // tiny layout hack
213
214 print "</div>";
215
216 print "<div class=\"dlgSec\">".__("Options")."</div>";
217 print "<div class=\"dlgSecCont\">";
218
219 print "<div style=\"line-height : 100%\">";
220
221 if ($enabled) {
222 $checked = "checked";
223 } else {
224 $checked = "";
225 }
226
227 print "<input type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
228 <label for=\"enabled\">".__('Enabled')."</label><br/>";
229
230 if ($inverse) {
231 $checked = "checked";
232 } else {
233 $checked = "";
234 }
235
236 print "<input type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
237 <label for=\"inverse\">".__('Inverse match')."</label>";
238
239 print "</div>";
240 print "</div>";
241
242 print "<div class=\"dlgButtons\">";
243
244 $reg_exp = htmlspecialchars($reg_exp, ENT_QUOTES); // second escaping seems to be needed for javascript
245
246 print "<div style=\"float : left\">";
247 print "<button onclick='return removeFilter($filter_id, \"$reg_exp\")'>".
248 __('Remove')."</button>";
249 print "</div>";
250
251 print "<button onclick=\"return filterEditSave()\">".
252 __('Save')."</button> ";
253
254 print "<button onclick=\"return filterEditCancel()\">".
255 __('Cancel')."</button>";
256
257 print "]]></content></dlg>";
258
259 return;
260 }
261
262
263 if ($subop == "editSave") {
264
265 global $memcache;
266
267 if ($memcache) $memcache->flush();
268
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"]));
278
279 # for the time being, no other filters use params anyway...
280 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
281
282 if (!$feed_id) {
283 $feed_id = 'NULL';
284 } else {
285 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
286 }
287
288 /* When processing 'assign label' filters, action_param_label dropbox
289 * overrides action_param */
290
291 if ($action_id == 7) {
292 $action_param = $action_param_label;
293 }
294
295 $result = db_query($link, "UPDATE ttrss_filters SET
296 reg_exp = '$reg_exp',
297 feed_id = $feed_id,
298 action_id = '$action_id',
299 filter_type = '$filter_type',
300 enabled = $enabled,
301 inverse = $inverse,
302 action_param = '$action_param',
303 filter_param = '$filter_param'
304 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
305 }
306
307 if ($subop == "remove") {
308
309 if ($memcache) $memcache->flush();
310
311 $ids = split(",", db_escape_string($_REQUEST["ids"]));
312
313 foreach ($ids as $id) {
314 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
315 }
316 }
317
318 if ($subop == "add") {
319
320 if ($memcache) $memcache->flush();
321
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"]));
329
330 # for the time being, no other filters use params anyway...
331 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
332
333 if (!$regexp) return;
334
335 if (!$feed_id) {
336 $feed_id = 'NULL';
337 } else {
338 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
339 }
340
341 /* When processing 'assign label' filters, action_param_label dropbox
342 * overrides action_param */
343
344 if ($action_id == 7) {
345 $action_param = $action_param_label;
346 }
347
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)
351 VALUES
352 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
353 $feed_id, '$action_id', '$action_param', $inverse, '$filter_param')");
354
355 if (db_affected_rows($link, $result) != 0) {
356 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
357 }
358
359 return;
360 }
361
362 if ($quiet) return;
363
364 set_pref($link, "_PREFS_ACTIVE_TAB", "filterConfig");
365
366 $sort = db_escape_string($_REQUEST["sort"]);
367
368 if (!$sort || $sort == "undefined") {
369 $sort = "reg_exp";
370 }
371
372 $result = db_query($link, "SELECT id,description
373 FROM ttrss_filter_types ORDER BY description");
374
375 $filter_types = array();
376
377 while ($line = db_fetch_assoc($result)) {
378 //array_push($filter_types, $line["description"]);
379 $filter_types[$line["id"]] = $line["description"];
380 }
381
382
383 $filter_search = db_escape_string($_REQUEST["search"]);
384
385 if (array_key_exists("search", $_REQUEST)) {
386 $_SESSION["prefs_filter_search"] = $filter_search;
387 } else {
388 $filter_search = $_SESSION["prefs_filter_search"];
389 }
390
391 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
392 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
393 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
394
395 print "<div style='float : right; padding-right : 4px;'>
396 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
397 onfocus=\"javascript:disableHotkeys();\"
398 onblur=\"javascript:enableHotkeys();\"
399 onchange=\"javascript:updateFilterList()\" value=\"$filter_search\">
400 <button dojoType=\"dijit.form.Button\" onclick=\"javascript:updateFilterList()\">".__('Search')."</button>
401 </div>";
402
403 print "<div dojoType=\"dijit.form.DropDownButton\">".
404 "<span>" . __('Select')."</span>";
405 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
406 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
407 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
408 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
409 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
410 print "</div></div>";
411
412 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
413 __('Create filter')."</button dojoType=\"dijit.form.Button\"> ";
414
415 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
416 __('Edit')."</button dojoType=\"dijit.form.Button\"> ";
417
418 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
419 __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
420
421 if (defined('_ENABLE_FEED_DEBUGGING')) {
422 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
423 __('Rescore articles')."</button dojoType=\"dijit.form.Button\"> ";
424 }
425
426 print "</div>"; # toolbar
427 print "</div>"; # toolbar-frame
428 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
429
430 print "<div id=\"filterlistLoading\">
431 <img src='images/indicator_tiny.gif'>".
432 __("Loading, please wait...")."</div>";
433
434 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
435 url=\"backend.php?op=pref-filters&subop=getfiltertree\">
436 </div>
437 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
438 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
439 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
440 </div>
441 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
442 model=\"filterModel\" openOnClick=\"true\">
443 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
444 Element.hide(\"filterlistLoading\");
445 </script>
446 </div>";
447
448 print "</div>"; #pane
449 print "</div>"; #container
450 }
451
452 ?>