]> git.wh0rd.org Git - tt-rss.git/blob - modules/pref-labels.php
js: misc fixes, code cleanup
[tt-rss.git] / modules / pref-labels.php
1 <?php
2         function module_pref_labels($link) {
3
4                 $subop = $_REQUEST["subop"];
5
6                 if ($subop == "color-set") {
7                         $kind = db_escape_string($_REQUEST["kind"]);
8                         $ids = split(',', db_escape_string($_REQUEST["ids"]));
9                         $color = db_escape_string($_REQUEST["color"]);
10                         $fg = db_escape_string($_REQUEST["fg"]);
11                         $bg = db_escape_string($_REQUEST["bg"]);
12
13                         foreach ($ids as $id) {
14
15                                 if ($kind == "fg" || $kind == "bg") {
16                                         db_query($link, "UPDATE ttrss_labels2 SET
17                                                 ${kind}_color = '$color' WHERE id = '$id'
18                                                 AND owner_uid = " . $_SESSION["uid"]);                  
19                                 } else {
20                                         db_query($link, "UPDATE ttrss_labels2 SET
21                                                 fg_color = '$fg', bg_color = '$bg' WHERE id = '$id'
22                                                 AND owner_uid = " . $_SESSION["uid"]);                  
23                                 }
24
25                                 $caption = db_escape_string(label_find_caption($link, $id, $_SESSION["uid"]));
26
27                                 /* Remove cached data */
28
29                                 db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
30                                         WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
31
32                         }
33
34                         return;
35                 }
36
37                 if ($subop == "color-reset") {
38                         $ids = split(',', db_escape_string($_REQUEST["ids"]));
39
40                         foreach ($ids as $id) {
41                                 db_query($link, "UPDATE ttrss_labels2 SET
42                                         fg_color = '', bg_color = '' WHERE id = '$id'
43                                         AND owner_uid = " . $_SESSION["uid"]);                  
44
45                                 $caption = db_escape_string(label_find_caption($link, $id, $_SESSION["uid"]));
46
47                                 /* Remove cached data */
48
49                                 db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
50                                         WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
51                         }
52
53                 }
54
55                 if ($subop == "save") {
56
57                         $id = db_escape_string($_REQUEST["id"]);
58                         $caption = db_escape_string(trim($_REQUEST["value"]));
59
60                         db_query($link, "BEGIN");
61
62                         $result = db_query($link, "SELECT caption FROM ttrss_labels2
63                                 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
64
65                         if (db_num_rows($result) != 0) {
66                                 $old_caption = db_fetch_result($result, 0, "caption");
67
68                                 $result = db_query($link, "SELECT id FROM ttrss_labels2
69                                         WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
70
71                                 if (db_num_rows($result) == 0) {
72                                         if ($caption) {
73                                                 $result = db_query($link, "UPDATE ttrss_labels2 SET
74                                                         caption = '$caption' WHERE id = '$id' AND
75                                                         owner_uid = " . $_SESSION["uid"]);
76
77                                                 /* Update filters that reference label being renamed */
78
79                                                 $old_caption = db_escape_string($old_caption);
80
81                                                 db_query($link, "UPDATE ttrss_filters SET
82                                                         action_param = '$caption' WHERE action_param = '$old_caption'
83                                                         AND action_id = 7
84                                                         AND owner_uid = " . $_SESSION["uid"]);
85
86                                                 print $_REQUEST["value"];
87                                         } else {
88                                                 print $old_caption;
89                                         }
90                                 } else {
91                                         print $old_caption;
92                                 }
93                         }
94
95                         db_query($link, "COMMIT");
96
97                         return;
98                 }
99
100                 if ($subop == "remove") {
101
102                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
103
104                         foreach ($ids as $id) {
105                                 label_remove($link, $id, $_SESSION["uid"]);
106                         }
107
108                 }
109
110                 if ($subop == "add") {
111                         $caption = db_escape_string($_REQUEST["caption"]);
112                         $output = db_escape_string($_REQUEST["output"]);
113
114                         if ($caption) {
115
116                                 if (label_create($link, $caption)) {
117                                         if (!$output) {
118                                                 print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
119                                         }
120                                 }
121
122                                 if ($output == "select") {
123                                         header("Content-Type: text/xml");
124
125                                         print "<rpc-reply><payload>";
126
127                                         print_label_select($link, "select_label", 
128                                                 $caption, "");
129
130                                         print "</payload></rpc-reply>";
131                                 }
132                         }
133
134                         return;
135                 }
136
137                 set_pref($link, "_PREFS_ACTIVE_TAB", "labelConfig");
138
139                 $sort = db_escape_string($_REQUEST["sort"]);
140
141                 if (!$sort || $sort == "undefined") {
142                         $sort = "caption";
143                 }
144
145                 $label_search = db_escape_string($_REQUEST["search"]);
146
147                 if (array_key_exists("search", $_REQUEST)) {
148                         $_SESSION["prefs_label_search"] = $label_search;
149                 } else {
150                         $label_search = $_SESSION["prefs_label_search"];
151                 }
152
153                 print "<div style='float : right'>
154                         <input id=\"label_search\" size=\"20\" type=\"search\"
155                                 onfocus=\"javascript:disableHotkeys();\" 
156                                 onblur=\"javascript:enableHotkeys();\"
157                                 onchange=\"javascript:updateLabelList()\" value=\"$label_search\">
158                         <button onclick=\"javascript:updateLabelList()\">".__('Search')."</button>
159                         </div>";
160
161                 print "<div class=\"prefGenericAddBox\">";
162
163                 print"<button onclick=\"return addLabel()\">".
164                         __('Create label')."</button> ";
165
166                 print "<button onclick=\"javascript:removeSelectedLabels()\">".
167                         __('Remove')."</button> ";
168
169                 print "<button onclick=\"labelColorReset()\">".
170                         __('Clear colors')."</button>";
171
172
173                 print "</div>";
174
175                 if ($label_search) {
176
177                         $label_search = split(" ", $label_search);
178                         $tokens = array();
179
180                         foreach ($label_search as $token) {
181
182                                 $token = trim($token);
183                                 array_push($tokens, "(UPPER(caption) LIKE UPPER('%$token%'))");
184
185                         }
186
187                         $label_search_query = "(" . join($tokens, " AND ") . ") AND ";
188                         
189                 } else {
190                         $label_search_query = "";
191                 }
192
193                 $result = db_query($link, "SELECT 
194                                 *
195                         FROM 
196                                 ttrss_labels2
197                         WHERE 
198                                 $label_search_query
199                                 owner_uid = ".$_SESSION["uid"]."
200                         ORDER BY $sort");
201
202                 if (db_num_rows($result) != 0) {
203
204                         print "<p><table width=\"100%\" cellspacing=\"0\" 
205                                 class=\"prefLabelList\" id=\"prefLabelList\">";
206
207                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">
208                                 ".__('Select:')." 
209                                         <a href=\"#\" onclick=\"selectTableRows('prefLabelList', 'all')\">".__('All')."</a>,
210                                         <a href=\"#\" onclick=\"selectTableRows('prefLabelList', 'none')\">".__('None')."</a>
211                                 </td</tr>";
212
213                         $lnum = 0;
214                         
215                         while ($line = db_fetch_assoc($result)) {
216         
217                                 $class = ($lnum % 2) ? "even" : "odd";
218         
219                                 $label_id = $line["id"];
220                                 $this_row_id = "id=\"LILRR-$label_id\"";
221
222                                 print "<tr class=\"$class\" $this_row_id>";
223         
224                                 $line["caption"] = htmlspecialchars($line["caption"]);
225
226                                 $fg_color = $line["fg_color"];
227                                 $bg_color = $line["bg_color"];
228
229                                 if (!$fg_color) $fg_color = "";
230                                 if (!$bg_color) $bg_color = "";
231
232                                 print "<td width='5%' align='center'><input 
233                                         onclick='toggleSelectRow(this);' 
234                                         type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
235         
236                                 $id = $line['id'];
237
238                                 print "<td>";
239
240                                 print "<div class='labelColorIndicator' id='LICID-$id' 
241                                         style='color : $fg_color; background-color : $bg_color'
242                                         title='".__('Click to change color')."'
243                                         onclick=\"colorPicker('$id', '$fg_color', '$bg_color')\">&alpha;";
244                                 print_color_picker($id);
245                                 print "</div>";
246
247
248                                 print "<span class='prefsLabelEntry' 
249                                         id=\"LILT-".$line["id"]."\">" . $line["caption"] . 
250                                         "</span>";
251
252                                 print "</td>";
253
254                                 print "</tr>";
255         
256                                 ++$lnum;
257                         }
258
259                         print "</table>";
260         
261
262                 } else {
263                         print "<p>";
264                         if (!$label_search) {
265                                 print_warning(__('No labels defined.'));
266                         } else {
267                                 print_warning(__('No matching labels found.'));
268                         }
269                         print "</p>";
270
271                 }
272         }
273
274         function print_color_picker($id) {
275
276                 print "<div id=\"colorPicker-$id\" 
277                         onmouseover=\"colorPickerActive(true)\"
278                         onmouseout=\"colorPickerActive(false)\"
279                         class=\"colorPicker\" style='display : none'>";
280
281                 $color_picker_pairs = array(
282                         array('#ff0000', '#ffffff'),
283                         array('#009000', '#ffffff'),
284                         array('#0000ff', '#ffffff'),    
285                         array('#ff00ff', '#ffffff'),                            
286                         array('#009090', '#ffffff'),
287
288                         array('#ffffff', '#ff0000'),
289                         array('#000000', '#00ff00'),
290                         array('#ffffff', '#0000ff'),
291                         array('#ffffff', '#ff00ff'),
292                         array('#000000', '#00ffff'),
293
294                         array('#7b07e1', '#ffffff'),
295                         array('#0091b4', '#ffffff'),
296                         array('#00aa71', '#ffffff'),
297                         array('#7d9e01', '#ffffff'),
298                         array('#e14a00', '#ffffff'),
299
300                         array('#ffffff', '#7b07e1'),
301                         array('#ffffff', '#00b5e1'),
302                         array('#ffffff', '#00e196'),
303                         array('#ffffff', '#b3e100'),
304                         array('#ffffff', '#e14a00'),
305
306                         array('#000000', '#ffffff'),
307                         array('#ffffff', '#000000'),
308                         array('#ffffff', '#909000'),
309                         array('#063064', '#fff7d5'),
310                         array('#ffffff', '#4E4E90'),
311                 );
312
313                 foreach ($color_picker_pairs as $c) { 
314                         $fg_color = $c[0];
315                         $bg_color = $c[1];
316
317                         print "<div class='colorPickerEntry' 
318                                 style='color : $fg_color; background-color : $bg_color;'
319                                 onclick=\"colorPickerDo('$id', '$fg_color', '$bg_color')\">&alpha;</div>";
320
321                 }
322
323                 print "<br clear='both'>";
324
325                 print "<br/><b>".__('custom color:')."</b>";
326                 print "<div class=\"ccPrompt\" onclick=\"labelColorAsk('$id', 'fg')\">".__("foreground")."</div>";
327                 print "<div class=\"ccPrompt\" onclick=\"labelColorAsk('$id', 'bg')\">".__("background")."</div>";
328
329                 print "</div>";
330         }
331
332 ?>