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