]> git.wh0rd.org Git - tt-rss.git/blob - modules/pref-labels.php
rework label edit dialog
[tt-rss.git] / modules / pref-labels.php
1 <?php
2         function module_pref_labels($link) {
3
4                 $subop = $_REQUEST["subop"];
5
6                 if ($subop == "edit") {
7                         $label_id = db_escape_string($_REQUEST['id']);
8
9                         header("Content-Type: text/xml");
10                         print "<dlg id=\"$subop\">";
11                         print "<title>" . __("Label Editor") . "</title>";
12                         print "<content><![CDATA[";
13
14                         $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
15                                 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
16
17                         $line = db_fetch_assoc($result);
18
19                         print "<form id=\"label_edit_form\" name=\"label_edit_form\"
20                                 onsubmit=\"return false;\">";
21
22                         print "<input type=\"hidden\" name=\"id\" value=\"$label_id\">";
23                         print "<input type=\"hidden\" name=\"op\" value=\"pref-labels\">";
24                         print "<input type=\"hidden\" name=\"subop\" value=\"save\">";
25
26                         print "<div class=\"dlgSec\">".__("Caption")."</div>";
27
28                         print "<div class=\"dlgSecCont\">";
29
30                         print "<input style=\"font-size : 18px\" name=\"caption\" 
31                                 onkeypress=\"return filterCR(event, editLabelSave)\"
32                                 value=\"".htmlspecialchars($line['caption'])."\">";
33
34                         print "</div>";
35                         print "<div class=\"dlgSec\">" . __("Colors") . "</div>";
36                         print "<div class=\"dlgSecCont\">";
37
38                         print "<table cellspacing=\"5\"><th>";
39
40                         print "<tr><td>".__("Foreground:")."</td><td>".__("Background:").
41                                 "</td></tr>";
42
43                         print "</th><tr><td>";
44
45                         $fg_color = $line['fg_color'];
46                         $bg_color = $line['bg_color'];
47
48                         print "<input type=\"hidden\" name=\"fg_color\" value=\"$fg_color\">";
49                         print "<input type=\"hidden\" name=\"bg_color\" value=\"$bg_color\">";
50
51                         print "<div dojoType=\"dijit.ColorPalette\">
52                                 <script type=\"dojo/method\" event=\"onChange\" args=\"fg_color\">
53                                         document.forms['label_edit_form'].fg_color.value = fg_color;
54                                 </script>
55                         </div>";
56                         print "</div>";
57
58                         print "</td><td>";
59
60                         print "<div dojoType=\"dijit.ColorPalette\">
61                                 <script type=\"dojo/method\" event=\"onChange\" args=\"bg_color\">
62                                         document.forms['label_edit_form'].bg_color.value = bg_color;
63                                 </script>
64                         </div>";
65                         print "</div>";
66
67                         print "</td></tr></table>";
68                         print "</div>";
69
70                         print "</form>";
71
72                         print "<div class=\"dlgButtons\">";
73                         print "<button onclick=\"return editLabelSave()\">".
74                                 __('Save')."</button>";
75                         print "<button onclick=\"return closeInfoBox()\">".
76                                 __('Cancel')."</button>";
77                         print "</div>";
78
79                         print "]]></content></dlg>";
80                         return;
81                 }
82
83                 if ($subop == "getlabeltree") {
84                         $root = array();
85                         $root['id'] = 'root';
86                         $root['name'] = __('Labels');
87                         $root['items'] = array();
88
89                         $result = db_query($link, "SELECT *
90                                 FROM ttrss_labels2
91                                 WHERE owner_uid = ".$_SESSION["uid"]."
92                                 ORDER BY caption");
93
94                         while ($line = db_fetch_assoc($result)) {
95                                 $label = array();
96                                 $label['id'] = 'LABEL:' . $line['id'];
97                                 $label['bare_id'] = $line['id'];
98                                 $label['name'] = $line['caption'];
99                                 $label['fg_color'] = $line['fg_color'];
100                                 $label['bg_color'] = $line['bg_color'];
101                                 $label['type'] = 'label';
102                                 $label['checkbox'] = false;
103
104                                 array_push($root['items'], $label);
105                         }
106
107                         $fl = array();
108                         $fl['identifier'] = 'id';
109                         $fl['label'] = 'name';
110                         $fl['items'] = array($root);
111
112                         print json_encode($fl);
113                         return;
114                 }
115
116                 if ($subop == "color-set") {
117                         $kind = db_escape_string($_REQUEST["kind"]);
118                         $ids = split(',', db_escape_string($_REQUEST["ids"]));
119                         $color = db_escape_string($_REQUEST["color"]);
120                         $fg = db_escape_string($_REQUEST["fg"]);
121                         $bg = db_escape_string($_REQUEST["bg"]);
122
123                         foreach ($ids as $id) {
124
125                                 if ($kind == "fg" || $kind == "bg") {
126                                         db_query($link, "UPDATE ttrss_labels2 SET
127                                                 ${kind}_color = '$color' WHERE id = '$id'
128                                                 AND owner_uid = " . $_SESSION["uid"]);                  
129                                 } else {
130                                         db_query($link, "UPDATE ttrss_labels2 SET
131                                                 fg_color = '$fg', bg_color = '$bg' WHERE id = '$id'
132                                                 AND owner_uid = " . $_SESSION["uid"]);                  
133                                 }
134
135                                 $caption = db_escape_string(label_find_caption($link, $id, $_SESSION["uid"]));
136
137                                 /* Remove cached data */
138
139                                 db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
140                                         WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
141
142                         }
143
144                         return;
145                 }
146
147                 if ($subop == "color-reset") {
148                         $ids = split(',', db_escape_string($_REQUEST["ids"]));
149
150                         foreach ($ids as $id) {
151                                 db_query($link, "UPDATE ttrss_labels2 SET
152                                         fg_color = '', bg_color = '' WHERE id = '$id'
153                                         AND owner_uid = " . $_SESSION["uid"]);                  
154
155                                 $caption = db_escape_string(label_find_caption($link, $id, $_SESSION["uid"]));
156
157                                 /* Remove cached data */
158
159                                 db_query($link, "UPDATE ttrss_user_entries SET label_cache = ''
160                                         WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $_SESSION["uid"]);
161                         }
162
163                 }
164
165                 if ($subop == "save") {
166
167                         $id = db_escape_string($_REQUEST["id"]);
168                         $caption = db_escape_string(trim($_REQUEST["caption"]));
169
170                         db_query($link, "BEGIN");
171
172                         $result = db_query($link, "SELECT caption FROM ttrss_labels2
173                                 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
174
175                         if (db_num_rows($result) != 0) {
176                                 $old_caption = db_fetch_result($result, 0, "caption");
177
178                                 $result = db_query($link, "SELECT id FROM ttrss_labels2
179                                         WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
180
181                                 if (db_num_rows($result) == 0) {
182                                         if ($caption) {
183                                                 $result = db_query($link, "UPDATE ttrss_labels2 SET
184                                                         caption = '$caption' WHERE id = '$id' AND
185                                                         owner_uid = " . $_SESSION["uid"]);
186
187                                                 /* Update filters that reference label being renamed */
188
189                                                 $old_caption = db_escape_string($old_caption);
190
191                                                 db_query($link, "UPDATE ttrss_filters SET
192                                                         action_param = '$caption' WHERE action_param = '$old_caption'
193                                                         AND action_id = 7
194                                                         AND owner_uid = " . $_SESSION["uid"]);
195
196                                                 print $_REQUEST["value"];
197                                         } else {
198                                                 print $old_caption;
199                                         }
200                                 } else {
201                                         print $old_caption;
202                                 }
203                         }
204
205                         db_query($link, "COMMIT");
206
207                         return;
208                 }
209
210                 if ($subop == "remove") {
211
212                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
213
214                         foreach ($ids as $id) {
215                                 label_remove($link, $id, $_SESSION["uid"]);
216                         }
217
218                 }
219
220                 if ($subop == "add") {
221                         $caption = db_escape_string($_REQUEST["caption"]);
222                         $output = db_escape_string($_REQUEST["output"]);
223
224                         if ($caption) {
225
226                                 if (label_create($link, $caption)) {
227                                         if (!$output) {
228                                                 print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
229                                         }
230                                 }
231
232                                 if ($output == "select") {
233                                         header("Content-Type: text/xml");
234
235                                         print "<rpc-reply><payload>";
236
237                                         print_label_select($link, "select_label", 
238                                                 $caption, "");
239
240                                         print "</payload></rpc-reply>";
241                                 }
242                         }
243
244                         return;
245                 }
246
247                 $sort = db_escape_string($_REQUEST["sort"]);
248
249                 if (!$sort || $sort == "undefined") {
250                         $sort = "caption";
251                 }
252
253                 $label_search = db_escape_string($_REQUEST["search"]);
254
255                 if (array_key_exists("search", $_REQUEST)) {
256                         $_SESSION["prefs_label_search"] = $label_search;
257                 } else {
258                         $label_search = $_SESSION["prefs_label_search"];
259                 }
260
261                 print "<div id=\"pref-label-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
262                 print "<div id=\"pref-label-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
263                 print "<div id=\"pref-label-toolbar\" dojoType=\"dijit.Toolbar\">";
264
265                 print "<div dojoType=\"dijit.form.DropDownButton\">".
266                                 "<span>" . __('Select')."</span>";
267                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
268                 print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(true)\" 
269                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
270                 print "<div onclick=\"dijit.byId('labelTree').model.setAllChecked(false)\" 
271                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
272                 print "</div></div>";
273
274                 print"<button dojoType=\"dijit.form.Button\" onclick=\"return addLabel()\">".
275                         __('Create label')."</button dojoType=\"dijit.form.Button\"> ";
276
277                 print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedLabels()\">".
278                         __('Remove')."</button dojoType=\"dijit.form.Button\"> ";
279
280                 print "<button dojoType=\"dijit.form.Button\" onclick=\"labelColorReset()\">".
281                         __('Clear colors')."</button dojoType=\"dijit.form.Button\">";
282
283
284                 print "</div>"; #toolbar
285                 print "</div>"; #pane
286                 print "<div id=\"pref-label-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
287
288                 print "<div id=\"labellistLoading\">
289                 <img src='images/indicator_tiny.gif'>".
290                  __("Loading, please wait...")."</div>";
291
292                 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"labelStore\" 
293                         url=\"backend.php?op=pref-labels&subop=getlabeltree\">
294                 </div>
295                 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"labelModel\" store=\"labelStore\"
296                 query=\"{id:'root'}\" rootId=\"root\"
297                         childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
298                 </div>
299                 <div dojoType=\"fox.PrefLabelTree\" id=\"labelTree\" 
300                         model=\"labelModel\" openOnClick=\"true\">
301                 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
302                         Element.hide(\"labellistLoading\");
303                 </script>
304                 </div>";
305
306                 print "</div>"; #pane
307                 print "</div>"; #container
308         }
309
310 ?>