33
|
1 <?php
|
|
2 /*
|
|
3 */
|
|
4
|
|
5 class rc_foldersort extends rcube_plugin
|
|
6 {
|
|
7 public $task = 'mail|settings';
|
|
8
|
|
9 private $rc;
|
|
10 private $sort_order;
|
|
11
|
|
12 private $uname;
|
|
13 private $debug = false;
|
|
14
|
|
15 public function init()
|
|
16 {
|
|
17 $this->rc = rcube::get_instance();
|
|
18 $this->uname = $this->rc->user->get_username();
|
|
19 $userprefs = $this->rc->user->get_prefs();
|
|
20 $this->sort_order = $userprefs['per_folder_sort'];
|
|
21 if (!is_array($this->sort_order)) {
|
|
22 $this->sort_order = array();
|
|
23 }
|
|
24
|
|
25 $this->rc->output->set_env('per_folder_sort', $this->sort_order);
|
|
26
|
|
27 if ($this->rc->task == 'settings') {
|
|
28 $this->add_hook('folder_form', array($this, 'folder_form_hook'));
|
|
29 $this->add_hook('folder_update', array($this, 'folder_update_hook'));
|
|
30 $this->add_hook('preferences_list', array($this, 'preferences_list_hook'));
|
|
31 $this->add_hook('preferences_save', array($this, 'preferences_save_hook'));
|
|
32 }
|
|
33
|
|
34 if ($this->rc->task == 'mail') {
|
|
35 $this->include_script('rc_foldersort.js');
|
|
36 $this->register_action('plugin.rc_foldersort_json', array($this, 'sort_json_action'));
|
|
37 }
|
|
38 }
|
|
39
|
|
40 public function folder_form_hook($args)
|
|
41 {
|
|
42 $content = $args['form']['props']['fieldsets']['settings']['content'];
|
|
43 $options = $args['options'];
|
|
44 $mbox = $options['name'];
|
|
45
|
|
46 $cols = array(
|
|
47 'from',
|
|
48 'to',
|
|
49 'subject',
|
|
50 'date',
|
|
51 'size',
|
|
52 'dates'
|
|
53 );
|
|
54
|
|
55 $folder_sorts = $this->sort_order;
|
|
56 if (array_key_exists($mbox, $folder_sorts)) {
|
|
57 $folder_sort = $folder_sorts[$mbox];
|
|
58 } else if (array_key_exists('default', $folder_sorts)) {
|
|
59 $folder_sort = $folder_sorts['default'];
|
|
60 } else {
|
|
61 $folder_sort = 'date_DESC';
|
|
62 }
|
|
63
|
|
64 list($col, $order) = explode('_', $folder_sort);
|
|
65 if ($order != 'DESC' && $order != 'ASC') {
|
|
66 $order = 'DESC';
|
|
67 }
|
|
68
|
|
69 if (!in_array($col, $cols)) {
|
|
70 $col = 'date';
|
|
71 }
|
|
72
|
|
73 if (is_array($content) && !array_key_exists('_sortcol', $content)) {
|
|
74 $folder_sort_col_select = new html_select(array('name' => '_sortcol', 'id' => '_sortcol'));
|
|
75 foreach ($cols as $temp_col) {
|
|
76 $folder_sort_col_select->add($this->gettext($temp_col), $temp_col);
|
|
77 }
|
|
78
|
|
79 $content['_sortcol'] = array(
|
|
80 'label' => $this->gettext('listsorting'),
|
|
81 'value' => $folder_sort_col_select->show($col),
|
|
82 );
|
|
83 }
|
|
84
|
|
85 if (is_array($content) && !array_key_exists('_sortcol', $options)) {
|
|
86 $options['_sortcol'] = $col;
|
|
87 }
|
|
88
|
|
89 if (is_array($content) && !array_key_exists('_sortord', $content)) {
|
|
90 $folder_sort_order_select = new html_select(array('name' => '_sortord', 'id' => '_sortord'));
|
|
91 $folder_sort_order_select->add($this->gettext('asc'), 'ASC');
|
|
92 $folder_sort_order_select->add($this->gettext('desc'), 'DESC');
|
|
93 $content['_sortord'] = array(
|
|
94 'label' => $this->gettext('listorder'),
|
|
95 'value' => $folder_sort_order_select->show($order),
|
|
96 );
|
|
97 }
|
|
98
|
|
99 if (is_array($content) && !array_key_exists('_sortord', $options)) {
|
|
100 $options['_sortord'] = $order;
|
|
101 }
|
|
102
|
|
103 $args['form']['props']['fieldsets']['settings']['content'] = $content;
|
|
104
|
|
105 $args['options'] = $options;
|
|
106
|
|
107 return $args;
|
|
108 }
|
|
109
|
|
110 public function folder_update_hook($args)
|
|
111 {
|
|
112 $mbox = $args['record']['name'];
|
|
113 $settings = $args['record']['settings'];
|
|
114 $sort_order = $settings['sort_column'] . '_' . $settings['sort_order'];
|
|
115 $cfg_sort = $this->sort_order;
|
|
116 $cfg_sort[$mbox] = $sort_order;
|
|
117 $this->sort_order = $cfg_sort;
|
|
118
|
|
119 $this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
|
|
120 $this->rc->output->set_env('per_folder_sort', $this->sort_order);
|
|
121
|
|
122 return $args;
|
|
123 }
|
|
124
|
|
125 public function preferences_list_hook($args)
|
|
126 {
|
|
127 if ($args['section'] == 'mailbox') {
|
|
128 $cols = array(
|
|
129 'from',
|
|
130 'to',
|
|
131 'subject',
|
|
132 'date',
|
|
133 'size',
|
|
134 'dates'
|
|
135 );
|
|
136
|
|
137 $folder_sorts = $this->sort_order;
|
|
138 if (array_key_exists('default', $folder_sorts)) {
|
|
139 $folder_sort = $folder_sorts['default'];
|
|
140 } else {
|
|
141 $folder_sort = 'date_DESC';
|
|
142 }
|
|
143
|
|
144 list($col, $order) = explode('_', $folder_sort);
|
|
145 if ($order != 'DESC' && $order != 'ASC') {
|
|
146 $order = 'DESC';
|
|
147 }
|
|
148
|
|
149 if (!in_array($col, $cols)) {
|
|
150 $col = 'date';
|
|
151 }
|
|
152
|
|
153 $sort_select_col = new html_select(array('name' => '_default_sort_col', 'id' => '_default_sort_col'));
|
|
154 foreach ($cols as $temp_col) {
|
|
155 $sort_select_col->add($this->gettext($temp_col), $temp_col);
|
|
156 }
|
|
157
|
|
158 $sort_select_order = new html_select(array('name' => '_default_sort_order', 'id' => '_default_sort_order'));
|
|
159 $sort_select_order->add($this->gettext('asc'), 'ASC');
|
|
160 $sort_select_order->add($this->gettext('desc'), 'DESC');
|
|
161 $sort_options = array(
|
|
162 'title' => $this->gettext('listorder'),
|
|
163 'content' => $sort_select_col->show($col) . $sort_select_order->show($order),
|
|
164 );
|
|
165
|
|
166 $args['blocks']['main']['options']['listorder'] = $sort_options;
|
|
167 }
|
|
168
|
|
169 return $args;
|
|
170 }
|
|
171
|
|
172 public function preferences_save_hook($args)
|
|
173 {
|
|
174 if ($args['section'] == 'mailbox') {
|
|
175 $folder_sort_col = rcube_utils::get_input_value('_default_sort_col', RCUBE_INPUT_POST);
|
|
176 $folder_sort_order = rcube_utils::get_input_value('_default_sort_order', RCUBE_INPUT_POST);
|
|
177 $folder_sort = $folder_sort_col . '_' . $folder_sort_order;
|
|
178 $folder_sorts = $this->sort_order;
|
|
179 $folder_sorts['default'] = $folder_sort;
|
|
180 $args['prefs']['per_folder_sort'] = $folder_sorts;
|
|
181 }
|
|
182
|
|
183 return $args;
|
|
184 }
|
|
185
|
|
186 public function sort_json_action()
|
|
187 {
|
|
188 $cmd = rcube_utils::get_input_value('cmd', RCUBE_INPUT_POST);
|
|
189 $folder = rcube_utils::get_input_value('folder', RCUBE_INPUT_POST);
|
|
190 $col = rcube_utils::get_input_value('col', RCUBE_INPUT_POST);
|
|
191 $order = rcube_utils::get_input_value('order', RCUBE_INPUT_POST);
|
|
192
|
|
193 switch ($cmd) {
|
|
194 case 'change_session': {
|
|
195 $_SESSION['sort_col'] = $col;
|
|
196 $_SESSION['sort_order'] = $order;
|
|
197 }
|
|
198 case 'save_order': {
|
|
199 $sort_order = $this->sort_order;
|
|
200 $sort_order[$folder] = $col . "_" . $order;
|
|
201 $this->sort_order = $sort_order;
|
|
202
|
|
203 $this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
|
|
204 $this->rc->output->set_env('per_folder_sort', $this->sort_order);
|
|
205 break;
|
|
206 }
|
|
207 }
|
|
208 }
|
|
209
|
|
210 private function _debug($value, $key = '', $force = false)
|
|
211 {
|
|
212 if ($this->debug || $force) {
|
|
213 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
|
|
214 $caller_trace = $trace[0];
|
|
215 $caller_function = $trace[1]['function'];
|
|
216 $caller_line = $caller_trace['line'];
|
|
217 $caller_file = $caller_trace['file'];
|
|
218 $caller_file = preg_replace("|.*/|", "", $caller_file);
|
|
219 $str = sprintf("[%s:%d - %s] ", $caller_file, $caller_line, $caller_function);
|
|
220
|
|
221 $val_type = gettype($value);
|
|
222
|
|
223 switch ($val_type) {
|
|
224 case "object": {
|
|
225 $old_value = $value;
|
|
226 $value = get_class($old_value);
|
|
227 $str .= $key . ' type = ' . $value;
|
|
228 break;
|
|
229 }
|
|
230 default: {
|
|
231 $old_value = $value;
|
|
232 $value = var_export($old_value, true);
|
|
233 $str .= $key. ' = ' .$value;
|
|
234 break;
|
|
235 }
|
|
236 }
|
|
237
|
|
238 if ($this->uname) {
|
|
239 $str = sprintf("[%s] %s", $this->uname, $str);
|
|
240 }
|
|
241
|
|
242 write_log($this->ID, $str);
|
|
243 }
|
|
244 }
|
|
245
|
|
246 }
|
|
247 ?>
|