summaryrefslogtreecommitdiff
path: root/plugins/thunderbird_labels/thunderbird_labels.php
blob: e6487ae958b21c7d9d085d0d91c2842d9207272d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
 * Thunderbird Labels Plugin for Roundcube Webmail
 *
 * Plugin to show the 5 Message Labels Thunderbird Email-Client provides for IMAP
 *
 * @version $Revision: 24 $
 * @author Michael Kefeder
 * @url http://code.google.com/p/rcmail-thunderbird-labels/
 */
class thunderbird_labels extends rcube_plugin
{
	public $task = 'mail';
	private $map;
	
	function init()
	{
		$rcmail = rcmail::get_instance();
		# -- disable plugin when printing message
		if ($rcmail->action == 'print')
			return;
		
		$this->include_script('tb_label.js');
		$this->add_texts('localization/', true);
		$this->add_hook('messages_list', array($this, 'read_flags'));
		$this->add_hook('message_load', array($this, 'read_single_flags'));
		$this->add_hook('template_object_messageheaders', array($this, 'color_headers'));
		$this->add_hook('render_page', array($this, 'tb_label_popup'));
		$this->include_stylesheet($this->local_skin_path() . '/tb_label.css');
		
		$this->name = get_class($this);
		$this->prefs = array('show_labels' => true);
		# -- additional TB flags
		$this->add_tb_flags = array(
			'LABEL1' => '$Label1',
			'LABEL2' => '$Label2',
			'LABEL3' => '$Label3',
			'LABEL4' => '$Label4',
			'LABEL5' => '$Label5',
			);
		$this->message_tb_labels = array();
		
		$this->add_button(
			array(
				'command' => 'plugin.thunderbird_labels.rcm_tb_label_submenu',
				'id' => 'tb_label_popuplink',
				'title' => 'label', # gets translated
				'domain' => $this->ID,
				'type' => 'link',
				'content' => ' ', # maybe put translated version of "Labels" here?
				'class' => ($rcmail->config->get('skin') == 'larry') ? 'button' : 'tb_noclass',
			),
			'toolbar'
		);
		
		$this->register_action('plugin.thunderbird_labels.set_flags', array($this, 'set_flags'));
		
		
		if (method_exists($this, 'require_plugin')
			&& in_array('contextmenu', $rcmail->config->get('plugins'))
			&& $this->require_plugin('contextmenu'))
		{
			if ($rcmail->action == '')
				$this->add_hook('render_mailboxlist', array($this, 'show_tb_label_contextmenu'));
		}
	}
	
	public function show_tb_label_contextmenu($args)
	{
		$rcmail = rcmail::get_instance();
		$this->add_texts('localization/');
		#$this->api->output->add_label('copymessage.copyingmessage');

		$li = html::tag('li', array('class' => 'submenu'), Q($this->gettext('label')) . $this->_gen_label_submenu($args, 'tb_label_ctxm_submenu'));
		$out .= html::tag('ul', array('id' => 'tb_label_ctxm_mainmenu'), $li);
		$this->api->output->add_footer(html::div(array('style' => 'display: none;'), $out));
	}
	
	private function _gen_label_submenu($args, $id)
	{
		$rcmail = rcmail::get_instance();
		$out = '';
		for ($i = 0; $i < 6; $i++)
		{
			$separator = ($i == 0)? ' separator_below' :'';
			$out .= '<li class="label'.$i.$separator.' ctxm_tb_label"><a href="#ctxm_tb_label" class="active" onclick="rcmail_ctxm_label_set('.$i.')">'.$i.' '.$this->gettext('label'.$i).'</a></li>';
		}
		$out = html::tag('ul', array('class' => 'popupmenu toolbarmenu folders', 'id' => $id), $out);
		return $out;
	}
	
	public function read_single_flags($args)
	{
		#write_log($this->name, print_r(($args['object']), true));
		if (!count($this->prefs) 
			or !isset($args['object']) 
			)
				return;
		
		if (is_array($args['object']->headers->flags))
		{
			$this->message_tb_labels = array();
			foreach ($args['object']->headers->flags as $flagname => $flagvalue)
			{
				$flag = is_numeric("$flagvalue")? $flagname:$flagvalue;// for compatibility with < 0.5.4
				$flag = strtolower($flag);
				if (preg_match('/^\$?label/', $flag))
				{
					$flag_no = preg_replace('/^\$?label/', '', $flag);
					#write_log($this->name, "Single message Flag: ".$flag." Flag_no:".$flag_no);
					$this->message_tb_labels[] = (int)$flag_no;
				}
			}
		}
		# -- no return value for this hook
	}
	
	/**
	*	Writes labelnumbers for single message display
	*	Coloring of Message header table happens via Javascript
	*/
	public function color_headers($p)
	{
		#write_log($this->name, print_r($p, true));
		# -- always write array, even when empty
		$p['content'] .= '<script type="text/javascript">
		var tb_labels_for_message = ['.join(',', $this->message_tb_labels).'];
		</script>';
		return $p;
	}
	
	public function read_flags($args)
	{
		#write_log($this->name, print_r($args, true));
		// add color information for all messages
		#$rcmail = rcmail::get_instance();
		#$this->prefs = $rcmail->config->get('thunderbird_labels', array());
		// dont loop over all messages if we dont have any highlights or no msgs
		if (!count($this->prefs) 
			or !isset($args['messages']) 
			or !is_array($args['messages']))
				return $args;

		// loop over all messages and add $LabelX info to the extra_flags
		foreach($args['messages'] as $message)
		{
			#write_log($this->name, print_r($message->flags, true));
			$message->list_flags['extra_flags']['tb_labels'] = array(); # always set extra_flags, needed for javascript later!
			if (is_array($message->flags))
			foreach ($message->flags as $flagname => $flagvalue)
			{
				$flag = is_numeric("$flagvalue")? $flagname:$flagvalue;// for compatibility with < 0.5.4
				$flag = strtolower($flag);
				if (preg_match('/^\$?label/', $flag))
				{
					$flag_no = preg_replace('/^\$?label/', '', $flag);
					#write_log($this->name, "Flag:".$flag." Flag_no:".$flag_no);
					$message->list_flags['extra_flags']['tb_labels'][] = (int)$flag_no;
				}
			}
		}
		return($args);
	}
	
	function set_flags()
	{
		#write_log($this->name, print_r($_GET, true));

		$rcmail = rcmail::get_instance();
		$imap = $rcmail->imap;
		$cbox = get_input_value('_cur', RCUBE_INPUT_GET);
		$mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
		$toggle_label = get_input_value('_toggle_label', RCUBE_INPUT_GET);
		$flag_uids = get_input_value('_flag_uids', RCUBE_INPUT_GET);
		$flag_uids = explode(',', $flag_uids);
		$unflag_uids = get_input_value('_unflag_uids', RCUBE_INPUT_GET);
		$unflag_uids = explode(',', $unflag_uids);
		
		$imap->conn->flags = array_merge($imap->conn->flags, $this->add_tb_flags);
		
		#write_log($this->name, print_r($flag_uids, true));
		#write_log($this->name, print_r($unflag_uids, true));

		if (!is_array($unflag_uids)
			|| !is_array($flag_uids))
			return false;

		$imap->set_flag($flag_uids, $toggle_label, $mbox);
		$imap->set_flag($unflag_uids, "UN$toggle_label", $mbox);

		$this->api->output->send();
	}
	
	function tb_label_popup()
	{
		$rcmail = rcmail::get_instance();
		$out = '<div id="tb_label_popup" class="popupmenu">
			<ul class="toolbarmenu">';
		for ($i = 0; $i < 6; $i++)
		{
			$separator = ($i == 0)? ' separator_below' :'';
			$out .= '<li class="label'.$i.$separator.'"><a href="#" class="active">'.$i.' '.$this->gettext('label'.$i).'</a></li>';
		}
		$out .= '</ul>
		</div>';
		$rcmail->output->add_gui_object('tb_label_popup_obj', 'tb_label_popup');
    	$rcmail->output->add_footer($out);
	}
}
?>