]> git.wh0rd.org Git - tt-rss.git/blob - modules/pref-filters.php
fix label and filter editors for safari (borked markup)
[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_unescape_string(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\">";
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 //                      print "<div class=\"notice\"><b>Note:</b> filter will only apply to new articles.</div>";
32                         
33                         $result = db_query($link, "SELECT id,description 
34                                 FROM ttrss_filter_types ORDER BY description");
35         
36                         $filter_types = array();
37         
38                         while ($line = db_fetch_assoc($result)) {
39                                 //array_push($filter_types, $line["description"]);
40                                 $filter_types[$line["id"]] = $line["description"];
41                         }
42
43                         print "<table width='100%'>";
44
45                         print "<tr><td>Match:</td>
46                                 <td><input onkeypress=\"return filterCR(event, filterEditSave)\"
47                                          onkeyup=\"toggleSubmitNotEmpty(this, 'infobox_submit')\"
48                                          onchange=\"toggleSubmitNotEmpty(this, 'infobox_submit')\"
49                                          name=\"reg_exp\" class=\"iedit\" value=\"$reg_exp\">";
50                         
51                         print "</td></tr><tr><td>On field:</td><td>";
52                         
53                         print_select_hash("filter_type", $filter_type, $filter_types, "class=\"_iedit\"");      
54         
55                         print "</td></tr>";
56                         print "<tr><td>Feed:</td><td colspan='2'>";
57
58                         print_feed_select($link, "feed_id", $feed_id);
59                         
60                         print "</td></tr>";
61         
62                         print "<tr><td>Action:</td>";
63         
64                         print "<td colspan='2'><select name=\"action_id\"
65                                 onchange=\"filterDlgCheckAction(this)\">";
66         
67                         $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions 
68                                 ORDER BY name");
69
70                         while ($line = db_fetch_assoc($result)) {
71                                 $is_sel = ($line["id"] == $action_id) ? "selected" : "";                        
72                                 printf("<option value='%d' $is_sel>%s</option>", $line["id"], $line["description"]);
73                         }
74         
75                         print "</select>";
76
77                         print "</td></tr>";
78
79                         print "<tr><td>Params:</td>";
80
81                         $param_disabled = ($action_id == 4) ? "" : "disabled";
82
83                         print "<td><input $param_disabled class='iedit' 
84                                 name=\"action_param\" value=\"$action_param\"></td></tr>";
85
86                         if ($enabled) {
87                                 $checked = "checked";
88                         } else {
89                                 $checked = "";
90                         }
91
92                         print "<tr><td valign='top'>Options:</td><td>
93                                         <input type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
94                                         <label for=\"enabled\">Enabled</label><br/>";
95
96                         if ($inverse) {
97                                 $checked = "checked";
98                         } else {
99                                 $checked = "";
100                         }
101
102                         print "<input type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
103                                 <label for=\"inverse\">Inverse match</label>";
104
105                         print "</td></tr></table>";
106
107                         print "</form>";
108
109                         print "<div align='right'>";
110
111                         print "<input type=\"submit\" 
112                                 id=\"infobox_submit\"
113                                 class=\"button\" onclick=\"return filterEditSave()\" 
114                                 value=\"Save\"> ";
115
116                         print "<input class=\"button\"
117                                 type=\"submit\" onclick=\"return filterEditCancel()\" 
118                                 value=\"Cancel\">";
119
120                         print "</div>";
121
122                         return;
123                 }
124
125
126                 if ($subop == "editSave") {
127
128                         $reg_exp = db_escape_string(trim($_GET["reg_exp"]));
129                         $filter_type = db_escape_string(trim($_GET["filter_type"]));
130                         $filter_id = db_escape_string($_GET["id"]);
131                         $feed_id = db_escape_string($_GET["feed_id"]);
132                         $action_id = db_escape_string($_GET["action_id"]); 
133                         $action_param = db_escape_string($_GET["action_param"]); 
134                         $enabled = checkbox_to_sql_bool(db_escape_string($_GET["enabled"]));
135                         $inverse = checkbox_to_sql_bool(db_escape_string($_GET["inverse"]));
136
137                         if (!$feed_id) {
138                                 $feed_id = 'NULL';
139                         } else {
140                                 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
141                         }
142                         
143                         $result = db_query($link, "UPDATE ttrss_filters SET 
144                                         reg_exp = '$reg_exp', 
145                                         feed_id = $feed_id,
146                                         action_id = '$action_id',
147                                         filter_type = '$filter_type',
148                                         enabled = $enabled,
149                                         inverse = $inverse,
150                                         action_param = '$action_param'
151                                 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
152                 }
153
154                 if ($subop == "remove") {
155
156                         if (!WEB_DEMO_MODE) {
157
158                                 $ids = split(",", db_escape_string($_GET["ids"]));
159
160                                 foreach ($ids as $id) {
161                                         db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
162                                         
163                                 }
164                         }
165                 }
166
167                 if ($subop == "add") {
168                 
169                         if (!WEB_DEMO_MODE) {
170
171                                 $regexp = db_escape_string(trim($_GET["reg_exp"]));
172                                 $filter_type = db_escape_string(trim($_GET["filter_type"]));
173                                 $feed_id = db_escape_string($_GET["feed_id"]);
174                                 $action_id = db_escape_string($_GET["action_id"]); 
175                                 $action_param = db_escape_string($_GET["action_param"]); 
176
177                                 $inverse = checkbox_to_sql_bool(db_escape_string($_GET["inverse"]));
178
179                                 if (!$regexp) return;
180
181                                 if (!$feed_id) {
182                                         $feed_id = 'NULL';
183                                 } else {
184                                         $feed_id = sprintf("'%s'", db_escape_string($feed_id));
185                                 }
186
187                                 $result = db_query($link,
188                                         "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
189                                                 action_id, action_param, inverse) 
190                                         VALUES 
191                                                 ('$regexp', '$filter_type','".$_SESSION["uid"]."', 
192                                                         $feed_id, '$action_id', '$action_param', $inverse)");
193                         } 
194                 }
195
196                 if ($quiet) return;
197
198                 $sort = db_escape_string($_GET["sort"]);
199
200                 if (!$sort || $sort == "undefined") {
201                         $sort = "reg_exp";
202                 }
203
204 //              print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
205
206                 $result = db_query($link, "SELECT id,description 
207                         FROM ttrss_filter_types ORDER BY description");
208
209                 $filter_types = array();
210
211                 while ($line = db_fetch_assoc($result)) {
212                         //array_push($filter_types, $line["description"]);
213                         $filter_types[$line["id"]] = $line["description"];
214                 }
215
216                 print "<input type=\"submit\" 
217                         class=\"button\" 
218                         onclick=\"return displayDlg('quickAddFilter', false)\" 
219                         id=\"create_filter_btn\"
220                         value=\"Create filter\">"; 
221
222                 $result = db_query($link, "SELECT 
223                                 ttrss_filters.id AS id,reg_exp,
224                                 ttrss_filter_types.name AS filter_type_name,
225                                 ttrss_filter_types.description AS filter_type_descr,
226                                 enabled,
227                                 inverse,
228                                 feed_id,
229                                 ttrss_filter_actions.description AS action_description,
230                                 ttrss_feeds.title AS feed_title
231                         FROM 
232                                 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
233                                         ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
234                         WHERE
235                                 filter_type = ttrss_filter_types.id AND
236                                 ttrss_filter_actions.id = action_id AND
237                                 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
238                         ORDER by $sort");
239
240                 if (db_num_rows($result) != 0) {
241
242                         print "<p><table width=\"100%\" cellspacing=\"0\" class=\"prefFilterList\" 
243                                 id=\"prefFilterList\">";
244
245                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
246                                 Select: 
247                                         <a href=\"javascript:selectPrefRows('filter', true)\">All</a>,
248                                         <a href=\"javascript:selectPrefRows('filter', false)\">None</a>
249                                 </td</tr>";
250
251                         print "<tr class=\"title\">
252                                                 <td align='center' width=\"5%\">&nbsp;</td>
253                                                 <td width=\"20%\"><a href=\"javascript:updateFilterList('reg_exp')\">Filter expression</a></td>
254                                                 <td width=\"20%\"><a href=\"javascript:updateFilterList('feed_title')\">Feed</a></td>
255                                                 <td width=\"15%\"><a href=\"javascript:updateFilterList('filter_type')\">Match</a></td>
256                                                 <td width=\"15%\"><a href=\"javascript:updateFilterList('action_description')\">Action</a></td>";
257
258                         $lnum = 0;
259                         
260                         while ($line = db_fetch_assoc($result)) {
261         
262                                 $class = ($lnum % 2) ? "even" : "odd";
263         
264                                 $filter_id = $line["id"];
265                                 $edit_filter_id = $_GET["id"];
266
267                                 $enabled = sql_bool_to_bool($line["enabled"]);
268                                 $inverse = sql_bool_to_bool($line["inverse"]);
269
270                                 if ($subop == "edit" && $filter_id != $edit_filter_id) {
271                                         $class .= "Grayed";
272                                         $this_row_id = "";
273                                 } else {
274                                         $this_row_id = "id=\"FILRR-$filter_id\"";
275                                 }
276         
277                                 print "<tr class=\"$class\" $this_row_id>";
278         
279                                 $line["reg_exp"] = htmlspecialchars(db_unescape_string($line["reg_exp"]));
280         
281                                 if (!$line["feed_title"]) $line["feed_title"] = "All feeds";
282
283                                 $line["feed_title"] = htmlspecialchars(db_unescape_string($line["feed_title"]));
284
285                                 print "<td align='center'><input onclick='toggleSelectPrefRow(this, \"filter\");' 
286                                         type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
287
288                                 if (!$enabled) {
289                                         $line["reg_exp"] = "<span class=\"insensitive\">" . 
290                                                 $line["reg_exp"] . " (Disabled)</span>";
291                                         $line["feed_title"] = "<span class=\"insensitive\">" . 
292                                                 $line["feed_title"] . "</span>";
293                                         $line["filter_type_descr"] = "<span class=\"insensitive\">" . 
294                                                 $line["filter_type_descr"] . "</span>";
295                                         $line["action_description"] = "<span class=\"insensitive\">" . 
296                                                 $line["action_description"] . "</span>";
297                                 }       
298         
299                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
300                                         $line["reg_exp"] . "</td>";             
301         
302                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
303                                         $line["feed_title"] . "</td>";                  
304
305                                 $inverse_label = "";
306
307                                 if ($inverse) {
308                                         $inverse_label = " <span class='insensitive'>(Inverse)</span>";
309                                 }
310         
311                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
312                                         $line["filter_type_descr"] . "$inverse_label</td>";             
313                 
314                                 print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
315                                         $line["action_description"] . "</td>";                  
316                                 
317                                 print "</tr>";
318         
319                                 ++$lnum;
320                         }
321         
322                         if ($lnum == 0) {
323                                 print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
324                         }
325         
326                         print "</table>";
327
328                         print "<p id=\"filterOpToolbar\">";
329
330                         print "
331                                         Selection:
332                                 <input type=\"submit\" class=\"button\" disabled=\"true\"
333                                         onclick=\"return editSelectedFilter()\" value=\"Edit\">
334                                 <input type=\"submit\" class=\"button\" disabled=\"true\"
335                                         onclick=\"return removeSelectedFilters()\" value=\"Remove\">";
336
337                         print "</p>";
338
339                         print "<div class=\"insensitive\" style=\"float : right\">See 
340                                 <a target=\"_new\" href=\"http://tt-rss.spb.ru/trac/wiki/ContentFilters\">this page</a> 
341                                 for additional  information on filtering</div>";
342
343 /*                      print "<div class=\"insensitive\" style=\"float : right\">
344                                 First matching filter is used, filtering is performed
345                                 when importing articles from the feed.</div>"; */
346
347                 } else {
348
349                         print "<p>No filters defined.</p>";
350
351                 }
352         }
353 ?>