]> git.wh0rd.org Git - tt-rss.git/blob - modules/pref-filters.php
rework feed dialog layouts
[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                         print "<div style=\"line-height : 100%\">";
158
159                         if ($enabled) {
160                                 $checked = "checked";
161                         } else {
162                                 $checked = "";
163                         }
164
165                         print "<input type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
166                                         <label for=\"enabled\">".__('Enabled')."</label><br/>";
167
168                         if ($inverse) {
169                                 $checked = "checked";
170                         } else {
171                                 $checked = "";
172                         }
173
174                         print "<input type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
175                                 <label for=\"inverse\">".__('Inverse match')."</label>";
176
177                         print "</div>";
178                         print "</div>";
179
180                         print "<div class=\"dlgButtons\">";
181
182                         print "<input type=\"submit\" 
183                                 id=\"infobox_submit\"
184                                 class=\"button\" onclick=\"return filterEditSave()\" 
185                                 value=\"".__('Save')."\"> ";
186
187                         print "<input class=\"button\"
188                                 type=\"submit\" onclick=\"return filterEditCancel()\" 
189                                 value=\"".__('Cancel')."\">";
190
191                         print "</div>";
192
193                         return;
194                 }
195
196
197                 if ($subop == "editSave") {
198
199                         $reg_exp = db_escape_string(trim($_GET["reg_exp"]));
200                         $filter_type = db_escape_string(trim($_GET["filter_type"]));
201                         $filter_id = db_escape_string($_GET["id"]);
202                         $feed_id = db_escape_string($_GET["feed_id"]);
203                         $action_id = db_escape_string($_GET["action_id"]); 
204                         $action_param = db_escape_string($_GET["action_param"]); 
205                         $enabled = checkbox_to_sql_bool(db_escape_string($_GET["enabled"]));
206                         $inverse = checkbox_to_sql_bool(db_escape_string($_GET["inverse"]));
207
208                         if (!$feed_id) {
209                                 $feed_id = 'NULL';
210                         } else {
211                                 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
212                         }
213                         
214                         $result = db_query($link, "UPDATE ttrss_filters SET 
215                                         reg_exp = '$reg_exp', 
216                                         feed_id = $feed_id,
217                                         action_id = '$action_id',
218                                         filter_type = '$filter_type',
219                                         enabled = $enabled,
220                                         inverse = $inverse,
221                                         action_param = '$action_param'
222                                         WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
223
224                         if (db_affected_rows($link, $result) != 0) {
225                                 print_notice(T_sprintf("Saved filter <b>%s</b>", htmlspecialchars($reg_exp)));
226                         }
227
228                 }
229
230                 if ($subop == "remove") {
231
232                         $ids = split(",", db_escape_string($_GET["ids"]));
233
234                         foreach ($ids as $id) {
235                                 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
236                         }
237                 }
238
239                 if ($subop == "add") {
240                 
241                         $regexp = db_escape_string(trim($_GET["reg_exp"]));
242                         $filter_type = db_escape_string(trim($_GET["filter_type"]));
243                         $feed_id = db_escape_string($_GET["feed_id"]);
244                         $action_id = db_escape_string($_GET["action_id"]); 
245                         $action_param = db_escape_string($_GET["action_param"]); 
246
247                         $inverse = checkbox_to_sql_bool(db_escape_string($_GET["inverse"]));
248
249                         if (!$regexp) return;
250
251                         if (!$feed_id) {
252                                 $feed_id = 'NULL';
253                         } else {
254                                 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
255                         }
256
257                         $result = db_query($link,
258                                 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
259                                         action_id, action_param, inverse) 
260                                 VALUES 
261                                         ('$regexp', '$filter_type','".$_SESSION["uid"]."', 
262                                                 $feed_id, '$action_id', '$action_param', $inverse)");
263
264                         if (db_affected_rows($link, $result) != 0) {
265                                 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
266                         }
267
268                         return;
269                 }
270
271                 if ($quiet) return;
272
273                 set_pref($link, "_PREFS_ACTIVE_TAB", "filterConfig");
274
275                 $sort = db_escape_string($_GET["sort"]);
276
277                 if (!$sort || $sort == "undefined") {
278                         $sort = "reg_exp";
279                 }
280
281 //              print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
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($_GET["search"]);
295
296                 if (array_key_exists("search", $_GET)) {
297                         $_SESSION["prefs_filter_search"] = $filter_search;
298                 } else {
299                         $filter_search = $_SESSION["prefs_filter_search"];
300                 }
301
302                 print "<div class=\"feedEditSearch\">
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                         <input type=\"submit\" class=\"button\" 
308                         onclick=\"javascript:updateFilterList()\" value=\"".__('Search')."\">
309                         <p<a class='helpLinkPic' href=\"javascript:displayHelpInfobox(2)\">
310                         <img src='images/sign_quest.gif'></a></p>
311                         </div>";
312
313
314                 print "<input type=\"submit\" 
315                         class=\"button\" 
316                         onclick=\"return displayDlg('quickAddFilter', false)\" 
317                         id=\"create_filter_btn\"
318                         value=\"".__('Create filter')."\">"; 
319
320                 print "&nbsp;";
321
322                 print "<input type=\"submit\" 
323                         class=\"button\" 
324                         onclick=\"rescore_all_feeds()\" 
325                         value=\"".__('Rescore articles')."\">"; 
326
327                 if ($filter_search) {
328                         $filter_search = db_escape_string($filter_search);
329                         $filter_search_query = "(
330                                 UPPER(ttrss_filter_actions.description) LIKE UPPER('%$filter_search%') OR 
331                                 UPPER(reg_exp) LIKE UPPER('%$filter_search%') OR 
332                                 UPPER(ttrss_feeds.title) LIKE UPPER('%$filter_search%') OR
333                                 UPPER(ttrss_filter_types.description) LIKE UPPER('%$filter_search%')) AND";
334                 } else {
335                         $filter_search_query = "";
336                 }
337
338                 $result = db_query($link, "SELECT 
339                                 ttrss_filters.id AS id,reg_exp,
340                                 ttrss_filter_types.name AS filter_type_name,
341                                 ttrss_filter_types.description AS filter_type_descr,
342                                 enabled,
343                                 inverse,
344                                 feed_id,
345                                 ttrss_filter_actions.description AS action_description,
346                                 ttrss_feeds.title AS feed_title,
347                                 ttrss_filter_actions.name AS action_name,
348                                 ttrss_filters.action_param AS action_param
349                         FROM 
350                                 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
351                                         ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
352                         WHERE
353                                 filter_type = ttrss_filter_types.id AND
354                                 $filter_search_query
355                                 ttrss_filter_actions.id = action_id AND
356                                 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
357                         ORDER by action_description, $sort");
358
359                 if (db_num_rows($result) != 0) {
360
361                         print "<p><table width=\"100%\" cellspacing=\"0\" class=\"prefFilterList\" 
362                                 id=\"prefFilterList\">";
363
364                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
365                                 ".__('Select:')." 
366                                         <a href=\"javascript:selectPrefRows('filter', true)\">".__('All')."</a>,
367                                         <a href=\"javascript:selectPrefRows('filter', false)\">".__('None')."</a>
368                                 </td</tr>";
369
370                         $lnum = 0;
371
372                         $cur_action_description = "";
373
374                         while ($line = db_fetch_assoc($result)) {
375         
376                                 $class = ($lnum % 2) ? "even" : "odd";
377         
378                                 $filter_id = $line["id"];
379                                 $edit_filter_id = $_GET["id"];
380
381                                 $enabled = sql_bool_to_bool($line["enabled"]);
382                                 $inverse = sql_bool_to_bool($line["inverse"]);
383
384                                 if ($subop == "edit" && $filter_id != $edit_filter_id) {
385                                         $class .= "Grayed";
386                                         $this_row_id = "";
387                                 } else {
388                                         $this_row_id = "id=\"FILRR-$filter_id\"";
389                                 }
390
391                                 $line["filter_type_descr"] = __($line["filter_type_descr"]);
392                                 $line["action_description"] = __($line["action_description"]);
393
394                                 if ($line["action_description"] != $cur_action_description) {
395                                         $cur_action_description = $line["action_description"];
396
397                                         print "<tr><td class='filterEditCat' colspan='6'>$cur_action_description</td></tr>";
398
399                                         print "<tr class=\"title\">
400                                                 <td align='center' width=\"5%\">&nbsp;</td>
401                                                 <td width=\"20%\"><a href=\"javascript:updateFilterList('reg_exp')\">".__('Filter expression')."</a></td>
402                                                 <td width=\"\"><a href=\"javascript:updateFilterList('feed_title')\">".__('Feed')."</a></td>
403                                                 <td width=\"20%\"><a href=\"javascript:updateFilterList('filter_type')\">".__('Match')."</a></td>
404                                                 <!-- <td width=\"15%\"><a href=\"javascript:updateFilterList('action_description')\">".__('Action')."</a></td> -->
405                                                 <td width=\"20%\"><a href=\"javascript:updateFilterList('action_param')\">".__('Params')."</a></td>"; 
406
407                                 }
408
409                                 print "<tr class=\"$class\" $this_row_id>";
410         
411                                 $line["reg_exp"] = htmlspecialchars($line["reg_exp"]);
412         
413                                 if (!$line["feed_title"]) $line["feed_title"] = __("All feeds");
414
415                                 if (!$line["action_param"]) {
416                                         $line["action_param"] = "&mdash;";
417                                 } else if ($line["action_name"] == "score") {
418
419                                         $score_pic = get_score_pic($line["action_param"]);
420
421                                         $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\">";
422
423                                         $line["action_param"] = "$score_pic " . $line["action_param"];
424
425                                 }
426
427                                 $line["feed_title"] = htmlspecialchars($line["feed_title"]);
428
429                                 print "<td align='center'><input onclick='toggleSelectPrefRow(this, \"filter\");' 
430                                         type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
431
432                                 if (!$enabled) {
433                                         $line["reg_exp"] = "<span class=\"insensitive\">" . 
434                                                 $line["reg_exp"] . " " .  __("(Disabled)")."</span>";
435                                         $line["feed_title"] = "<span class=\"insensitive\">" . 
436                                                 $line["feed_title"] . "</span>";
437                                         $line["filter_type_descr"] = "<span class=\"insensitive\">" . 
438                                                 $line["filter_type_descr"] . "</span>";
439                                         $line["action_description"] = "<span class=\"insensitive\">" . 
440                                                 $line["action_description"] . "</span>";
441                                         $line["action_param"] = "<span class=\"insensitive\">" . 
442                                                 $line["action_param"] . "</span>";
443                                 }       
444
445                                 $onclick = "onclick='editFilter($filter_id)' title='".__('Click to edit')."'";
446
447                                 print "<td $onclick>" . $line["reg_exp"] . "</td>";             
448                                 print "<td $onclick>" . $line["feed_title"] . "</td>";                  
449
450                                 $inverse_label = "";
451
452                                 if ($inverse) {
453                                         $inverse_label = " <span class='insensitive'>".__('(Inverse)')."</span>";
454                                 }
455         
456                                 print "<td $onclick>" . $line["filter_type_descr"] . "$inverse_label</td>";
457                                 print "<td $onclick>" . $line["action_param"] . "</td>";
458
459                                 print "</tr>";
460         
461                                 ++$lnum;
462                         }
463
464                         print "</table>";
465
466                         print "<p id=\"filterOpToolbar\">";
467
468                         print "<input type=\"submit\" class=\"button\" disabled=\"true\"
469                                         onclick=\"return editSelectedFilter()\" value=\"".__('Edit')."\">
470                                 <input type=\"submit\" class=\"button\" disabled=\"true\"
471                                         onclick=\"return removeSelectedFilters()\" value=\"".__('Remove')."\">";
472
473                         print "</p>";
474
475                 } else {
476
477                         print "<p>";
478                         if (!$filter_search) {
479                                 print __('No filters defined.');
480                         } else {
481                                 print __('No matching filters found.');
482                         }
483                         print "</p>";
484
485                 }
486         }
487 ?>