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