]> git.wh0rd.org - tt-rss.git/blob - classes/pref/labels.php
8f1f70be94ede47d0a48d570c112371d5923a77e
[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 = $_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 "<div class=\"dlgSec\">".__("Caption")."</div>";
24
25 print "<div class=\"dlgSecCont\">";
26
27 $fg_color = $line['fg_color'];
28 $bg_color = $line['bg_color'];
29
30 print "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-bottom : 4px; margin-right : 4px'>&alpha;</span>";
31
32 print "<input style=\"font-size : 16px\" name=\"caption\"
33 dojoType=\"dijit.form.ValidationTextBox\"
34 required=\"true\"
35 value=\"".htmlspecialchars($line['caption'])."\">";
36
37 print "</div>";
38 print "<div class=\"dlgSec\">" . __("Colors") . "</div>";
39 print "<div class=\"dlgSecCont\">";
40
41 print "<table cellspacing=\"0\">";
42
43 print "<tr><td>".__("Foreground:")."</td><td>".__("Background:").
44 "</td></tr>";
45
46 print "<tr><td style='padding-right : 10px'>";
47
48 print "<input dojoType=\"dijit.form.TextBox\"
49 style=\"display : none\" id=\"labelEdit_fgColor\"
50 name=\"fg_color\" value=\"$fg_color\">";
51 print "<input dojoType=\"dijit.form.TextBox\"
52 style=\"display : none\" id=\"labelEdit_bgColor\"
53 name=\"bg_color\" value=\"$bg_color\">";
54
55 print "<div dojoType=\"dijit.ColorPalette\">
56 <script type=\"dojo/method\" event=\"onChange\" args=\"fg_color\">
57 dijit.byId(\"labelEdit_fgColor\").attr('value', fg_color);
58 $('label-editor-indicator').setStyle({color: fg_color});
59 </script>
60 </div>";
61 print "</div>";
62
63 print "</td><td>";
64
65 print "<div dojoType=\"dijit.ColorPalette\">
66 <script type=\"dojo/method\" event=\"onChange\" args=\"bg_color\">
67 dijit.byId(\"labelEdit_bgColor\").attr('value', bg_color);
68 $('label-editor-indicator').setStyle({backgroundColor: bg_color});
69 </script>
70 </div>";
71 print "</div>";
72
73 print "</td></tr></table>";
74 print "</div>";
75
76 # print "</form>";
77
78 print "<div class=\"dlgButtons\">";
79 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('labelEditDlg').execute()\">".
80 __('Save')."</button>";
81 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('labelEditDlg').hide()\">".
82 __('Cancel')."</button>";
83 print "</div>";
84 }
85 }
86
87 function getlabeltree() {
88 $root = array();
89 $root['id'] = 'root';
90 $root['name'] = __('Labels');
91 $root['items'] = array();
92
93 $sth = $this->pdo->prepare("SELECT *
94 FROM ttrss_labels2
95 WHERE owner_uid = ?
96 ORDER BY caption");
97 $sth->execute([$_SESSION['uid']]);
98
99 while ($line = $sth->fetch()) {
100 $label = array();
101 $label['id'] = 'LABEL:' . $line['id'];
102 $label['bare_id'] = $line['id'];
103 $label['name'] = $line['caption'];
104 $label['fg_color'] = $line['fg_color'];
105 $label['bg_color'] = $line['bg_color'];
106 $label['type'] = 'label';
107 $label['checkbox'] = false;
108
109 array_push($root['items'], $label);
110 }
111
112 $fl = array();
113 $fl['identifier'] = 'id';
114 $fl['label'] = 'name';
115 $fl['items'] = array($root);
116
117 print json_encode($fl);
118 return;
119 }
120
121 function colorset() {
122 $kind = $_REQUEST["kind"];
123 $ids = explode(',', $_REQUEST["ids"]);
124 $color = $_REQUEST["color"];
125 $fg = $_REQUEST["fg"];
126 $bg = $_REQUEST["bg"];
127
128 foreach ($ids as $id) {
129
130 if ($kind == "fg" || $kind == "bg") {
131 $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
132 ${kind}_color = ? WHERE id = ?
133 AND owner_uid = ?");
134
135 $sth->execute([$color, $id, $_SESSION['uid']]);
136
137 } else {
138
139 $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
140 fg_color = ?, bg_color = ? WHERE id = ?
141 AND owner_uid = ?");
142
143 $sth->execute([$fg, $bg, $id, $_SESSION['uid']]);
144 }
145
146 $caption = Labels::find_caption($id, $_SESSION["uid"]);
147
148 /* Remove cached data */
149
150 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
151 WHERE label_cache LIKE ? AND owner_uid = ?");
152 $sth->execute(["%$caption%", $_SESSION['uid']]);
153 }
154 }
155
156 function colorreset() {
157 $ids = explode(',', $_REQUEST["ids"]);
158
159 foreach ($ids as $id) {
160 $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
161 fg_color = '', bg_color = '' WHERE id = ?
162 AND owner_uid = ?");
163 $sth->execute([$id, $_SESSION['uid']]);
164
165 $caption = Labels::find_caption($id, $_SESSION["uid"]);
166
167 /* Remove cached data */
168
169 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
170 WHERE label_cache LIKE ? AND owner_uid = ?");
171 $sth->execute(["%$caption%", $_SESSION['uid']]);
172 }
173 }
174
175 function save() {
176
177 $id = $_REQUEST["id"];
178 $caption = trim($_REQUEST["caption"]);
179
180 $this->pdo->beginTransaction();
181
182 $sth = $this->pdo->prepare("SELECT caption FROM ttrss_labels2
183 WHERE id = ? AND owner_uid = ?");
184 $sth->execute([$id, $_SESSION['uid']]);
185
186 if ($row = $sth->fetch()) {
187 $old_caption = $row["caption"];
188
189 $sth = $this->pdo->prepare("SELECT id FROM ttrss_labels2
190 WHERE caption = ? AND owner_uid = ?");
191 $sth->execute([$caption, $_SESSION['uid']]);
192
193 if (!$sth->fetch()) {
194 if ($caption) {
195 $sth = $this->pdo->prepare("UPDATE ttrss_labels2 SET
196 caption = ? WHERE id = ? AND
197 owner_uid = ?");
198 $sth->execute([$caption, $id, $_SESSION['uid']]);
199
200 /* Update filters that reference label being renamed */
201
202 $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions SET
203 action_param = ? WHERE action_param = ?
204 AND action_id = 7
205 AND filter_id IN (SELECT id FROM ttrss_filters2 WHERE owner_uid = ?)");
206
207 $sth->execute([$caption, $old_caption, $_SESSION['uid']]);
208
209 print $_REQUEST["value"];
210 } else {
211 print $old_caption;
212 }
213 } else {
214 print $old_caption;
215 }
216 }
217
218 $this->pdo->commit();
219
220 }
221
222 function remove() {
223
224 $ids = explode(",", $_REQUEST["ids"]);
225
226 foreach ($ids as $id) {
227 Labels::remove($id, $_SESSION["uid"]);
228 }
229
230 }
231
232 function add() {
233 $caption = $_REQUEST["caption"];
234 $output = $_REQUEST["output"];
235
236 if ($caption) {
237
238 if (Labels::create($caption)) {
239 if (!$output) {
240 print T_sprintf("Created label <b>%s</b>", htmlspecialchars($caption));
241 }
242 }
243
244 if ($output == "select") {
245 header("Content-Type: text/xml");
246
247 print "<rpc-reply><payload>";
248
249 print_label_select("select_label",
250 $caption, "");
251
252 print "</payload></rpc-reply>";
253 }
254 }
255
256 return;
257 }
258
259 function index() {
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&method=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 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
305 var id = String(item.id);
306 var bare_id = id.substr(id.indexOf(':')+1);
307
308 if (id.match('LABEL:')) {
309 editLabel(bare_id);
310 }
311 </script>
312 </div>";
313
314 print "</div>"; #pane
315
316 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
317 "hook_prefs_tab", "prefLabels");
318
319 print "</div>"; #container
320
321 }
322 }