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