Completed
Pull Request — master (#42)
by
unknown
12:46
created

TicketGridView::columns()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 113

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 0
cts 106
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($decorator->subject, $model->threadUrl, [
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', $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
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL, expecting ']'
Loading history...
69
                'format' => 'raw',
70
=======
71
                'contentOptions' => [
72
                    'style' => 'width: 1%; white-space: nowrap;',
73
                ],
74
>>>>>>> 9fa50bd532db1148300eaafb98a058eb277f89b7
75
                'value' => function ($model) {
76
                    return ClientSellerLink::widget(compact('model'));
77
                },
78
            ],
79
            'responsible_id' => [
80
                'class' => ClientColumn::class,
81
                'idAttribute' => 'responsibles',
82
                'sortAttribute' => 'responsible',
83
                'attribute' => 'responsible',
84
                'clientType' => ['admin', 'reseller', 'manager'],
85
<<<<<<< HEAD
86
                'format' => 'raw',
87
=======
88
                'contentOptions' => [
89
                    'style' => 'width: 1%; white-space: nowrap;',
90
                ],
91
>>>>>>> 9fa50bd532db1148300eaafb98a058eb277f89b7
92
                'value' => function ($model) {
93
                    return Html::a(Html::encode($model['responsible']), ['/client/client/view', 'id' => $model->responsible_id]);
94
                },
95
                'visible' => Yii::$app->user->can('support'),
96
            ],
97
            'recipient_id' => [
98
                'class' => ClientColumn::class,
99
                'idAttribute' => 'recipient_id',
100
                'label' => Yii::t('hipanel:ticket', 'Recipient'),
101
                'sortAttribute' => 'recipient',
102
                'attribute' => 'recipient_id',
103
<<<<<<< HEAD
104
                'format' => 'raw',
105
=======
106
                'contentOptions' => [
107
                    'style' => 'width: 1%; white-space: nowrap;',
108
                ],
109
>>>>>>> 9fa50bd532db1148300eaafb98a058eb277f89b7
110
                'value' => function ($model) {
111
                    return Html::a(Html::encode($model->recipient), ['/client/client/view', 'id' => $model->recipient_id]);
112
                },
113
                'visible' => Yii::$app->user->can('support'),
114
            ],
115
            'answer_count' => [
116
                'attribute' => 'answer_count',
117
                'label' => Yii::t('hipanel:ticket', 'Answers'),
118
                'format' => 'raw',
119
                'filter' => false,
120
                'enableSorting' => false,
121
                'value' => function ($model) {
122
                    $lastAnswer = [
123
                        ClientSellerLink::widget([
124
                            'model' => $model,
125
                            'clientAttribute' => 'replier',
126
                            'clientIdAttribute' => 'replier_id',
127
                            'sellerAttribute' => false,
128
                        ]), '<br>',
129
130
                        Html::tag('span', Yii::$app->formatter->asRelativeTime($model->reply_time), [
131
                            'style' => 'font-size: smaller;white-space: nowrap;',
132
                            'class' => 'text-muted',
133
                        ]), '&nbsp;&nbsp;',
134
135
                        Html::tag('span', $model->answer_count, [
136
                            'class' => 'label label-default',
137
                            'title' => Yii::t('hipanel:ticket',
138
                                'Ticket contains {n, plural, one{# answer} other{# answers}}',
139
                                ['n' => $model->answer_count]
140
                            ),
141
                        ]),
142
                    ];
143
144
                    return implode('', $lastAnswer);
145
                },
146
                'contentOptions' => [
147
                    'class' => 'answer',
148
                    'style' => 'width: 1%; white-space: nowrap;',
149
                ],
150
            ],
151
            'actions' => [
152
                'class' => MenuColumn::class,
153
                'menuClass' => TicketActionsMenu::class,
154
            ],
155
        ]);
156
    }
157
158
    public function run()
159
    {
160
        if ($this->enableListChecker) {
161
            ThreadListCheckerAsset::register($this->view);
162
            $options = Json::encode(['pjaxSelector' => '#ticket-grid-pjax']);
163
            $this->view->registerJs("$('#ticket-grid-pjax').closest('form').parent().threadListChecker($options);");
164
        }
165
166
        parent::run();
167
    }
168
}
169