-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrc_foldersort.php
245 lines (203 loc) · 8.47 KB
/
rc_foldersort.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/*
*/
class rc_foldersort extends rcube_plugin
{
public $task = 'mail|settings';
private $rc;
private $sort_order;
private $uname;
private $debug = false;
public function init()
{
$this->rc = rcube::get_instance();
$this->uname = $this->rc->user->get_username();
$userprefs = $this->rc->user->get_prefs();
$this->sort_order = $userprefs['per_folder_sort'];
if (!is_array($this->sort_order)) {
$this->sort_order = array();
}
$this->rc->output->set_env('per_folder_sort', $this->sort_order);
if ($this->rc->task == 'settings') {
$this->add_hook('folder_form', array($this, 'folder_form_hook'));
$this->add_hook('folder_update', array($this, 'folder_update_hook'));
$this->add_hook('preferences_list', array($this, 'preferences_list_hook'));
$this->add_hook('preferences_save', array($this, 'preferences_save_hook'));
}
if ($this->rc->task == 'mail') {
$this->include_script('rc_foldersort.js');
$this->register_action('plugin.rc_foldersort_json', array($this, 'sort_json_action'));
}
}
public function folder_form_hook($args)
{
$content = $args['form']['props']['fieldsets']['settings']['content'];
$options = $args['options'];
$mbox = $options['name'];
$cols = array(
'from',
'to',
'subject',
'date',
'size',
);
$folder_sorts = $this->sort_order;
if (array_key_exists($mbox, $folder_sorts)) {
$folder_sort = $folder_sorts[$mbox];
} else if (array_key_exists('default', $folder_sorts)) {
$folder_sort = $folder_sorts['default'];
} else {
$folder_sort = 'date_DESC';
}
list($col, $order) = explode('_', $folder_sort);
if ($order != 'DESC' && $order != 'ASC') {
$order = 'DESC';
}
if (!in_array($col, $cols)) {
$col = 'date';
}
if (is_array($content) && !array_key_exists('_sortcol', $content)) {
$folder_sort_col_select = new html_select(array('name' => '_sortcol', 'id' => '_sortcol'));
foreach ($cols as $temp_col) {
$folder_sort_col_select->add($this->gettext($temp_col), $temp_col);
}
$content['_sortcol'] = array(
'label' => $this->gettext('listsorting'),
'value' => $folder_sort_col_select->show($col),
);
}
if (is_array($content) && !array_key_exists('_sortcol', $options)) {
$options['_sortcol'] = $col;
}
if (is_array($content) && !array_key_exists('_sortord', $content)) {
$folder_sort_order_select = new html_select(array('name' => '_sortord', 'id' => '_sortord'));
$folder_sort_order_select->add($this->gettext('asc'), 'ASC');
$folder_sort_order_select->add($this->gettext('desc'), 'DESC');
$content['_sortord'] = array(
'label' => $this->gettext('listorder'),
'value' => $folder_sort_order_select->show($order),
);
}
if (is_array($content) && !array_key_exists('_sortord', $options)) {
$options['_sortord'] = $order;
}
$args['form']['props']['fieldsets']['settings']['content'] = $content;
$args['options'] = $options;
return $args;
}
public function folder_update_hook($args)
{
$mbox = $args['record']['name'];
$settings = $args['record']['settings'];
$sort_order = $settings['sort_column'] . '_' . $settings['sort_order'];
$cfg_sort = $this->sort_order;
$cfg_sort[$mbox] = $sort_order;
$this->sort_order = $cfg_sort;
$this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
$this->rc->output->set_env('per_folder_sort', $this->sort_order);
return $args;
}
public function preferences_list_hook($args)
{
if ($args['section'] == 'mailbox') {
$cols = array(
'from',
'to',
'subject',
'date',
'size',
);
$folder_sorts = $this->sort_order;
if (array_key_exists('default', $folder_sorts)) {
$folder_sort = $folder_sorts['default'];
} else {
$folder_sort = 'date_DESC';
}
list($col, $order) = explode('_', $folder_sort);
if ($order != 'DESC' && $order != 'ASC') {
$order = 'DESC';
}
if (!in_array($col, $cols)) {
$col = 'date';
}
$sort_select_col = new html_select(array('name' => '_default_sort_col', 'id' => '_default_sort_col'));
foreach ($cols as $temp_col) {
$sort_select_col->add($this->gettext($temp_col), $temp_col);
}
$sort_select_order = new html_select(array('name' => '_default_sort_order', 'id' => '_default_sort_order'));
$sort_select_order->add($this->gettext('asc'), 'ASC');
$sort_select_order->add($this->gettext('desc'), 'DESC');
$sort_options = array(
'title' => $this->gettext('listorder'),
'content' => $sort_select_col->show($col) . $sort_select_order->show($order),
);
$args['blocks']['main']['options']['listorder'] = $sort_options;
}
return $args;
}
public function preferences_save_hook($args)
{
if ($args['section'] == 'mailbox') {
$folder_sort_col = rcube_utils::get_input_value('_default_sort_col', rcube_utils::INPUT_POST);
$folder_sort_order = rcube_utils::get_input_value('_default_sort_order', rcube_utils::INPUT_POST);
$folder_sort = $folder_sort_col . '_' . $folder_sort_order;
$folder_sorts = $this->sort_order;
$folder_sorts['default'] = $folder_sort;
$args['prefs']['per_folder_sort'] = $folder_sorts;
}
return $args;
}
public function sort_json_action()
{
$cmd = rcube_utils::get_input_value('cmd', rcube_utils::INPUT_POST);
$folder = rcube_utils::get_input_value('folder', rcube_utils::INPUT_POST);
$col = rcube_utils::get_input_value('col', rcube_utils::INPUT_POST);
$order = rcube_utils::get_input_value('order', rcube_utils::INPUT_POST);
switch ($cmd) {
case 'change_session': {
$_SESSION['sort_col'] = $col;
$_SESSION['sort_order'] = $order;
}
case 'save_order': {
$sort_order = $this->sort_order;
$sort_order[$folder] = $col . "_" . $order;
$this->sort_order = $sort_order;
$this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
$this->rc->output->set_env('per_folder_sort', $this->sort_order);
break;
}
}
}
private function _debug($value, $key = '', $force = false)
{
if ($this->debug || $force) {
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
$caller_trace = $trace[0];
$caller_function = $trace[1]['function'];
$caller_line = $caller_trace['line'];
$caller_file = $caller_trace['file'];
$caller_file = preg_replace("|.*/|", "", $caller_file);
$str = sprintf("[%s:%d - %s] ", $caller_file, $caller_line, $caller_function);
$val_type = gettype($value);
switch ($val_type) {
case "object": {
$old_value = $value;
$value = get_class($old_value);
$str .= $key . ' type = ' . $value;
break;
}
default: {
$old_value = $value;
$value = var_export($old_value, true);
$str .= $key. ' = ' .$value;
break;
}
}
if ($this->uname) {
$str = sprintf("[%s] %s", $this->uname, $str);
}
rcmail::write_log($this->ID, $str);
}
}
}
?>