comparison plugins/thunderbird_labels/thunderbird_labels.php @ 0:1e000243b222

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