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