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