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