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