Completed
Push — master ( b84cd2...e67b3f )
by Andrii
12:52
created

src/models/Answer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\models\File;
14
use Yii;
15
16
/**
17
 * Class Answer.
18
 *
19
 * @property Thread $thread
20
 */
21
class Answer extends \hipanel\base\Model
22
{
23
    use \hipanel\base\ModelTrait;
24
25
    public static $i18nDictionary = 'hipanel:ticket';
26
27
    public static function tableName()
28
    {
29
        return 'thread';
30
    }
31
32
    public function init()
33
    {
34
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
35
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
36
    }
37
38
    public function load($data, $formName = null)
39
    {
40
        $result = parent::load($data, $formName);
41
        $this->prepareSpentTime();
42
43
        return $result;
44
    }
45
46
    public function beforeChange($event)
47
    {
48
        $this->switchId();
49
50
        return true;
51
    }
52
53
    private function switchId()
54
    {
55
        if ($this->scenario === 'update') {
56
            $this->id = $this->answer_id;
57
        }
58
    }
59
60
    /**
61
     * @return array
62
     */
63 View Code Duplication
    public function behaviors()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        return [
66
            [
67
                'class' => 'hipanel\behaviors\File',
68
                'attribute' => 'file',
69
                'targetAttribute' => 'file_ids',
70
                'scenarios' => ['create', 'answer'],
71
            ],
72
        ];
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function attributes()
79
    {
80
        return [
81
            'id',
82
            'answer_id',
83
            'message',
84
            'private',
85
            'is_private',
86
            'is_answer',
87
            'changed_num',
88
            'author',
89
            'author_id',
90
            'author_seller',
91
            'seller_id',
92
            'account',
93
            'email_hash',
94
            'create_time',
95
            'spent',
96
            'spent_hours',
97
            'is_moved',
98
            'ip',
99
            'file',
100
        ];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function rules()
107
    {
108
        return [
109
            [
110
                ['id', 'answer_id', 'spent', 'spent_hours'],
111
                'integer',
112
                'enableClientValidation' => false,
113
            ],
114
            [
115
                ['message', 'id', 'answer_id'],
116
                'required',
117
                'on' => 'update',
118
            ],
119
            [
120
                ['spent', '!spent_hours'],
121
                'safe',
122
                'on' => 'update',
123
            ],
124
            [
125
                ['is_private'],
126
                'boolean',
127
                'on' => 'update',
128
                'when' => function () {
129
                    return Yii::$app->user->can('support');
130
                },
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function attributeLabels()
139
    {
140
        return $this->mergeAttributeLabels([
141
            'author' => Yii::t('hipanel:ticket', 'Author'),
142
            'author_id' => Yii::t('hipanel:ticket', 'Author'),
143
            'recipient' => Yii::t('hipanel:ticket', 'Recipient'),
144
            'is_private' => Yii::t('hipanel:ticket', 'Make private'),
145
            'responsible' => Yii::t('hipanel:ticket', 'Assignee'),
146
            'responsible_id' => Yii::t('hipanel:ticket', 'Assignee'),
147
            'spent' => Yii::t('hipanel:ticket', 'Spent time'),
148
            'create_time' => Yii::t('hipanel:ticket', 'Created'),
149
            'a_reply_time' => Yii::t('hipanel:ticket', 'a_reply_time'),
150
            'file' => Yii::t('hipanel:ticket', 'Files'),
151
            'lastanswer' => Yii::t('hipanel:ticket', 'Last answer'),
152
            'author_seller' => Yii::t('hipanel:ticket', 'Seller'),
153
        ]);
154
    }
155
156
    public function getClient()
157
    {
158
        return $this->author;
159
    }
160
161
    public function getClient_id()
162
    {
163
        return $this->author_id;
164
    }
165
166
    public function getSeller()
167
    {
168
        return $this->author_seller;
169
    }
170
171
    public function getSeller_id()
172
    {
173
        return $this->author_seller_id;
174
    }
175
176
    public function prepareSpentTime()
177
    {
178
        list($this->spent_hours, $this->spent) = explode(':', $this->spent, 2);
179
    }
180
181
    public function getThread()
182
    {
183
        return $this->hasOne(Thread::class, ['id' => 'id']);
184
    }
185
186
    public function getFiles()
187
    {
188
        return $this->hasMany(File::class, ['object_id' => 'id']);
189
    }
190
191
    public function scenarioActions()
192
    {
193
        return [
194
            'update' => 'update-answer',
195
        ];
196
    }
197
198
    public function canSetSpent()
199
    {
200
        return $this->thread->canSetSpent();
201
    }
202
203
    public function isOpen()
204
    {
205
        return $this->thread->isOpen();
206
    }
207
}
208