Mercurial > hg > rc1
annotate plugins/thunderbird_labels/thunderbird_labels.php @ 2:c828b0fd4a6e
fix missing line to store changed labels
author | Charlie Root |
---|---|
date | Fri, 05 Jan 2018 14:17:23 -0500 |
parents | 5821049f1791 |
children | 7a7f68b4358e |
rev | line source |
---|---|
0 | 1 <?php |
2 /** | |
3 * Thunderbird Labels Plugin for Roundcube Webmail | |
4 * | |
5 * Plugin to show the 5 Message Labels Thunderbird Email-Client provides for IMAP | |
6 * | |
7 * @version $Revision$ | |
8 * @author Michael Kefeder | |
9 * @url http://code.google.com/p/rcmail-thunderbird-labels/ | |
10 */ | |
11 class thunderbird_labels extends rcube_plugin | |
12 { | |
13 public $task = 'mail|settings'; | |
14 private $rc; | |
15 private $map; | |
16 | |
17 function init() | |
18 { | |
19 $this->rc = rcmail::get_instance(); | |
20 $this->load_config(); | |
21 $this->add_texts('localization/', false); | |
22 | |
23 $this->setCustomLabels(); | |
24 | |
25 if ($this->rc->task == 'mail') | |
26 { | |
27 # -- disable plugin when printing message | |
28 if ($this->rc->action == 'print') | |
29 return; | |
30 | |
31 if (!$this->rc->config->get('tb_label_enable')) | |
32 // disable plugin according to prefs | |
33 return; | |
34 | |
35 // pass 'tb_label_enable_shortcuts' and 'tb_label_style' prefs to JS | |
36 $this->rc->output->set_env('tb_label_enable_shortcuts', $this->rc->config->get('tb_label_enable_shortcuts')); | |
37 $this->rc->output->set_env('tb_label_style', $this->rc->config->get('tb_label_style')); | |
38 | |
39 $this->include_script('tb_label.js'); | |
40 $this->add_hook('messages_list', array($this, 'read_flags')); | |
41 $this->add_hook('message_load', array($this, 'read_single_flags')); | |
42 $this->add_hook('template_object_messageheaders', array($this, 'color_headers')); | |
43 $this->add_hook('render_page', array($this, 'tb_label_popup')); | |
44 $this->include_stylesheet($this->local_skin_path() . '/tb_label.css'); | |
45 | |
46 $this->name = get_class($this); | |
47 # -- additional TB flags | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
48 $this->add_tb_flags = array(); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
49 $max_id = $this->rc->config->get('tb_max_label'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
50 for ($i = 1; $i <= $max_id; $i++) { |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
51 $this->add_tb_flags['LABEL'.$i]='$Label'.$i; |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
52 } |
0 | 53 $this->message_tb_labels = array(); |
54 | |
55 $this->add_button( | |
56 array( | |
57 'command' => 'plugin.thunderbird_labels.rcm_tb_label_submenu', | |
58 'id' => 'tb_label_popuplink', | |
59 'title' => 'tb_label_button_title', | |
60 'domain' => $this->ID, | |
61 'type' => 'link', | |
62 'content' => $this->gettext('tb_label_button_label'), | |
63 'class' => 'button buttonPas disabled', | |
64 'classact' => 'button', | |
65 ), | |
66 'toolbar' | |
67 ); | |
68 | |
69 // JS function "set_flags" => PHP function "set_flags" | |
70 $this->register_action('plugin.thunderbird_labels.set_flags', array($this, 'set_flags')); | |
71 | |
72 | |
73 if (method_exists($this, 'require_plugin') | |
74 && in_array('contextmenu', $this->rc->config->get('plugins')) | |
75 && $this->require_plugin('contextmenu') | |
76 && $this->rc->config->get('tb_label_enable_contextmenu')) | |
77 { | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
78 if ($this->rc->action == 'list') |
0 | 79 $this->add_hook('render_mailboxlist', array($this, 'show_tb_label_contextmenu')); |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
80 rcube::write_log('lab','ctxt: '.$this->rc->action); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
81 } |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
82 else { |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
83 rcube::write_log('lab','no ctxt'); |
0 | 84 } |
85 } | |
86 elseif ($this->rc->task == 'settings') | |
87 { | |
88 $this->include_stylesheet($this->local_skin_path() . '/tb_label.css'); | |
89 $this->add_hook('preferences_list', array($this, 'prefs_list')); | |
90 $this->add_hook('preferences_sections_list', array($this, 'prefs_section')); | |
91 $this->add_hook('preferences_save', array($this, 'prefs_save')); | |
92 } | |
93 } | |
94 | |
95 private function setCustomLabels() | |
96 { | |
97 $c = $this->rc->config->get('tb_label_custom_labels'); | |
98 if (empty($c)) | |
99 { | |
100 // if no user specific labels, use localized strings by default | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
101 $max_id = $this->rc->config->get('tb_max_label'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
102 $tb_flag_text = array(); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
103 for ($i = 0; $i <= $max_id; $i++) { |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
104 $tb_flag_text[$i] = $this->getText('label'.$i); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
105 } |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
106 $this->rc->config->set('tb_label_custom_labels', |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
107 tb_flag_text); |
0 | 108 } |
109 // pass label strings to JS | |
110 $this->rc->output->set_env('tb_label_custom_labels', $this->rc->config->get('tb_label_custom_labels')); | |
111 } | |
112 | |
113 // create a section for the tb-labels Settings | |
114 public function prefs_section($args) | |
115 { | |
116 $args['list']['thunderbird_labels'] = array( | |
117 'id' => 'thunderbird_labels', | |
118 'section' => rcube::Q($this->gettext('tb_label_options')) | |
119 ); | |
120 | |
121 return $args; | |
122 } | |
123 | |
124 // display thunderbird-labels prefs in Roundcube Settings | |
125 public function prefs_list($args) | |
126 { | |
127 if ($args['section'] != 'thunderbird_labels') | |
128 return $args; | |
129 | |
130 $this->load_config(); | |
131 $dont_override = (array) $this->rc->config->get('dont_override', array()); | |
132 | |
133 $args['blocks']['tb_label'] = array(); | |
134 $args['blocks']['tb_label']['name'] = $this->gettext('tb_label_options'); | |
135 | |
136 $key = 'tb_label_enable'; | |
137 if (!in_array($key, $dont_override)) | |
138 { | |
139 $input = new html_checkbox(array( | |
140 'name' => $key, | |
141 'id' => $key, | |
142 'value' => 1 | |
143 )); | |
144 $content = $input->show($this->rc->config->get($key)); | |
145 $args['blocks']['tb_label']['options'][$key] = array( | |
146 'title' => $this->gettext('tb_label_enable_option'), | |
147 'content' => $content | |
148 ); | |
149 } | |
150 | |
151 $key = 'tb_label_enable_shortcuts'; | |
152 if (!in_array($key, $dont_override)) | |
153 { | |
154 $input = new html_checkbox(array( | |
155 'name' => $key, | |
156 'id' => $key, | |
157 'value' => 1 | |
158 )); | |
159 $content = $input->show($this->rc->config->get($key)); | |
160 $args['blocks']['tb_label']['options'][$key] = array( | |
161 'title' => $this->gettext('tb_label_enable_shortcuts_option'), | |
162 'content' => $content | |
163 ); | |
164 } | |
165 | |
166 $key = 'tb_label_style'; | |
167 if (!in_array($key, $dont_override)) | |
168 { | |
169 $select = new html_select(array( | |
170 'name' => $key, | |
171 'id' => $key | |
172 )); | |
173 $select->add(array($this->gettext('thunderbird'), $this->gettext('bullets')), array('thunderbird', 'bullets')); | |
174 $content = $select->show($this->rc->config->get($key)); | |
175 | |
176 $args['blocks']['tb_label']['options'][$key] = array( | |
177 'title' => $this->gettext('tb_label_style_option'), | |
178 'content' => $content | |
179 ); | |
180 } | |
181 | |
182 $key = 'tb_label_custom_labels'; | |
183 if (!in_array($key, $dont_override) | |
184 && $this->rc->config->get('tb_label_modify_labels')) | |
185 { | |
186 $old = $this->rc->config->get($key); | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
187 $max_id = $this->rc->config->get('tb_max_label'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
188 for($i=1; $i<=$max_id; $i++) |
0 | 189 { |
190 $input = new html_inputfield(array( | |
191 'name' => $key.$i, | |
192 'id' => $key.$i, | |
193 'type' => 'text', | |
194 'autocomplete' => 'off', | |
195 'value' => $old[$i])); | |
196 | |
197 $args['blocks']['tb_label']['options'][$key.$i] = array( | |
198 'title' => $this->gettext('tb_label_label')." ".$i, | |
199 'content' => $input->show() | |
200 ); | |
201 } | |
202 } | |
203 | |
204 return $args; | |
205 } | |
206 | |
207 // save prefs after modified in UI | |
208 public function prefs_save($args) | |
209 { | |
2 | 210 rcube::write_log('lab','saving: '.print_r($args,true)); |
211 if ($args['section'] != 'thunderbird_labels') | |
0 | 212 return $args; |
213 | |
214 | |
215 $this->load_config(); | |
216 $dont_override = (array) $this->rc->config->get('dont_override', array()); | |
217 | |
218 if (!in_array('tb_label_enable', $dont_override)) | |
219 $args['prefs']['tb_label_enable'] = rcube_utils::get_input_value('tb_label_enable', rcube_utils::INPUT_POST) ? true : false; | |
220 | |
221 if (!in_array('tb_label_enable_shortcuts', $dont_override)) | |
222 $args['prefs']['tb_label_enable_shortcuts'] = rcube_utils::get_input_value('tb_label_enable_shortcuts', rcube_utils::INPUT_POST) ? true : false; | |
223 | |
224 if (!in_array('tb_label_style', $dont_override)) | |
225 $args['prefs']['tb_label_style'] = rcube_utils::get_input_value('tb_label_style', rcube_utils::INPUT_POST); | |
226 | |
227 if (!in_array('tb_label_custom_labels', $dont_override) | |
228 && $this->rc->config->get('tb_label_modify_labels')) | |
229 { | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
230 $max_id = $this->rc->config->get('tb_max_label'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
231 $tb_lab_prefs = array(); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
232 $tb_lab_prefs[0] = $this->gettext('label0'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
233 for ($i = 1; $i <= $max_id; $i++) { |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
234 $tb_lab_prefs[$i] = rcube_utils::get_input_value('tb_label_custom_labels'.$i, rcube_utils::INPUT_POST); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
235 } |
2 | 236 $args['prefs']['tb_label_custom_labels'] = $tb_lab_prefs; |
0 | 237 } |
238 | |
239 return $args; | |
240 } | |
241 | |
242 public function show_tb_label_contextmenu($args) | |
243 { | |
244 #$this->api->output->add_label('copymessage.copyingmessage'); | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
245 rcube::write_log('lab',"stblc"); |
0 | 246 $li = html::tag('li', |
247 array('class' => 'submenu'), | |
248 '<span>'.rcube::Q($this->gettext('tb_label_contextmenu_title')).'</span>' . $this->_gen_label_submenu($args, 'tb_label_ctxm_submenu')); | |
249 $out .= html::tag('ul', array('id' => 'tb_label_ctxm_mainmenu'), $li); | |
250 $this->api->output->add_footer(html::div(array('style' => 'display: none;'), $out)); | |
251 } | |
252 | |
253 private function _gen_label_submenu($args, $id) | |
254 { | |
255 $out = ''; | |
256 $custom_labels = $this->rc->config->get('tb_label_custom_labels'); | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
257 $max_id = $this->rc->config->get('tb_max_label'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
258 for ($i = 0; $i <= $max_id; $i++) |
0 | 259 { |
260 $separator = ($i == 0)? ' separator_below' :''; | |
261 $out .= '<li class="label'.$i.$separator. | |
262 ' ctxm_tb_label"><a href="#ctxm_tb_label" class="active" onclick="rcmail_ctxm_label_set('.$i.')"><span>'. | |
263 $i.' '.$custom_labels[$i]. | |
264 '</span></a></li>'; | |
265 } | |
266 $out = html::tag('ul', array('class' => 'popupmenu toolbarmenu folders', 'id' => $id), $out); | |
267 return $out; | |
268 } | |
269 | |
270 public function read_single_flags($args) | |
271 { | |
2 | 272 rcube::write_log($this->name, print_r(($args['object']), true)); |
0 | 273 if (!isset($args['object'])) { |
274 return; | |
275 } | |
276 | |
277 if (is_array($args['object']->headers->flags)) | |
278 { | |
279 $this->message_tb_labels = array(); | |
280 foreach ($args['object']->headers->flags as $flagname => $flagvalue) | |
281 { | |
282 $flag = is_numeric("$flagvalue")? $flagname:$flagvalue;// for compatibility with < 0.5.4 | |
283 $flag = strtolower($flag); | |
284 if (preg_match('/^\$?label/', $flag)) | |
285 { | |
286 $flag_no = preg_replace('/^\$?label/', '', $flag); | |
2 | 287 rcube::write_log($this->name, "Single message Flag: ".$flag." Flag_no:".$flag_no); |
0 | 288 $this->message_tb_labels[] = (int)$flag_no; |
289 } | |
290 } | |
291 } | |
292 # -- no return value for this hook | |
293 } | |
294 | |
295 /** | |
296 * Writes labelnumbers for single message display | |
297 * Coloring of Message header table happens via Javascript | |
298 */ | |
299 public function color_headers($p) | |
300 { | |
2 | 301 rcube::write_log($this->name, print_r($p, true)); |
0 | 302 # -- always write array, even when empty |
303 $p['content'] .= '<script type="text/javascript"> | |
304 var tb_labels_for_message = ['.join(',', $this->message_tb_labels).']; | |
305 </script>'; | |
306 return $p; | |
307 } | |
308 | |
309 public function read_flags($args) | |
310 { | |
2 | 311 rcube::write_log($this->name, print_r($args, true)); |
0 | 312 // add color information for all messages |
313 // dont loop over all messages if we dont have any highlights or no msgs | |
314 if (!isset($args['messages']) or !is_array($args['messages'])) { | |
315 return $args; | |
316 } | |
317 | |
318 // loop over all messages and add $LabelX info to the extra_flags | |
319 foreach($args['messages'] as $message) | |
320 { | |
2 | 321 rcube::write_log($this->name, print_r($message->flags, true)); |
0 | 322 $message->list_flags['extra_flags']['tb_labels'] = array(); # always set extra_flags, needed for javascript later! |
323 if (is_array($message->flags)) | |
324 foreach ($message->flags as $flagname => $flagvalue) | |
325 { | |
326 $flag = is_numeric("$flagvalue")? $flagname:$flagvalue;// for compatibility with < 0.5.4 | |
327 $flag = strtolower($flag); | |
328 if (preg_match('/^\$?label/', $flag)) | |
329 { | |
330 $flag_no = preg_replace('/^\$?label/', '', $flag); | |
2 | 331 rcube::write_log($this->name, "Flag:".$flag." Flag_no:".$flag_no); |
0 | 332 $message->list_flags['extra_flags']['tb_labels'][] = (int)$flag_no; |
333 } | |
334 } | |
335 } | |
336 return($args); | |
337 } | |
338 | |
339 // set flags in IMAP server | |
340 function set_flags() | |
341 { | |
2 | 342 rcube::write_log($this->name, print_r($_GET, true)); |
0 | 343 |
344 $imap = $this->rc->imap; | |
345 $cbox = rcube_utils::get_input_value('_cur', rcube_utils::INPUT_GET); | |
346 $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_GET); | |
347 $toggle_label = rcube_utils::get_input_value('_toggle_label', rcube_utils::INPUT_GET); | |
348 $flag_uids = rcube_utils::get_input_value('_flag_uids', rcube_utils::INPUT_GET); | |
349 $flag_uids = explode(',', $flag_uids); | |
350 $unflag_uids = rcube_utils::get_input_value('_unflag_uids', rcube_utils::INPUT_GET); | |
351 $unflag_uids = explode(',', $unflag_uids); | |
352 | |
353 $imap->conn->flags = array_merge($imap->conn->flags, $this->add_tb_flags); | |
354 | |
2 | 355 rcube::write_log($this->name, print_r($flag_uids, true)); |
356 rcube::write_log($this->name, print_r($unflag_uids, true)); | |
0 | 357 |
358 if (!is_array($unflag_uids) | |
359 || !is_array($flag_uids)) | |
360 return false; | |
361 | |
362 $imap->set_flag($flag_uids, $toggle_label, $mbox); | |
363 $imap->set_flag($unflag_uids, "UN$toggle_label", $mbox); | |
364 | |
365 $this->api->output->send(); | |
366 } | |
367 | |
368 function tb_label_popup() | |
369 { | |
370 $custom_labels = $this->rc->config->get('tb_label_custom_labels'); | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
371 $max_id = $this->rc->config->get('tb_max_label'); |
0 | 372 $out = '<div id="tb_label_popup" class="popupmenu"> |
373 <ul class="toolbarmenu">'; | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
374 for ($i = 0; $i <= $max_id; $i++) |
0 | 375 { |
376 $separator = ($i == 0)? ' separator_below' :''; | |
377 $out .= '<li class="label'.$i.$separator.'"><a href="#" class="active">'.$i.' '.$custom_labels[$i].'</a></li>'; | |
378 } | |
379 $out .= '</ul> | |
380 </div>'; | |
381 $this->rc->output->add_gui_object('tb_label_popup_obj', 'tb_label_popup'); | |
382 $this->rc->output->add_footer($out); | |
383 } | |
384 } | |
385 |