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