-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbits_vacation.php
More file actions
234 lines (196 loc) · 10.1 KB
/
cbits_vacation.php
File metadata and controls
234 lines (196 loc) · 10.1 KB
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
<?php
require 'drivers/vacationdriver.php';
class cbits_vacation extends rcube_plugin {
public $noframe = true;
public $noajax = true;
private $rc;
public $data;
function init() {
$this->rc = rcmail::get_instance();
$this->add_texts('localization/');
$this->load_driver();
$this->add_hook('settings_actions', array($this, 'settings_actions'));
$this->register_action('plugin.cbits_vacation', array($this, 'vacation_init'));
$this->register_action('plugin.cbits_vacation.save', array($this, 'vacation_save'));
}
function settings_actions($args) {
$args['actions'][] = array(
'action' => 'plugin.cbits_vacation',
'class' => 'vacation',
'label' => 'setvacation',
'title' => 'setvacation',
'domain' => 'cbits_vacation',
);
return $args;
}
function vacation_init() {
$this->register_handler('plugin.body', array($this, 'vacation_form'));
$this->rc->output->set_pagetitle($this->gettext('setvacation'));
$this->rc->output->send('plugin');
}
function vacation_save() {
$this->register_handler('plugin.body', array($this, 'create_form'));
$this->rc->output->set_pagetitle($this->gettext('setvacation'));
// data we will pass to the driver
$data_for_backend = [];
// data we have been sent, and will send back to the client on error
$this->data = [
'active' => rcube_utils::get_input_value('active', rcube_utils::INPUT_POST),
'forwarding_address' => rcube_utils::get_input_value('forwarding_address', rcube_utils::INPUT_POST),
'start_datetime' => rcube_utils::get_input_value('start_datetime', rcube_utils::INPUT_POST),
'end_datetime' => rcube_utils::get_input_value('end_datetime', rcube_utils::INPUT_POST),
'message' => rcube_utils::get_input_value('message', rcube_utils::INPUT_POST, true),
];
if ($this->data['active'] == "off") {
$data_for_backend['enabled'] = false;
$data_for_backend['start_datetime'] = null;
$data_for_backend['end_datetime'] = null;
} elseif ($this->data['active'] == "on-indef") {
$data_for_backend['enabled'] = true;
$data_for_backend['start_datetime'] = null;
$data_for_backend['end_datetime'] = null;
} elseif ($this->data['active'] == "on-dates") {
$data_for_backend['enabled'] = true;
$start_datetime = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', $this->data['start_datetime']);
if ($start_datetime === false) {
$this->send_error("Start datetime is in an invalid format, needs to be in form of " . date('Y-m-d\TH:i'));
}
$data_for_backend['start_datetime'] = $start_datetime;
$end_datetime = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', $this->data['end_datetime']);
if ($end_datetime === false) {
$this->send_error("End datetime is in an invalid format, needs to be in form of " . date('Y-m-d\TH:i'));
}
$data_for_backend['end_datetime'] = $end_datetime;
if ($data_for_backend['start_datetime'] > $data_for_backend['end_datetime']) {
$this->send_error("Start datetime should be before end datetime");
}
} else {
$this->send_error('Invalid value for "enabled" field');
}
$data_for_backend['message'] = rcube_utils::get_input_value('message', rcube_utils::INPUT_POST, true);
$data_for_backend['forward'] = rcube_utils::get_input_value('forwarding_address', rcube_utils::INPUT_POST);
if (trim($data_for_backend['forward']) == '') {
$data_for_backend['forward'] = null;
}
if ($this->save($data_for_backend)) {
$this->rc->output->command('display_message', 'Out of Office settings saved successfully', 'confirmation');
} else {
$this->rc->output->command('display_message', 'Out of Office settings not saved', 'error');
}
$this->data = $this->get();
$this->rc->overwrite_action('plugin.cbits_vacation');
$this->rc->output->send('plugin');
}
function send_error($error) {
$this->rc->output->command('display_message', $error, 'error');
$this->rc->overwrite_action('plugin.cbits_vacation');
$this->rc->output->send('plugin');
}
// creates form from data on backend
function vacation_form() {
$this->data = $this->get();
return $this->create_form();
}
// creates form from data stored in object
function create_form() {
$data = $this->data;
$this->rc->html_editor('identity');
$output = '';
$output = '<style>#layout-content { overflow: auto; height: 100%; }</style>';
$output .= '<div style="margin: 1em;">';
$output .= '<h3>Out of Office Autoresponder Settings</h3>';
$output .= html::div(['class' => 'form-group btn-group btn-group-toggle', 'data-toggle' => 'buttons'], ''
. html::label(['class' => $data['active'] == 'off' ? 'btn btn-outline-primary active' : 'btn btn-outline-primary'],
(new html_radiobutton(['name' => 'active']))->show($data['active'], ['value' => 'off']) . "Off"
)
. html::label(['class' => $data['active'] == 'on-indef' ? 'btn btn-outline-primary active' : 'btn btn-outline-primary'],
(new html_radiobutton(['name' => 'active']))->show($data['active'], ['value' => 'on-indef']) . "Enabled indefinitely"
)
. html::label(['class' => $data['active'] == 'on-dates' ? 'btn btn-outline-primary active' : 'btn btn-outline-primary'],
(new html_radiobutton(['name' => 'active']))->show($data['active'], ['value' => 'on-dates']) . "Enabled between date range"
)
);
$output .= html::div(['class' => 'form-group', 'id' => 'vacation-start_datetime-row'], ''
. html::label('start_datetime', rcube::Q($this->gettext('vacstartdate')))
. (new html_inputfield(['name' => 'start_datetime', 'type' => 'datetime-local', 'class' => 'form-control']))->show($data['start_datetime'])
);
$output .= html::div(['class' => 'form-group', 'id' => 'vacation-end_datetime-row'], ''
. html::label('end_datetime', rcube::Q($this->gettext('vacenddate')))
. (new html_inputfield(['name' => 'end_datetime', 'type' => 'datetime-local', 'class' => 'form-control']))->show($data['end_datetime'])
);
$output .= html::div(['class' => 'form-group', 'id' => 'vacation-forwarding_address-row'], ''
. html::label('forwarding_address', rcube::Q($this->gettext('vacforward')))
. (new html_inputfield(['name' => 'forwarding_address', 'type' => 'email', 'class' => 'form-control']))->show($data['forward'])
. html::tag('small', ['class' => 'form-test text-muted'], 'Forward a copy of the emails received to this address. Leave blank to not forward to anyone')
);
$output .= html::div(['class' => 'form-group', 'id' => 'vacation-message-row'], ''
. html::label('message', rcube::Q($this->gettext('vacmessage')))
. (new html_textarea(['name' => 'message', 'id' => 'vacation-message', 'class' => 'form-control']))->show($data['message'], ['class' => 'mce_editor'])
);
$output .= '<div>' . $this->rc->output->button(array(
'command' => 'plugin.cbits_vacation.save',
'class' => 'button mainaction submit',
'label' => 'save',
)) . '</div>';
$output .= '</div>';
$this->rc->output->add_gui_object('vacform', 'cbits_vacation-form');
$this->include_script('cbits_vacation.js');
return $this->rc->output->form_tag(array(
'id' => 'cbits_vacation-form',
'name' => 'cbits_vacation-form',
'method' => 'post',
'action' => './?_task=settings&_action=plugin.cbits_vacation.save',
), $output);
}
function get() {
$data = $this->driver->get();
if ($data['enabled'] === true) {
if ($data['start_datetime'] !== null && $data['end_datetime'] !== null) {
$data['active'] = "on-dates";
} else {
$data['active'] = "on-indef";
}
} else {
$data['active'] = "off";
}
// we use 'active'
unset($this->data['enabled']);
$data['start_datetime'] = is_null($data['start_datetime']) ? null : $data['start_datetime']->format('Y-m-d\TH:i');
$data['end_datetime'] = is_null($data['end_datetime']) ? null : $data['end_datetime']->format('Y-m-d\TH:i');
return $data;
}
function save($data) {
return $this->driver->save($data);
}
private function load_driver() {
$driver = $this->rc->config->get('vacation_driver', null);
$class = "rcube_{$driver}_vacation";
$file = $this->home . "/drivers/$driver.php";
if (!file_exists($file)) {
rcube::raise_error(array(
'code' => 600,
'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => "Vacation plugin: Driver file does not exist ($file)"
), true, false);
return false;
}
include_once $file;
if (!class_exists($class, false) || (!method_exists($class, 'save') && !method_exists($class, 'get'))) {
rcube::raise_error(array(
'code' => 600,
'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => "Vacation plugin: Broken driver $driver"
), true, false);
return false;
}
$this->driver = new $class;
$this->driver->rc = rcmail::get_instance();
$this->driver->user = new stdClass;
$this->driver->user->username = $this->rc->user->get_username('local');
$this->driver->user->domain = $this->rc->user->get_username('domain');
$this->driver->init();
return true;
}
}