Mercurial > hg > rc1
annotate plugins/thunderbird_labels/thunderbird_labels.php @ 1:5821049f1791
most of the way to allowing arbitrarily many labels
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 16:23:36 -0500 |
parents | 1e000243b222 |
children | c828b0fd4a6e |
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 { | |
210 if ($args['section'] != 'thunderbird_labels') | |
211 return $args; | |
212 | |
213 | |
214 $this->load_config(); | |
215 $dont_override = (array) $this->rc->config->get('dont_override', array()); | |
216 | |
217 if (!in_array('tb_label_enable', $dont_override)) | |
218 $args['prefs']['tb_label_enable'] = rcube_utils::get_input_value('tb_label_enable', rcube_utils::INPUT_POST) ? true : false; | |
219 | |
220 if (!in_array('tb_label_enable_shortcuts', $dont_override)) | |
221 $args['prefs']['tb_label_enable_shortcuts'] = rcube_utils::get_input_value('tb_label_enable_shortcuts', rcube_utils::INPUT_POST) ? true : false; | |
222 | |
223 if (!in_array('tb_label_style', $dont_override)) | |
224 $args['prefs']['tb_label_style'] = rcube_utils::get_input_value('tb_label_style', rcube_utils::INPUT_POST); | |
225 | |
226 if (!in_array('tb_label_custom_labels', $dont_override) | |
227 && $this->rc->config->get('tb_label_modify_labels')) | |
228 { | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
229 $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
|
230 $tb_lab_prefs = array(); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
231 $tb_lab_prefs[0] = $this->gettext('label0'); |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
232 for ($i = 1; $i <= $max_id; $i++) { |
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
233 $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
|
234 } |
0 | 235 } |
236 | |
237 return $args; | |
238 } | |
239 | |
240 public function show_tb_label_contextmenu($args) | |
241 { | |
242 #$this->api->output->add_label('copymessage.copyingmessage'); | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
243 rcube::write_log('lab',"stblc"); |
0 | 244 $li = html::tag('li', |
245 array('class' => 'submenu'), | |
246 '<span>'.rcube::Q($this->gettext('tb_label_contextmenu_title')).'</span>' . $this->_gen_label_submenu($args, 'tb_label_ctxm_submenu')); | |
247 $out .= html::tag('ul', array('id' => 'tb_label_ctxm_mainmenu'), $li); | |
248 $this->api->output->add_footer(html::div(array('style' => 'display: none;'), $out)); | |
249 } | |
250 | |
251 private function _gen_label_submenu($args, $id) | |
252 { | |
253 $out = ''; | |
254 $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
|
255 $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
|
256 for ($i = 0; $i <= $max_id; $i++) |
0 | 257 { |
258 $separator = ($i == 0)? ' separator_below' :''; | |
259 $out .= '<li class="label'.$i.$separator. | |
260 ' ctxm_tb_label"><a href="#ctxm_tb_label" class="active" onclick="rcmail_ctxm_label_set('.$i.')"><span>'. | |
261 $i.' '.$custom_labels[$i]. | |
262 '</span></a></li>'; | |
263 } | |
264 $out = html::tag('ul', array('class' => 'popupmenu toolbarmenu folders', 'id' => $id), $out); | |
265 return $out; | |
266 } | |
267 | |
268 public function read_single_flags($args) | |
269 { | |
270 #rcube::write_log($this->name, print_r(($args['object']), true)); | |
271 if (!isset($args['object'])) { | |
272 return; | |
273 } | |
274 | |
275 if (is_array($args['object']->headers->flags)) | |
276 { | |
277 $this->message_tb_labels = array(); | |
278 foreach ($args['object']->headers->flags as $flagname => $flagvalue) | |
279 { | |
280 $flag = is_numeric("$flagvalue")? $flagname:$flagvalue;// for compatibility with < 0.5.4 | |
281 $flag = strtolower($flag); | |
282 if (preg_match('/^\$?label/', $flag)) | |
283 { | |
284 $flag_no = preg_replace('/^\$?label/', '', $flag); | |
285 #rcube::write_log($this->name, "Single message Flag: ".$flag." Flag_no:".$flag_no); | |
286 $this->message_tb_labels[] = (int)$flag_no; | |
287 } | |
288 } | |
289 } | |
290 # -- no return value for this hook | |
291 } | |
292 | |
293 /** | |
294 * Writes labelnumbers for single message display | |
295 * Coloring of Message header table happens via Javascript | |
296 */ | |
297 public function color_headers($p) | |
298 { | |
299 #rcube::write_log($this->name, print_r($p, true)); | |
300 # -- always write array, even when empty | |
301 $p['content'] .= '<script type="text/javascript"> | |
302 var tb_labels_for_message = ['.join(',', $this->message_tb_labels).']; | |
303 </script>'; | |
304 return $p; | |
305 } | |
306 | |
307 public function read_flags($args) | |
308 { | |
309 #rcube::write_log($this->name, print_r($args, true)); | |
310 // add color information for all messages | |
311 // dont loop over all messages if we dont have any highlights or no msgs | |
312 if (!isset($args['messages']) or !is_array($args['messages'])) { | |
313 return $args; | |
314 } | |
315 | |
316 // loop over all messages and add $LabelX info to the extra_flags | |
317 foreach($args['messages'] as $message) | |
318 { | |
319 #rcube::write_log($this->name, print_r($message->flags, true)); | |
320 $message->list_flags['extra_flags']['tb_labels'] = array(); # always set extra_flags, needed for javascript later! | |
321 if (is_array($message->flags)) | |
322 foreach ($message->flags as $flagname => $flagvalue) | |
323 { | |
324 $flag = is_numeric("$flagvalue")? $flagname:$flagvalue;// for compatibility with < 0.5.4 | |
325 $flag = strtolower($flag); | |
326 if (preg_match('/^\$?label/', $flag)) | |
327 { | |
328 $flag_no = preg_replace('/^\$?label/', '', $flag); | |
329 #rcube::write_log($this->name, "Flag:".$flag." Flag_no:".$flag_no); | |
330 $message->list_flags['extra_flags']['tb_labels'][] = (int)$flag_no; | |
331 } | |
332 } | |
333 } | |
334 return($args); | |
335 } | |
336 | |
337 // set flags in IMAP server | |
338 function set_flags() | |
339 { | |
340 #rcube::write_log($this->name, print_r($_GET, true)); | |
341 | |
342 $imap = $this->rc->imap; | |
343 $cbox = rcube_utils::get_input_value('_cur', rcube_utils::INPUT_GET); | |
344 $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_GET); | |
345 $toggle_label = rcube_utils::get_input_value('_toggle_label', rcube_utils::INPUT_GET); | |
346 $flag_uids = rcube_utils::get_input_value('_flag_uids', rcube_utils::INPUT_GET); | |
347 $flag_uids = explode(',', $flag_uids); | |
348 $unflag_uids = rcube_utils::get_input_value('_unflag_uids', rcube_utils::INPUT_GET); | |
349 $unflag_uids = explode(',', $unflag_uids); | |
350 | |
351 $imap->conn->flags = array_merge($imap->conn->flags, $this->add_tb_flags); | |
352 | |
353 #rcube::write_log($this->name, print_r($flag_uids, true)); | |
354 #rcube::write_log($this->name, print_r($unflag_uids, true)); | |
355 | |
356 if (!is_array($unflag_uids) | |
357 || !is_array($flag_uids)) | |
358 return false; | |
359 | |
360 $imap->set_flag($flag_uids, $toggle_label, $mbox); | |
361 $imap->set_flag($unflag_uids, "UN$toggle_label", $mbox); | |
362 | |
363 $this->api->output->send(); | |
364 } | |
365 | |
366 function tb_label_popup() | |
367 { | |
368 $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
|
369 $max_id = $this->rc->config->get('tb_max_label'); |
0 | 370 $out = '<div id="tb_label_popup" class="popupmenu"> |
371 <ul class="toolbarmenu">'; | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
372 for ($i = 0; $i <= $max_id; $i++) |
0 | 373 { |
374 $separator = ($i == 0)? ' separator_below' :''; | |
375 $out .= '<li class="label'.$i.$separator.'"><a href="#" class="active">'.$i.' '.$custom_labels[$i].'</a></li>'; | |
376 } | |
377 $out .= '</ul> | |
378 </div>'; | |
379 $this->rc->output->add_gui_object('tb_label_popup_obj', 'tb_label_popup'); | |
380 $this->rc->output->add_footer($out); | |
381 } | |
382 } | |
383 |