]> git.wh0rd.org - tt-rss.git/blob - modules/pref-filters.php
use Modalbox library for popup dialogs
[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 == "edit") {
7
8 $filter_id = db_escape_string($_REQUEST["id"]);
9
10 header("Content-Type: text/xml");
11 print "<dlg id=\"$subop\">";
12 print "<title>".__('Filter Editor')."</title>";
13 print "<content><![CDATA[";
14
15 $result = db_query($link,
16 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
17
18 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
19 $filter_type = db_fetch_result($result, 0, "filter_type");
20 $feed_id = db_fetch_result($result, 0, "feed_id");
21 $action_id = db_fetch_result($result, 0, "action_id");
22 $action_param = db_fetch_result($result, 0, "action_param");
23 $filter_param = db_fetch_result($result, 0, "filter_param");
24
25 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
26 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
27
28 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
29
30 print "<input type=\"hidden\" name=\"op\" value=\"pref-filters\">";
31 print "<input type=\"hidden\" name=\"id\" value=\"$filter_id\">";
32 print "<input type=\"hidden\" name=\"subop\" value=\"editSave\">";
33
34 $result = db_query($link, "SELECT id,description
35 FROM ttrss_filter_types ORDER BY description");
36
37 $filter_types = array();
38
39 while ($line = db_fetch_assoc($result)) {
40 //array_push($filter_types, $line["description"]);
41 $filter_types[$line["id"]] = __($line["description"]);
42 }
43
44 print "<div class=\"dlgSec\">".__("Match")."</div>";
45
46 print "<div class=\"dlgSecCont\">";
47
48 if ($filter_type != 5) {
49 $date_ops_invisible = 'style="display : none"';
50 }
51
52 print "<span id=\"filter_dlg_date_mod_box\" $date_ops_invisible>";
53 print __("Date") . " ";
54
55 $filter_params = array(
56 "before" => __("before"),
57 "after" => __("after"));
58
59 print_select_hash("filter_date_modifier", $filter_param,
60 $filter_params);
61
62 print "&nbsp;</span>";
63
64 print "<input onkeypress=\"return filterCR(event, filterEditSave)\"
65 name=\"reg_exp\" size=\"30\" value=\"$reg_exp\">";
66
67 print "<span id=\"filter_dlg_date_chk_box\" $date_ops_invisible>";
68 print "&nbsp;<button onclick=\"return filterDlgCheckDate()\">".
69 __('Check it')."</button>";
70 print "</span>";
71
72 print "<br/> " . __("on field") . " ";
73 print_select_hash("filter_type", $filter_type, $filter_types,
74 'onchange="filterDlgCheckType(this)"');
75
76 print "<br/>";
77
78 print __("in") . " ";
79 print_feed_select($link, "feed_id", $feed_id);
80
81 print "</div>";
82
83 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
84
85 print "<div class=\"dlgSecCont\">";
86
87 print "<select name=\"action_id\"
88 onchange=\"filterDlgCheckAction(this)\">";
89
90 $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions
91 ORDER BY name");
92
93 while ($line = db_fetch_assoc($result)) {
94 $is_sel = ($line["id"] == $action_id) ? "selected" : "";
95 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
96 }
97
98 print "</select>";
99
100 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
101
102 print "<span id=\"filter_dlg_param_box\" style=\"$param_hidden\">";
103 print " " . __("with parameters:") . " ";
104
105 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
106
107 print "<input size=\"20\" style=\"$param_int_hidden\"
108 onkeypress=\"return filterCR(event, filterEditSave)\"
109 name=\"action_param\" value=\"$action_param\">";
110
111 $param_int_hidden = ($action_id == 7) ? "" : "display : none";
112
113 print_label_select($link, "action_param_label", $action_param,
114 $param_int_hidden);
115
116 print "</span>";
117
118 print "&nbsp;"; // tiny layout hack
119
120 print "</div>";
121
122 print "<div class=\"dlgSec\">".__("Options")."</div>";
123 print "<div class=\"dlgSecCont\">";
124
125 print "<div style=\"line-height : 100%\">";
126
127 if ($enabled) {
128 $checked = "checked";
129 } else {
130 $checked = "";
131 }
132
133 print "<input type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
134 <label for=\"enabled\">".__('Enabled')."</label><br/>";
135
136 if ($inverse) {
137 $checked = "checked";
138 } else {
139 $checked = "";
140 }
141
142 print "<input type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
143 <label for=\"inverse\">".__('Inverse match')."</label>";
144
145 print "</div>";
146 print "</div>";
147
148 print "<div class=\"dlgButtons\">";
149
150 $reg_exp = htmlspecialchars($reg_exp, ENT_QUOTES); // second escaping seems to be needed for javascript
151
152 print "<div style=\"float : left\">";
153 print "<button onclick='return removeFilter($filter_id, \"$reg_exp\")'>".
154 __('Remove')."</button>";
155 print "</div>";
156
157 print "<button onclick=\"return filterEditSave()\">".
158 __('Save')."</button> ";
159
160 print "<button onclick=\"return filterEditCancel()\">".
161 __('Cancel')."</button>";
162
163 print "]]></content></dlg>";
164
165 return;
166 }
167
168
169 if ($subop == "editSave") {
170
171 global $memcache;
172
173 if ($memcache) $memcache->flush();
174
175 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
176 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
177 $filter_id = db_escape_string($_REQUEST["id"]);
178 $feed_id = db_escape_string($_REQUEST["feed_id"]);
179 $action_id = db_escape_string($_REQUEST["action_id"]);
180 $action_param = db_escape_string($_REQUEST["action_param"]);
181 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
182 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
183 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
184
185 # for the time being, no other filters use params anyway...
186 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
187
188 if (!$feed_id) {
189 $feed_id = 'NULL';
190 } else {
191 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
192 }
193
194 /* When processing 'assign label' filters, action_param_label dropbox
195 * overrides action_param */
196
197 if ($action_id == 7) {
198 $action_param = $action_param_label;
199 }
200
201 $result = db_query($link, "UPDATE ttrss_filters SET
202 reg_exp = '$reg_exp',
203 feed_id = $feed_id,
204 action_id = '$action_id',
205 filter_type = '$filter_type',
206 enabled = $enabled,
207 inverse = $inverse,
208 action_param = '$action_param',
209 filter_param = '$filter_param'
210 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
211
212 if (db_affected_rows($link, $result) != 0) {
213 print_notice(T_sprintf("Saved filter <b>%s</b>", htmlspecialchars($reg_exp)));
214 }
215
216 }
217
218 if ($subop == "remove") {
219
220 if ($memcache) $memcache->flush();
221
222 $ids = split(",", db_escape_string($_REQUEST["ids"]));
223
224 foreach ($ids as $id) {
225 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
226 }
227 }
228
229 if ($subop == "add") {
230
231 if ($memcache) $memcache->flush();
232
233 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
234 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
235 $feed_id = db_escape_string($_REQUEST["feed_id"]);
236 $action_id = db_escape_string($_REQUEST["action_id"]);
237 $action_param = db_escape_string($_REQUEST["action_param"]);
238 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
239 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
240
241 # for the time being, no other filters use params anyway...
242 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
243
244 if (!$regexp) return;
245
246 if (!$feed_id) {
247 $feed_id = 'NULL';
248 } else {
249 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
250 }
251
252 /* When processing 'assign label' filters, action_param_label dropbox
253 * overrides action_param */
254
255 if ($action_id == 7) {
256 $action_param = $action_param_label;
257 }
258
259 $result = db_query($link,
260 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
261 action_id, action_param, inverse, filter_param)
262 VALUES
263 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
264 $feed_id, '$action_id', '$action_param', $inverse, '$filter_param')");
265
266 if (db_affected_rows($link, $result) != 0) {
267 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
268 }
269
270 return;
271 }
272
273 if ($quiet) return;
274
275 set_pref($link, "_PREFS_ACTIVE_TAB", "filterConfig");
276
277 $sort = db_escape_string($_REQUEST["sort"]);
278
279 if (!$sort || $sort == "undefined") {
280 $sort = "reg_exp";
281 }
282
283 $result = db_query($link, "SELECT id,description
284 FROM ttrss_filter_types ORDER BY description");
285
286 $filter_types = array();
287
288 while ($line = db_fetch_assoc($result)) {
289 //array_push($filter_types, $line["description"]);
290 $filter_types[$line["id"]] = $line["description"];
291 }
292
293
294 $filter_search = db_escape_string($_REQUEST["search"]);
295
296 if (array_key_exists("search", $_REQUEST)) {
297 $_SESSION["prefs_filter_search"] = $filter_search;
298 } else {
299 $filter_search = $_SESSION["prefs_filter_search"];
300 }
301
302 print "<div style='float : right'>
303 <input id=\"filter_search\" size=\"20\" type=\"search\"
304 onfocus=\"javascript:disableHotkeys();\"
305 onblur=\"javascript:enableHotkeys();\"
306 onchange=\"javascript:updateFilterList()\" value=\"$filter_search\">
307 <button onclick=\"javascript:updateFilterList()\">".__('Search')."</button>
308 &nbsp;
309 <a class='helpLinkPic' href=\"javascript:displayHelpInfobox(2)\">
310 <img style='vertical-align : top;' src='".theme_image($link, "images/sign_quest.png")."'></a>
311 </div>";
312
313 print "<button onclick=\"return quickAddFilter()\">".
314 __('Create filter')."</button> ";
315
316 print "<button onclick=\"return editSelectedFilter()\">".
317 __('Edit')."</button> ";
318
319 print "<button onclick=\"return removeSelectedFilters()\">".
320 __('Remove')."</button> ";
321
322 if (defined('_ENABLE_FEED_DEBUGGING')) {
323 print "<button onclick=\"rescore_all_feeds()\">".
324 __('Rescore articles')."</button> ";
325 }
326
327 if ($filter_search) {
328 $filter_search = split(' ', db_escape_string($filter_search));
329
330 $tokens = array();
331
332 foreach ($filter_search as $token) {
333 $token = trim($token);
334
335 array_push($tokens, "(
336 UPPER(ttrss_filter_actions.description) LIKE UPPER('%$token%') OR
337 UPPER(reg_exp) LIKE UPPER('%$token%') OR
338 UPPER(action_param) LIKE UPPER('%$token%') OR
339 UPPER(ttrss_feeds.title) LIKE UPPER('%$token%') OR
340 UPPER(ttrss_filter_types.description) LIKE UPPER('%$token%'))");
341 }
342
343 $filter_search_query = "(" . join($tokens, " AND ") . ") AND ";
344
345 } else {
346 $filter_search_query = "";
347 }
348
349 $result = db_query($link, "SELECT
350 ttrss_filters.id AS id,reg_exp,
351 ttrss_filter_types.name AS filter_type_name,
352 ttrss_filter_types.description AS filter_type_descr,
353 enabled,
354 inverse,
355 feed_id,
356 filter_param,
357 filter_type,
358 ttrss_filter_actions.description AS action_description,
359 ttrss_feeds.title AS feed_title,
360 ttrss_filter_actions.name AS action_name,
361 ttrss_filters.action_param AS action_param
362 FROM
363 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
364 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
365 WHERE
366 filter_type = ttrss_filter_types.id AND
367 $filter_search_query
368 ttrss_filter_actions.id = action_id AND
369 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
370 ORDER by action_description, $sort");
371
372 if (db_num_rows($result) != 0) {
373
374 print "<p><table width=\"100%\" cellspacing=\"0\" class=\"prefFilterList\"
375 id=\"prefFilterList\">";
376
377 print "<tr><td class=\"selectPrompt\" colspan=\"8\">
378 ".__('Select:')."
379 <a href=\"#\" onclick=\"selectTableRows('prefFilterList', 'all')\">".__('All')."</a>,
380 <a href=\"#\" onclick=\"selectTableRows('prefFilterList', 'none')\">".__('None')."</a>
381 </td</tr>";
382
383 $lnum = 0;
384
385 $cur_action_description = "";
386
387 while ($line = db_fetch_assoc($result)) {
388
389 $filter_id = $line["id"];
390 $edit_filter_id = $_REQUEST["id"];
391
392 $enabled = sql_bool_to_bool($line["enabled"]);
393 $inverse = sql_bool_to_bool($line["inverse"]);
394
395 $this_row_id = "id=\"FILRR-$filter_id\"";
396
397 $line["filter_type_descr"] = __($line["filter_type_descr"]);
398 $line["action_description"] = __($line["action_description"]);
399
400 if ($line["action_description"] != $cur_action_description) {
401 $cur_action_description = $line["action_description"];
402
403 print "<tr><td class='filterEditCat' colspan='6'>$cur_action_description</td></tr>";
404
405 print "<tr class=\"title\">
406 <td align='center' width=\"5%\">&nbsp;</td>
407 <td width=\"20%\"><a href=\"#\" onclick=\"updateFilterList('reg_exp')\">".__('Match')."</a></td>
408 <td width=\"\"><a href=\"#\" onclick=\"updateFilterList('feed_title')\">".__('Feed')."</a></td>
409 <td width=\"20%\"><a href=\"#\" onclick=\"updateFilterList('filter_type')\">".__('Field')."</a></td>
410 <td width=\"20%\"><a href=\"#\" onclick=\"updateFilterList('action_param')\">".__('Params')."</a></td>";
411
412 $lnum = 0;
413 }
414
415 $class = ($lnum % 2) ? "even" : "odd";
416
417 print "<tr class=\"$class\" $this_row_id>";
418
419 $line["reg_exp"] = htmlspecialchars($line["reg_exp"]);
420
421 if (!$line["feed_title"]) $line["feed_title"] = __("All feeds");
422
423 if (array_search($line["action_name"],
424 array("score", "tag", "label")) === false) {
425
426 $line["action_param"] = false;
427 }
428
429 if (!$line["action_param"]) {
430 $line["action_param"] = "&mdash;";
431 } else if ($line["action_name"] == "score") {
432
433 $score_pic = theme_image($link,
434 "images/" . get_score_pic($line["action_param"]));
435
436 $score_pic = "<img class='hlScorePic' src=\"$score_pic\">";
437
438 $line["action_param"] = "$score_pic " . $line["action_param"];
439
440 }
441
442 $line["feed_title"] = htmlspecialchars($line["feed_title"]);
443
444 print "<td align='center'><input onclick='toggleSelectRow(this);'
445 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
446
447 $filter_params = array(
448 "before" => __("before"),
449 "after" => __("after"));
450
451 if ($line["action_name"] == 'label') {
452
453 $tmp_result = db_query($link, "SELECT fg_color, bg_color
454 FROM ttrss_labels2 WHERE caption = '".
455 db_escape_string($line["action_param"])."' AND
456 owner_uid = " . $_SESSION["uid"]);
457
458 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
459 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
460
461 $tmp = "<div class='labelColorIndicator' id='LICID-$id'
462 style='color : $fg_color; background-color : $bg_color'>
463 &alpha;";
464 $tmp .= "</div>";
465
466 $line["action_param"] = "$tmp " . $line["action_param"];
467 }
468
469 if ($line["filter_type"] == 5) {
470
471 if (!strtotime($line["reg_exp"])) {
472 $line["reg_exp"] = "<span class=\"filterDateError\">" .
473 $line["reg_exp"] . "</span>";
474 }
475
476 $line["reg_exp"] = __("Date") . " " .
477 $filter_params[$line['filter_param']] . " " .
478 $line["reg_exp"];
479 }
480
481 if (!$enabled) {
482 $line["reg_exp"] = "<span class=\"insensitive\">" .
483 $line["reg_exp"] . " " . __("(Disabled)")."</span>";
484 $line["feed_title"] = "<span class=\"insensitive\">" .
485 $line["feed_title"] . "</span>";
486 $line["filter_type_descr"] = "<span class=\"insensitive\">" .
487 $line["filter_type_descr"] . "</span>";
488 $line["action_description"] = "<span class=\"insensitive\">" .
489 $line["action_description"] . "</span>";
490 $line["action_param"] = "<span class=\"insensitive\">" .
491 $line["action_param"] . "</span>";
492 }
493
494 $onclick = "onclick='editFilter($filter_id, event)' title='".__('Click to edit')."'";
495
496 $inverse_label = "";
497
498 if ($inverse) {
499 $inverse_label = " <span class='insensitive'>".__('(Inverse)')."</span>";
500 }
501
502 print "<td $onclick>" . $line["reg_exp"] . "$inverse_label</td>";
503 print "<td $onclick>" . $line["feed_title"] . "</td>";
504
505 print "<td $onclick>" . $line["filter_type_descr"] . "</td>";
506 print "<td $onclick>" . $line["action_param"] . "</td>";
507
508 print "</tr>";
509
510 ++$lnum;
511 }
512
513 print "</table>";
514
515 } else {
516
517 print "<p>";
518 if (!$filter_search) {
519 print_warning(__('No filters defined.'));
520 } else {
521 print_warning(__('No matching filters found.'));
522 }
523 print "</p>";
524
525 }
526 }
527
528 ?>