]> git.wh0rd.org - tt-rss.git/blame - modules/pref-filters.php
fix startup tab handling
[tt-rss.git] / modules / pref-filters.php
CommitLineData
ef8be8ea
AD
1<?php
2 function module_pref_filters($link) {
b4e75b2a
AD
3 $subop = $_REQUEST["subop"];
4 $quiet = $_REQUEST["quiet"];
ef8be8ea
AD
5
6 if ($subop == "edit") {
7
b4e75b2a 8 $filter_id = db_escape_string($_REQUEST["id"]);
ef8be8ea 9
951906dc
AD
10 header("Content-Type: text/xml");
11 print "<dlg id=\"$subop\">";
12 print "<title>".__('Filter Editor')."</title>";
13 print "<content><![CDATA[";
14
ef8be8ea
AD
15 $result = db_query($link,
16 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
17
47439031 18 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
ef8be8ea
AD
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");
073ca0e6 22 $action_param = db_fetch_result($result, 0, "action_param");
44d0e774 23 $filter_param = db_fetch_result($result, 0, "filter_param");
ef8be8ea
AD
24
25 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
3f2ff803 26 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
ef8be8ea 27
e6312f6c 28 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
ef8be8ea
AD
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\">";
ef8be8ea
AD
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"]);
89cb787e 41 $filter_types[$line["id"]] = __($line["description"]);
ef8be8ea
AD
42 }
43
10fa6615
AD
44 print "<div class=\"dlgSec\">".__("Match")."</div>";
45
46 print "<div class=\"dlgSecCont\">";
47
6e278744 48 if ($filter_type != 5) {
df00fb80 49 $date_ops_invisible = 'style="display : none"';
6e278744
AD
50 }
51
52 print "<span id=\"filter_dlg_date_mod_box\" $date_ops_invisible>";
53 print __("Date") . " ";
44d0e774
AD
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>";
6e278744 63
10fa6615 64 print "<input onkeypress=\"return filterCR(event, filterEditSave)\"
10fa6615
AD
65 name=\"reg_exp\" size=\"30\" value=\"$reg_exp\">";
66
6e278744 67 print "<span id=\"filter_dlg_date_chk_box\" $date_ops_invisible>";
a918f5f9
AD
68 print "&nbsp;<button onclick=\"return filterDlgCheckDate()\">".
69 __('Check it')."</button>";
6e278744
AD
70 print "</span>";
71
72 print "<br/> " . __("on field") . " ";
73 print_select_hash("filter_type", $filter_type, $filter_types,
74 'onchange="filterDlgCheckType(this)"');
10fa6615
AD
75
76 print "<br/>";
77
78 print __("in") . " ";
79 print_feed_select($link, "feed_id", $feed_id);
80
81 print "</div>";
82
ecace165 83 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
10fa6615
AD
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
ceb30ba4 100 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
10fa6615 101
143a4973
AD
102 print "<span id=\"filter_dlg_param_box\" style=\"$param_hidden\">";
103 print " " . __("with parameters:") . " ";
ceb30ba4
AD
104
105 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
106
107 print "<input size=\"20\" style=\"$param_int_hidden\"
074bf20c 108 onkeypress=\"return filterCR(event, filterEditSave)\"
143a4973 109 name=\"action_param\" value=\"$action_param\">";
ceb30ba4
AD
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
143a4973 116 print "</span>";
10fa6615 117
143a4973 118 print "&nbsp;"; // tiny layout hack
10fa6615
AD
119
120 print "</div>";
121
122 print "<div class=\"dlgSec\">".__("Options")."</div>";
123 print "<div class=\"dlgSecCont\">";
124
ecace165
AD
125 print "<div style=\"line-height : 100%\">";
126
10fa6615
AD
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>";
ecace165 146 print "</div>";
10fa6615
AD
147
148 print "<div class=\"dlgButtons\">";
ef8be8ea 149
ddb61e79 150 $reg_exp = htmlspecialchars($reg_exp, ENT_QUOTES); // second escaping seems to be needed for javascript
143a4973
AD
151
152 print "<div style=\"float : left\">";
577399e8
AD
153 print "<button onclick='return removeFilter($filter_id, \"$reg_exp\")'>".
154 __('Remove')."</button>";
143a4973
AD
155 print "</div>";
156
577399e8
AD
157 print "<button onclick=\"return filterEditSave()\">".
158 __('Save')."</button> ";
ef8be8ea 159
577399e8
AD
160 print "<button onclick=\"return filterEditCancel()\">".
161 __('Cancel')."</button>";
ef8be8ea 162
951906dc 163 print "]]></content></dlg>";
ef8be8ea
AD
164
165 return;
166 }
167
168
169 if ($subop == "editSave") {
170
b8ffa322
AD
171 global $memcache;
172
173 if ($memcache) $memcache->flush();
174
b4e75b2a
AD
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"]));
ef8be8ea 184
44d0e774 185 # for the time being, no other filters use params anyway...
b4e75b2a 186 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
44d0e774 187
ef8be8ea
AD
188 if (!$feed_id) {
189 $feed_id = 'NULL';
190 } else {
191 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
192 }
ceb30ba4
AD
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
ef8be8ea
AD
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',
073ca0e6 206 enabled = $enabled,
3f2ff803 207 inverse = $inverse,
44d0e774
AD
208 action_param = '$action_param',
209 filter_param = '$filter_param'
4dccf1ed 210 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
ef8be8ea
AD
211 }
212
213 if ($subop == "remove") {
a522a767
AD
214
215 if ($memcache) $memcache->flush();
ef8be8ea 216
b4e75b2a 217 $ids = split(",", db_escape_string($_REQUEST["ids"]));
ef8be8ea 218
4dccf1ed
AD
219 foreach ($ids as $id) {
220 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
ef8be8ea
AD
221 }
222 }
223
224 if ($subop == "add") {
a522a767
AD
225
226 if ($memcache) $memcache->flush();
227
b4e75b2a
AD
228 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
229 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
230 $feed_id = db_escape_string($_REQUEST["feed_id"]);
231 $action_id = db_escape_string($_REQUEST["action_id"]);
232 $action_param = db_escape_string($_REQUEST["action_param"]);
233 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
234 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
4dccf1ed 235
44d0e774 236 # for the time being, no other filters use params anyway...
b4e75b2a 237 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
44d0e774 238
4dccf1ed
AD
239 if (!$regexp) return;
240
241 if (!$feed_id) {
242 $feed_id = 'NULL';
243 } else {
244 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
245 }
246
db34a63e
AD
247 /* When processing 'assign label' filters, action_param_label dropbox
248 * overrides action_param */
249
250 if ($action_id == 7) {
251 $action_param = $action_param_label;
252 }
253
4dccf1ed
AD
254 $result = db_query($link,
255 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
a8732d0c 256 action_id, action_param, inverse, filter_param)
4dccf1ed
AD
257 VALUES
258 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
a8732d0c 259 $feed_id, '$action_id', '$action_param', $inverse, '$filter_param')");
4dccf1ed 260
5e6f933a
AD
261 if (db_affected_rows($link, $result) != 0) {
262 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
263 }
264
265 return;
ef8be8ea
AD
266 }
267
268 if ($quiet) return;
269
fe8d2059
AD
270 set_pref($link, "_PREFS_ACTIVE_TAB", "filterConfig");
271
b4e75b2a 272 $sort = db_escape_string($_REQUEST["sort"]);
ef8be8ea
AD
273
274 if (!$sort || $sort == "undefined") {
275 $sort = "reg_exp";
276 }
277
ef8be8ea
AD
278 $result = db_query($link, "SELECT id,description
279 FROM ttrss_filter_types ORDER BY description");
280
281 $filter_types = array();
282
283 while ($line = db_fetch_assoc($result)) {
284 //array_push($filter_types, $line["description"]);
285 $filter_types[$line["id"]] = $line["description"];
286 }
287
9c87d75c 288
b4e75b2a 289 $filter_search = db_escape_string($_REQUEST["search"]);
9c87d75c 290
b4e75b2a 291 if (array_key_exists("search", $_REQUEST)) {
9c87d75c
AD
292 $_SESSION["prefs_filter_search"] = $filter_search;
293 } else {
294 $filter_search = $_SESSION["prefs_filter_search"];
295 }
8df7184c
AD
296
297 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
298 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
299 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
9c87d75c 300
8df7184c 301 print "<div style='float : right; padding-right : 4px;'>
9c87d75c 302 <input id=\"filter_search\" size=\"20\" type=\"search\"
4cf6fc6a
AD
303 onfocus=\"javascript:disableHotkeys();\"
304 onblur=\"javascript:enableHotkeys();\"
9c87d75c 305 onchange=\"javascript:updateFilterList()\" value=\"$filter_search\">
577399e8 306 <button onclick=\"javascript:updateFilterList()\">".__('Search')."</button>
577399e8 307 </div>";
9c87d75c 308
3393238d 309 print "<button onclick=\"return quickAddFilter()\">".
577399e8 310 __('Create filter')."</button> ";
0d32b41e 311
577399e8
AD
312 print "<button onclick=\"return editSelectedFilter()\">".
313 __('Edit')."</button> ";
ef8be8ea 314
577399e8
AD
315 print "<button onclick=\"return removeSelectedFilters()\">".
316 __('Remove')."</button> ";
1e5548db 317
fe1c99af
AD
318 if (defined('_ENABLE_FEED_DEBUGGING')) {
319 print "<button onclick=\"rescore_all_feeds()\">".
320 __('Rescore articles')."</button> ";
321 }
95a948a5 322
8df7184c
AD
323 print "</div>"; # toolbar
324 print "</div>"; # toolbar-frame
325 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
326
9c87d75c 327 if ($filter_search) {
1638fc4e
AD
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
9c87d75c
AD
345 } else {
346 $filter_search_query = "";
347 }
348
ef8be8ea
AD
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,
3f2ff803 354 inverse,
ef8be8ea 355 feed_id,
7f94284b 356 filter_param,
1b74062f 357 filter_type,
ef8be8ea 358 ttrss_filter_actions.description AS action_description,
ff6e357a
AD
359 ttrss_feeds.title AS feed_title,
360 ttrss_filter_actions.name AS action_name,
361 ttrss_filters.action_param AS action_param
ef8be8ea
AD
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
9c87d75c 367 $filter_search_query
ef8be8ea
AD
368 ttrss_filter_actions.id = action_id AND
369 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
a82dc5e4 370 ORDER by action_description, $sort");
ef8be8ea
AD
371
372 if (db_num_rows($result) != 0) {
373
ef8be8ea
AD
374 print "<p><table width=\"100%\" cellspacing=\"0\" class=\"prefFilterList\"
375 id=\"prefFilterList\">";
376
377 print "<tr><td class=\"selectPrompt\" colspan=\"8\">
e8d0177d 378 ".__('Select:')."
e69fb880
AD
379 <a href=\"#\" onclick=\"selectTableRows('prefFilterList', 'all')\">".__('All')."</a>,
380 <a href=\"#\" onclick=\"selectTableRows('prefFilterList', 'none')\">".__('None')."</a>
ef8be8ea
AD
381 </td</tr>";
382
ef8be8ea 383 $lnum = 0;
a82dc5e4
AD
384
385 $cur_action_description = "";
386
ef8be8ea
AD
387 while ($line = db_fetch_assoc($result)) {
388
ef8be8ea 389 $filter_id = $line["id"];
b4e75b2a 390 $edit_filter_id = $_REQUEST["id"];
ef8be8ea
AD
391
392 $enabled = sql_bool_to_bool($line["enabled"]);
3f2ff803
AD
393 $inverse = sql_bool_to_bool($line["inverse"]);
394
ef9cab23 395 $this_row_id = "id=\"FILRR-$filter_id\"";
a82dc5e4
AD
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>
a998dbef
AD
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>";
a82dc5e4 411
ef9cab23 412 $lnum = 0;
a82dc5e4
AD
413 }
414
ef9cab23
AD
415 $class = ($lnum % 2) ? "even" : "odd";
416
ef8be8ea
AD
417 print "<tr class=\"$class\" $this_row_id>";
418
47439031 419 $line["reg_exp"] = htmlspecialchars($line["reg_exp"]);
ef8be8ea 420
a3c159c4 421 if (!$line["feed_title"]) $line["feed_title"] = __("All feeds");
0966ea37 422
799e1b39
AD
423 if (array_search($line["action_name"],
424 array("score", "tag", "label")) === false) {
425
426 $line["action_param"] = false;
427 }
428
0966ea37 429 if (!$line["action_param"]) {
08e618f7 430 $line["action_param"] = "&mdash;";
0966ea37
AD
431 } else if ($line["action_name"] == "score") {
432
883fee8d
AD
433 $score_pic = theme_image($link,
434 "images/" . get_score_pic($line["action_param"]));
0966ea37 435
883fee8d 436 $score_pic = "<img class='hlScorePic' src=\"$score_pic\">";
0966ea37
AD
437
438 $line["action_param"] = "$score_pic " . $line["action_param"];
439
440 }
ef8be8ea 441
47439031 442 $line["feed_title"] = htmlspecialchars($line["feed_title"]);
ef8be8ea 443
a998dbef 444 print "<td align='center'><input onclick='toggleSelectRow(this);'
ef8be8ea
AD
445 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
446
7f94284b
AD
447 $filter_params = array(
448 "before" => __("before"),
449 "after" => __("after"));
a970edb1
AD
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
7f94284b
AD
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
ef8be8ea
AD
481 if (!$enabled) {
482 $line["reg_exp"] = "<span class=\"insensitive\">" .
a3c159c4 483 $line["reg_exp"] . " " . __("(Disabled)")."</span>";
ef8be8ea
AD
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>";
a82dc5e4
AD
490 $line["action_param"] = "<span class=\"insensitive\">" .
491 $line["action_param"] . "</span>";
ef8be8ea 492 }
a95da136 493
a998dbef 494 $onclick = "onclick='editFilter($filter_id, event)' title='".__('Click to edit')."'";
a95da136 495
3f2ff803
AD
496 $inverse_label = "";
497
498 if ($inverse) {
a3c159c4 499 $inverse_label = " <span class='insensitive'>".__('(Inverse)')."</span>";
3f2ff803 500 }
1b74062f
AD
501
502 print "<td $onclick>" . $line["reg_exp"] . "$inverse_label</td>";
503 print "<td $onclick>" . $line["feed_title"] . "</td>";
ef8be8ea 504
1b74062f 505 print "<td $onclick>" . $line["filter_type_descr"] . "</td>";
08e618f7 506 print "<td $onclick>" . $line["action_param"] . "</td>";
ff6e357a 507
ef8be8ea
AD
508 print "</tr>";
509
510 ++$lnum;
511 }
a5bd7bf0 512
ef8be8ea
AD
513 print "</table>";
514
ef8be8ea
AD
515 } else {
516
9c87d75c 517 print "<p>";
9c87d75c 518 if (!$filter_search) {
cea04d61 519 print_warning(__('No filters defined.'));
9c87d75c 520 } else {
cea04d61 521 print_warning(__('No matching filters found.'));
9c87d75c 522 }
9c87d75c 523 print "</p>";
ef8be8ea
AD
524
525 }
8df7184c
AD
526
527 print "</div>"; #pane
528 print "</div>"; #container
ef8be8ea 529 }
ceb30ba4 530
ef8be8ea 531?>