1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* MikoPBX - free phone system for small business |
4
|
|
|
* Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace MikoPBX\AdminCabinet\Controllers; |
21
|
|
|
|
22
|
|
|
use MikoPBX\AdminCabinet\Forms\TimeFrameEditForm; |
23
|
|
|
use MikoPBX\Common\Models\{Extensions, |
24
|
|
|
IncomingRoutingTable, |
25
|
|
|
OutWorkTimes, |
26
|
|
|
OutWorkTimesRouts, |
27
|
|
|
Sip, |
28
|
|
|
SoundFiles}; |
29
|
|
|
|
30
|
|
|
class OutOffWorkTimeController extends BaseController |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Построение списка правила маршрутизации в нерабочее время |
36
|
|
|
*/ |
37
|
|
|
public function indexAction(): void |
38
|
|
|
{ |
39
|
|
|
$paremeters = [ |
40
|
|
|
'order' => 'date_from, weekday_from, time_from', |
41
|
|
|
]; |
42
|
|
|
$timeframesTable = []; |
43
|
|
|
$timeFrames = OutWorkTimes::find($paremeters); |
44
|
|
|
foreach ($timeFrames as $timeFrame) { |
45
|
|
|
if(mb_strlen($timeFrame->description) < 50){ |
46
|
|
|
$shot_description = $timeFrame->description; |
47
|
|
|
}else{ |
48
|
|
|
$shot_description = trim(mb_substr($timeFrame->description, 0 , 50)).'...'; |
49
|
|
|
} |
50
|
|
|
$timeframesTable[] = [ |
51
|
|
|
'id' => $timeFrame->id, |
52
|
|
|
'date_from' => ( ! empty($timeFrame->date_from)) |
53
|
|
|
> 0 ? date( |
54
|
|
|
"d.m.Y", |
55
|
|
|
$timeFrame->date_from |
56
|
|
|
) : '', |
57
|
|
|
'date_to' => ( ! empty($timeFrame->date_to)) > 0 |
58
|
|
|
? date("d.m.Y", $timeFrame->date_to) : '', |
59
|
|
|
'weekday_from' => ( ! empty($timeFrame->weekday_from)) ? $this->translation->_( |
60
|
|
|
date( |
61
|
|
|
'D', |
62
|
|
|
strtotime("Sunday +{$timeFrame->weekday_from} days") |
63
|
|
|
) |
64
|
|
|
) : '', |
65
|
|
|
'weekday_to' => ( ! empty($timeFrame->weekday_to)) ? $this->translation->_( |
66
|
|
|
date( |
67
|
|
|
'D', |
68
|
|
|
strtotime("Sunday +{$timeFrame->weekday_to} days") |
69
|
|
|
) |
70
|
|
|
) : '', |
71
|
|
|
'time_from' => $timeFrame->time_from, |
72
|
|
|
'time_to' => $timeFrame->time_to, |
73
|
|
|
'action' => $timeFrame->action, |
74
|
|
|
'audio_message_id' => ($timeFrame->SoundFiles) ? $timeFrame->SoundFiles->name : '', |
75
|
|
|
'extension' => ($timeFrame->Extensions) ? $timeFrame->Extensions->getRepresent() : '', |
76
|
|
|
'description' => $timeFrame->description, |
77
|
|
|
'allowRestriction' => $timeFrame->allowRestriction, |
78
|
|
|
'shot_description' => $shot_description, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->view->indexTable = $timeframesTable; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Карточка редактирования записи нерабочего времени |
87
|
|
|
* |
88
|
|
|
* @param string $id |
89
|
|
|
*/ |
90
|
|
|
public function modifyAction($id = ''): void |
91
|
|
|
{ |
92
|
|
|
$timeFrame = OutWorkTimes::findFirstById($id); |
93
|
|
|
if ($timeFrame === null) { |
94
|
|
|
$timeFrame = new OutWorkTimes(); |
95
|
|
|
} |
96
|
|
|
$forwardingExtensions = []; |
97
|
|
|
$forwardingExtensions[""] = $this->translation->_("ex_SelectNumber"); |
98
|
|
|
$parameters = [ |
99
|
|
|
'conditions' => 'number = :extension:', |
100
|
|
|
'bind' => [ |
101
|
|
|
'extension' => $timeFrame->extension, |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
$extensions = Extensions::find($parameters); |
105
|
|
|
foreach ($extensions as $record) { |
106
|
|
|
$forwardingExtensions[$record->number] = $record->getRepresent(); |
107
|
|
|
} |
108
|
|
|
$audioMessages = []; |
109
|
|
|
$audioMessages[""] = $this->translation->_("sf_SelectAudioFile"); |
110
|
|
|
$soundFiles = SoundFiles::find('category="custom"'); |
111
|
|
|
foreach ($soundFiles as $record) { |
112
|
|
|
$audioMessages[$record->id] = $record->name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$availableActions = [ |
116
|
|
|
'playmessage' => $this->translation->_('tf_SelectActionPlayMessage'), |
117
|
|
|
'extension' => $this->translation->_('tf_SelectActionRedirectToExtension'), |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$weekDays = ['-1' => '-']; |
121
|
|
|
for ($i = "1"; $i <= 7; $i++) { |
122
|
|
|
$weekDays[$i] = $this->translation->_(date('D', strtotime("Sunday +{$i} days"))); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$form = new TimeFrameEditForm( |
126
|
|
|
$timeFrame, [ |
127
|
|
|
'extensions' => $forwardingExtensions, |
128
|
|
|
'audio-message' => $audioMessages, |
129
|
|
|
'available-actions' => $availableActions, |
130
|
|
|
'week-days' => $weekDays, |
131
|
|
|
] |
132
|
|
|
); |
133
|
|
|
$this->view->form = $form; |
134
|
|
|
$this->view->represent = $timeFrame->getRepresent(); |
135
|
|
|
|
136
|
|
|
// Получим список ссылок на разрещенные правила маршутизации в этой группе |
137
|
|
|
$parameters = [ |
138
|
|
|
'columns' => 'routId AS rule_id', |
139
|
|
|
'conditions' => 'timeConditionId=:timeConditionId:', |
140
|
|
|
'bind' => [ |
141
|
|
|
'timeConditionId' => $id, |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
$allowedRules = OutWorkTimesRouts::find($parameters)->toArray(); |
145
|
|
|
$allowedRulesIds = array_column($allowedRules, 'rule_id'); |
146
|
|
|
|
147
|
|
|
// Получим список правил маршрутизации |
148
|
|
|
$rules = IncomingRoutingTable::find(['order' => 'priority', 'conditions' => 'id>1']); |
149
|
|
|
$routingTable = []; |
150
|
|
|
foreach ($rules as $rule) { |
151
|
|
|
$provider = $rule->Providers; |
152
|
|
|
if ($provider) { |
153
|
|
|
$modelType = ucfirst($provider->type); |
154
|
|
|
$provByType = $provider->$modelType; |
155
|
|
|
} else { |
156
|
|
|
$provByType = new SIP(); |
157
|
|
|
} |
158
|
|
|
$extension = $rule->Extensions; |
159
|
|
|
$values = [ |
160
|
|
|
'id' => $rule->id, |
161
|
|
|
'rulename' => $rule->rulename, |
162
|
|
|
'priority' => $rule->priority, |
163
|
|
|
'number' => $rule->number, |
164
|
|
|
'timeout' => $rule->timeout, |
165
|
|
|
'provider' => $rule->Providers ? $rule->Providers->getRepresent() : '', |
166
|
|
|
'provider-uniqid' => $rule->Providers ? $rule->Providers->uniqid : 'none', |
167
|
|
|
'disabled' => $provByType->disabled, |
168
|
|
|
'extension' => $rule->extension, |
169
|
|
|
'callerid' => $extension ? $extension->getRepresent() : '', |
170
|
|
|
'note' => $rule->note, |
171
|
|
|
'status' => in_array($rule->id, $allowedRulesIds, true) ? '' : 'disabled', |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$routingTable[] = $values; |
175
|
|
|
} |
176
|
|
|
$this->view->rules = $routingTable; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Сохранение записи нерабочего времени |
181
|
|
|
*/ |
182
|
|
|
public function saveAction(): void |
183
|
|
|
{ |
184
|
|
|
if ( ! $this->request->isPost()) { |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
$data = $this->request->getPost(); |
188
|
|
|
|
189
|
|
|
$this->db->begin(); |
190
|
|
|
$timeFrame = OutWorkTimes::findFirstByid($data['id']); |
191
|
|
|
if ($timeFrame === null) { |
192
|
|
|
$timeFrame = new OutWorkTimes(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Заполним параметры пользователя |
196
|
|
|
foreach ($timeFrame as $name => $value) { |
197
|
|
|
switch ($name) { |
198
|
|
|
case 'weekday_from': |
199
|
|
|
case 'weekday_to': |
200
|
|
|
if ( ! array_key_exists($name, $data)) { |
201
|
|
|
$timeFrame->$name = ''; |
202
|
|
|
} else { |
203
|
|
|
$timeFrame->$name = ($data[$name] < 1) ? null : $data[$name]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
break; |
207
|
|
|
case 'allowRestriction': |
208
|
|
|
if(isset($data[$name])){ |
209
|
|
|
$timeFrame->$name = ($data[$name] === 'on') ? "1" : "0"; |
210
|
|
|
}else{ |
211
|
|
|
$timeFrame->$name = '0'; |
212
|
|
|
} |
213
|
|
|
break; |
214
|
|
|
case 'date_from': |
215
|
|
|
case 'date_to': |
216
|
|
|
case 'time_from': |
217
|
|
|
case 'time_to': |
218
|
|
|
if ( ! array_key_exists($name, $data)) { |
219
|
|
|
$timeFrame->$name = ''; |
220
|
|
|
} else { |
221
|
|
|
$timeFrame->$name = $data[$name]; |
222
|
|
|
} |
223
|
|
|
break; |
224
|
|
|
default: |
225
|
|
|
if ( ! array_key_exists($name, $data)) { |
226
|
|
|
continue 2; |
227
|
|
|
} |
228
|
|
|
$timeFrame->$name = $data[$name]; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if('playmessage' === $timeFrame->action){ |
233
|
|
|
$timeFrame->extension = ''; |
234
|
|
|
} |
235
|
|
|
$error = false; |
236
|
|
|
if ($timeFrame->save() === false) { |
237
|
|
|
$errors = $timeFrame->getMessages(); |
238
|
|
|
$this->flash->warning(implode('<br>', $errors)); |
239
|
|
|
$this->view->success = false; |
240
|
|
|
$this->db->rollback(); |
241
|
|
|
return; |
242
|
|
|
} |
243
|
|
|
if ( ! $error) { |
|
|
|
|
244
|
|
|
$data['id'] = $timeFrame->id; |
245
|
|
|
$error = ! $this->saveAllowedOutboundRules($data); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if($error){ |
249
|
|
|
$this->view->success = false; |
250
|
|
|
$this->db->rollback(); |
251
|
|
|
}else{ |
252
|
|
|
$this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
253
|
|
|
$this->view->success = true; |
254
|
|
|
$this->db->commit(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
// Если это было создание карточки то надо перегрузить страницу с указанием ID |
259
|
|
|
if (empty($data['id'])) { |
260
|
|
|
$this->view->reload = "out-off-work-time/modify/{$timeFrame->id}"; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Сохраняет параметры маршрутов |
266
|
|
|
* |
267
|
|
|
* @param $data |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
private function saveAllowedOutboundRules($data): bool |
272
|
|
|
{ |
273
|
|
|
// 1. Удалим все старые ссылки на правила относящиеся к этой группе |
274
|
|
|
$parameters = [ |
275
|
|
|
'conditions' => 'timeConditionId=:timeConditionId:', |
276
|
|
|
'bind' => [ |
277
|
|
|
'timeConditionId' => $data['id'], |
278
|
|
|
], |
279
|
|
|
]; |
280
|
|
|
$oldRules = OutWorkTimesRouts::find($parameters); |
281
|
|
|
if ($oldRules->delete() === false) { |
282
|
|
|
$errors = $oldRules->getMessages(); |
283
|
|
|
$this->flash->error(implode('<br>', $errors)); |
284
|
|
|
|
285
|
|
|
return false; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// 2. Запишем разрешенные направления |
289
|
|
|
foreach ($data as $key => $value) { |
290
|
|
|
if (substr_count($key, 'rule-') > 0) { |
291
|
|
|
$rule_id = explode('rule-', $key)[1]; |
292
|
|
|
if ($data[$key] === 'on') { |
293
|
|
|
$newRule = new OutWorkTimesRouts(); |
294
|
|
|
$newRule->id = $rule_id; |
|
|
|
|
295
|
|
|
$newRule->timeConditionId = $data['id']; |
296
|
|
|
$newRule->routId = $rule_id; |
|
|
|
|
297
|
|
|
if ($newRule->save() === false) { |
298
|
|
|
$errors = $newRule->getMessages(); |
299
|
|
|
$this->flash->error(implode('<br>', $errors)); |
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return true; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Удаление запси с данными о нерабочем времени |
311
|
|
|
* |
312
|
|
|
* @param string $id |
313
|
|
|
*/ |
314
|
|
|
public function deleteAction($id = '') |
315
|
|
|
{ |
316
|
|
|
$timeFrame = OutWorkTimes::findFirstByid($id); |
317
|
|
|
if ($timeFrame !== null) { |
318
|
|
|
$timeFrame->delete(); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$this->forward('OutOffWorkTime/index'); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
} |
326
|
|
|
|