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