TicketGridView::columns()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 116

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 116
ccs 0
cts 109
cp 0
rs 8
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 6

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