Completed
Push — master ( 4839ed...c12feb )
by Klochok
27:31 queued 12:36
created

TicketGridView::columns()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 113
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 84
nc 1
nop 0
dl 0
loc 113
ccs 0
cts 103
cp 0
crap 6
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\grid;
12
13
use hipanel\grid\BoxedGridView;
14
use hipanel\modules\client\grid\ClientColumn;
15
use hipanel\modules\ticket\assets\ThreadListCheckerAsset;
16
use hipanel\modules\ticket\menus\TicketActionsMenu;
17
use hipanel\modules\ticket\models\Thread;
18
use hipanel\modules\ticket\widgets\ThreadDecorator;
19
use hipanel\modules\ticket\widgets\Topic;
20
use hipanel\widgets\ClientSellerLink;
21
use hipanel\widgets\Gravatar;
22
use hiqdev\yii2\menus\grid\MenuColumn;
23
use Yii;
24
use yii\helpers\Html;
25
use yii\helpers\Json;
26
27
class TicketGridView extends BoxedGridView
28
{
29
    public $enableListChecker = false;
30
31
    public function columns()
32
    {
33
        return array_merge(parent::columns(), [
34
            'subject' => [
35
                'attribute' => 'subject',
36
                'format' => 'raw',
37
                'filterInputOptions' => ['style' => 'width:100%', 'class' => 'form-control'],
38
                'value' => function ($model) {
39
                    $decorator = new ThreadDecorator($model);
40
41
                    $ava = Html::tag('div', Gravatar::widget([
42
                        'emailHash' => $model->author_email,
43
                        'defaultImage' => 'identicon',
44
                        'options' => [
45
                            'alt' => Yii::t('hipanel:ticket', 'Avatar for {login}', ['login' => $model->author]),
46
                            'class' => 'img-circle',
47
                        ],
48
                        'size' => 40,
49
                    ]), ['class' => 'pull-right']);
50
51
                    $titleLink = [
52
                        Html::a($decorator->subject, $model->threadUrl, [
53
                            'class' => 'text-bold',
54
                            'style' => $model->state === Thread::STATE_CLOSE ? 'color: black !important;' : '',
55
                        ]),
56
                        Topic::widget(['topics' => $model->topics]),
57
                        Html::tag(
58
                            'div',
59
                            sprintf('#%s %s %s',
60
                                $model->id,
61
                                Html::tag('span', Yii::t('hipanel:ticket', $model->state_label), ['class' => 'text-bold']),
62
                                Yii::$app->formatter->asDatetime($model->create_time)
63
                            ),
64
                            ['class' => 'text-muted']
65
                        ),
66
                    ];
67
68
                    return $ava . Html::tag('div', implode('', $titleLink));
69
                },
70
            ],
71
            'author_id' => [
72
                'class' => ClientColumn::class,
73
                'label' => Yii::t('hipanel:ticket', 'Author'),
74
                'idAttribute' => 'author_id',
75
                'sortAttribute' => 'author',
76
                'attribute' => 'author_id',
77
                'value' => function ($model) {
78
                    return ClientSellerLink::widget(compact('model'));
79
                },
80
            ],
81
            'responsible_id' => [
82
                'class' => ClientColumn::class,
83
                'idAttribute' => 'responsible_id',
84
                'sortAttribute' => 'responsible',
85
                'attribute' => 'responsible_id',
86
                'clientType' => ['admin', 'seller', 'manager'],
87
                'value' => function ($model) {
88
                    return Html::a($model['responsible'], ['/client/client/view', 'id' => $model->responsible_id]);
89
                },
90
                'visible' => Yii::$app->user->can('support'),
91
            ],
92
            'recipient_id' => [
93
                'class' => ClientColumn::class,
94
                'idAttribute' => 'recipient_id',
95
                'label' => Yii::t('hipanel:ticket', 'Recipient'),
96
                'sortAttribute' => 'recipient',
97
                'attribute' => 'recipient_id',
98
                'value' => function ($model) {
99
                    return Html::a($model->recipient, ['/client/client/view', 'id' => $model->recipient_id]);
100
                },
101
                'visible' => Yii::$app->user->can('support'),
102
            ],
103
            'answer_count' => [
104
                'attribute' => 'answer_count',
105
                'label' => Yii::t('hipanel:ticket', 'Answers'),
106
                'format' => 'raw',
107
                'filter' => false,
108
                'enableSorting' => false,
109
                'value' => function ($model) {
110
                    $lastAnswer = [
111
                        ClientSellerLink::widget([
112
                            'model' => $model,
113
                            'clientAttribute' => 'replier',
114
                            'clientIdAttribute' => 'replier_id',
115
                            'sellerAttribute' => false,
116
                        ]), '<br>',
117
118
                        Html::tag('span', Yii::$app->formatter->asRelativeTime($model->reply_time), [
119
                            'style' => 'font-size: smaller;white-space: nowrap;',
120
                            'class' => 'text-muted',
121
                        ]), '&nbsp;&nbsp;',
122
123
                        Html::tag('span', $model->answer_count, [
124
                            'class' => 'label label-default',
125
                            'title' => Yii::t('hipanel:ticket',
126
                                'Ticket contains {n, plural, one{# answer} other{# answers}}',
127
                                ['n' => $model->answer_count]
128
                            ),
129
                        ]),
130
                    ];
131
132
                    return implode('', $lastAnswer);
133
                },
134
                'contentOptions' => [
135
                    'class' => 'answer',
136
                ],
137
            ],
138
            'actions' => [
139
                'class' => MenuColumn::class,
140
                'menuClass' => TicketActionsMenu::class,
141
            ],
142
        ]);
143
    }
144
145
    public function run()
146
    {
147
        if ($this->enableListChecker) {
148
            ThreadListCheckerAsset::register($this->view);
149
            $options = Json::encode(['pjaxSelector' => '#ticket-grid-pjax']);
150
            $this->view->registerJs("$('#ticket-grid-pjax').closest('form').parent().threadListChecker($options);");
151
        }
152
153
        parent::run();
154
    }
155
}
156