1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel tickets module |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-ticket |
6
|
|
|
* @package hipanel-module-ticket |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\ticket\models; |
12
|
|
|
|
13
|
|
|
use hipanel\behaviors\File; |
14
|
|
|
use hipanel\helpers\Markdown; |
15
|
|
|
use hipanel\modules\client\models\Client; |
16
|
|
|
use stdClass; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\helpers\HtmlPurifier; |
19
|
|
|
use yii\web\NotFoundHttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Ticket. |
23
|
|
|
* |
24
|
|
|
* @property string $priority |
25
|
|
|
* @property string $responsible_id |
26
|
|
|
*/ |
27
|
|
|
class Thread extends \hipanel\base\Model |
28
|
|
|
{ |
29
|
|
|
use \hipanel\base\ModelTrait; |
30
|
|
|
|
31
|
|
|
const DEFAULT_SHOW_ALL = 'all'; |
32
|
|
|
|
33
|
|
|
public static $i18nDictionary = 'hipanel:ticket'; |
34
|
|
|
|
35
|
|
|
const STATE_OPEN = 'opened'; |
36
|
|
|
const STATE_CLOSE = 'closed'; |
37
|
|
|
|
38
|
|
|
const PRIORITY_HIGH = 'high'; |
39
|
|
|
|
40
|
|
|
public $search_form; |
41
|
|
|
|
42
|
|
|
public function init() |
43
|
|
|
{ |
44
|
|
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']); |
45
|
|
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function beforeChange($event) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->prepareSpentTime(); |
51
|
|
|
$this->prepareTopic(); |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
[ |
63
|
|
|
'class' => File::class, |
64
|
|
|
'attribute' => 'file', |
65
|
|
|
'targetAttribute' => 'file_ids', |
66
|
|
|
'scenarios' => ['create', 'answer'], |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function attributes() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
'id', |
78
|
|
|
'subject', |
79
|
|
|
'state', |
80
|
|
|
'state_label', |
81
|
|
|
'author_email', |
82
|
|
|
'author', |
83
|
|
|
'author_id', |
84
|
|
|
'responsible_id', |
85
|
|
|
'responsible_email', |
86
|
|
|
'author_seller', |
87
|
|
|
'author_seller_id', |
88
|
|
|
'recipient_id', |
89
|
|
|
'recipient', |
90
|
|
|
'recipient_seller', |
91
|
|
|
'recipient_seller_id', |
92
|
|
|
'replier_id', |
93
|
|
|
'replier', |
94
|
|
|
'replier_seller', |
95
|
|
|
'replier_name', |
96
|
|
|
'responsible', |
97
|
|
|
'priority', |
98
|
|
|
'priority_label', |
99
|
|
|
'spent', 'spent_hours', |
100
|
|
|
'answer_count', |
101
|
|
|
'status', |
102
|
|
|
'reply_time', |
103
|
|
|
'create_time', |
104
|
|
|
'a_reply_time', |
105
|
|
|
'elapsed', |
106
|
|
|
'topics', |
107
|
|
|
'topic', |
108
|
|
|
'watchers', |
109
|
|
|
'watcher_ids', |
110
|
|
|
'watcher', |
111
|
|
|
'watcher_id', |
112
|
|
|
'add_tag_ids', |
113
|
|
|
'file_ids', |
114
|
|
|
'file', |
115
|
|
|
'message', // 'answer_message', |
116
|
|
|
'is_private', |
117
|
|
|
|
118
|
|
|
'anonym_name', |
119
|
|
|
'anonym_email', |
120
|
|
|
'anonym_seller', |
121
|
|
|
|
122
|
|
|
'lastanswer', |
123
|
|
|
'time', |
124
|
|
|
'add_watchers', 'del_watchers', |
125
|
|
|
|
126
|
|
|
'time_from', |
127
|
|
|
'time_till', |
128
|
|
|
|
129
|
|
|
'contact', |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function rules() |
137
|
|
|
{ |
138
|
|
|
$rules = [ |
139
|
|
|
[['author_id', 'responsible_id'], 'integer'], |
140
|
|
|
[['subject', 'message'], 'required', 'on' => ['create']], |
141
|
|
|
[['subject'], 'string', 'min' => 3], |
142
|
|
|
[['id'], 'required', 'on' => ['answer', 'update-answer', 'open', 'close']], |
143
|
|
|
[['recipient_id'], 'required', 'when' => function () { |
144
|
|
|
return Yii::$app->user->can('support'); |
145
|
|
|
}, 'on' => 'create'], |
146
|
|
|
[ |
147
|
|
|
[ |
148
|
|
|
'topics', |
149
|
|
|
'state', |
150
|
|
|
'priority', |
151
|
|
|
'responsible', |
152
|
|
|
'recipient_id', |
153
|
|
|
'watchers', |
154
|
|
|
'spent', 'spent_hours', |
155
|
|
|
'file_ids', |
156
|
|
|
], |
157
|
|
|
'safe', |
158
|
|
|
'on' => 'create', |
159
|
|
|
], |
160
|
|
|
[ |
161
|
|
|
[ |
162
|
|
|
'message', |
163
|
|
|
'topics', 'state', 'priority', |
164
|
|
|
'responsible', 'recipient_id', |
165
|
|
|
'watchers', 'add_watchers', 'del_watchers', 'watcher_ids', |
166
|
|
|
'is_private', |
167
|
|
|
'file_ids', |
168
|
|
|
'spent', 'spent_hours', |
169
|
|
|
], |
170
|
|
|
'safe', |
171
|
|
|
'on' => 'answer', |
172
|
|
|
], |
173
|
|
|
[['state'], 'safe', 'on' => ['close', 'open']], |
174
|
|
|
// only client-side validation. Answer is actually possible without a message, |
175
|
|
|
// but does not make any sense. |
176
|
|
|
[['message'], 'required', 'on' => ['answer'], 'when' => function () { |
177
|
|
|
return false; |
178
|
|
|
}], |
179
|
|
|
[['state'], 'default', 'value' => self::DEFAULT_SHOW_ALL, 'on' => ['default']], |
180
|
|
|
[['id'], 'integer', 'on' => 'answer'], |
181
|
|
|
[['file'], 'file', 'maxFiles' => 15], |
182
|
|
|
[['lastanswer', 'create_time', 'recipient'], 'safe'], |
183
|
|
|
[['author', 'author_seller'], 'safe', 'when' => Yii::$app->user->can('support')], |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
return $rules; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function attributeLabels() |
193
|
|
|
{ |
194
|
|
|
return $this->mergeAttributeLabels([ |
195
|
|
|
'author' => Yii::t('hipanel:ticket', 'Author'), |
196
|
|
|
'author_id' => Yii::t('hipanel:ticket', 'Author'), |
197
|
|
|
'recipient' => Yii::t('hipanel:ticket', 'Recipient'), |
198
|
|
|
'is_private' => Yii::t('hipanel:ticket', 'Make private'), |
199
|
|
|
'responsible' => Yii::t('hipanel:ticket', 'Assignee'), |
200
|
|
|
'responsible_id' => Yii::t('hipanel:ticket', 'Assignee'), |
201
|
|
|
'spent' => Yii::t('hipanel:ticket', 'Spent time'), |
202
|
|
|
'create_time' => Yii::t('hipanel:ticket', 'Created'), |
203
|
|
|
'a_reply_time' => Yii::t('hipanel:ticket', 'a_reply_time'), |
204
|
|
|
'file' => Yii::t('hipanel:ticket', 'Files'), |
205
|
|
|
'lastanswer' => Yii::t('hipanel:ticket', 'Last answer'), |
206
|
|
|
'author_seller' => Yii::t('hipanel:ticket', 'Seller'), |
207
|
|
|
'watcher_ids' => Yii::t('hipanel:ticket', 'Watchers'), |
208
|
|
|
]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getClient() |
212
|
|
|
{ |
213
|
|
|
return $this->author; |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function getClient_id() |
217
|
|
|
{ |
218
|
|
|
return $this->author_id; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getSeller() |
222
|
|
|
{ |
223
|
|
|
return $this->author_seller; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getSeller_id() |
227
|
|
|
{ |
228
|
|
|
return $this->author_seller_id; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getThreadUrlArray() |
232
|
|
|
{ |
233
|
|
|
return ['@ticket/view', 'id' => $this->id]; |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public static function parseMessage($message) |
237
|
|
|
{ |
238
|
|
|
$message = str_replace(["\n\r", "\r\r", "\r\n"], "\n", $message); // "\n\n", |
239
|
|
|
$message = Markdown::process($message); |
240
|
|
|
$message = HtmlPurifier::process($message, [ |
241
|
|
|
'HTML.SafeObject' => true, // Allow safe HTML entities |
242
|
|
|
'Core.EscapeInvalidTags' => true, // Escape not allowed tags instead of stripping them |
243
|
|
|
'Core.LexerImpl' => 'DirectLex', // Do not try to close unclosed tags |
244
|
|
|
]); |
245
|
|
|
|
246
|
|
|
return $message; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function prepareSpentTime() |
250
|
|
|
{ |
251
|
|
|
list($this->spent_hours, $this->spent) = explode(':', $this->spent, 2); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function prepareTopic() |
255
|
|
|
{ |
256
|
|
|
$this->topics = is_array($this->topics) ? implode(',', $this->topics) : $this->topics; |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function getWatchersLogin() |
260
|
|
|
{ |
261
|
|
|
$results = []; |
262
|
|
|
foreach ((array)$this->watchers as $id => $watcher) { |
263
|
|
|
list($login, $email) = explode(' ', $watcher); |
|
|
|
|
264
|
|
|
$results[$id] = $login; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $results; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function xFormater(array $items) |
271
|
|
|
{ |
272
|
|
|
$result = []; |
273
|
|
|
foreach ($items as $id => $label) { |
274
|
|
|
$object = new stdClass(); |
275
|
|
|
$object->value = $id; |
276
|
|
|
$object->text = $label; |
277
|
|
|
$result[] = $object; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $result; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function getAnswers() |
284
|
|
|
{ |
285
|
|
|
// TODO: redo API in order to have different `Thread` and `ThreadMessage` models |
286
|
|
|
return $this->hasMany(Answer::class, ['id' => 'id'])->joinWith('files')->indexBy('answer_id'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns array of client types, that can be set as responsible for the thread. |
291
|
|
|
* |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
|
|
public static function getResponsibleClientTypes() |
295
|
|
|
{ |
296
|
|
|
return [Client::TYPE_SELLER, Client::TYPE_ADMIN, Client::TYPE_MANAGER, Client::TYPE_OWNER]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param integer $id |
301
|
|
|
* @param bool $throwOnError whether to throw an exception when answer is not found in thread |
302
|
|
|
* @throws NotFoundHttpException |
303
|
|
|
* @return Answer |
304
|
|
|
*/ |
305
|
|
|
public function getAnswer($id, $throwOnError = true) |
306
|
|
|
{ |
307
|
|
|
if (!isset($this->answers[$id]) && $throwOnError) { |
|
|
|
|
308
|
|
|
throw new NotFoundHttpException('Answer does not belong to this model'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $this->answers[$id]; |
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function getMaxAnswerId() |
315
|
|
|
{ |
316
|
|
|
if ($this->isRelationPopulated('answers')) { |
317
|
|
|
return max(array_keys($this->answers)); |
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return $this->id; |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function scenarioActions() |
324
|
|
|
{ |
325
|
|
|
return [ |
326
|
|
|
'open' => 'answer', |
327
|
|
|
'close' => 'answer', |
328
|
|
|
]; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function canSetSpent() |
332
|
|
|
{ |
333
|
|
|
if (isset(Yii::$app->params['ticket.canSetSpent']) && is_callable(Yii::$app->params['ticket.canSetSpent'])) { |
334
|
|
|
return call_user_func(Yii::$app->params['ticket.canSetSpent'], $this); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return true; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function isOpen() |
341
|
|
|
{ |
342
|
|
|
return $this->state === self::STATE_OPEN; |
|
|
|
|
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function isHighPriority(): bool |
346
|
|
|
{ |
347
|
|
|
return $this->priority === self::PRIORITY_HIGH; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function isUserAssigned(): bool |
351
|
|
|
{ |
352
|
|
|
return $this->responsible_id === Yii::$app->getUser()->getId(); |
|
|
|
|
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.