DocumentsColumn   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 97
ccs 0
cts 81
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A generateManagementButtons() 0 11 2
A renderSeeNewButton() 0 10 1
A renderUpdateButton() 0 18 1
A getRouteForSeeNew() 0 7 1
A getRouteForUpdate() 0 4 1
A getDataCellValue() 0 28 1
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid;
12
13
use hipanel\helpers\FontIcon;
14
use hipanel\modules\finance\widgets\DocumentByMonthButton;
15
use hipanel\widgets\ArraySpoiler;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\helpers\Html;
19
20
class DocumentsColumn extends \hipanel\grid\DataColumn
21
{
22
    public $format = 'raw';
23
24
    public $type;
25
26
    public function init()
27
    {
28
        if ($this->type === null) {
29
            throw new InvalidConfigException('Property "type" must be set');
30
        }
31
    }
32
33
    public function getDataCellValue($model, $key, $index)
34
    {
35
        return ArraySpoiler::widget([
36
            'mode' => ArraySpoiler::MODE_SPOILER,
37
            'data' => parent::getDataCellValue($model, $key, $index),
38
            'delimiter' => ' ',
39
            'formatter' => function ($doc) {
40
                return Html::a(
41
                    FontIcon::i('fa-file-pdf-o') . date(' M Y', strtotime($doc->validity_start)),
42
                    ["/file/{$doc->file_id}/{$doc->filename}", 'nocache' => 1],
43
                    [
44
                        'target' => '_blank',
45
                        'class' => 'btn btn-app pull-left',
46
                        'style' => 'width: 8em;',
47
                    ]
48
                );
49
            },
50
            'template' => '<div class="text-right clearfix" style="margin-bottom: 10px;padding-left: 10px;">' . $this->generateManagementButtons($model) . '{button}</div><div>{visible}{hidden}</div>',
51
            'visibleCount' => 3,
52
            'button' => [
53
                'label' => FontIcon::i('fa-history') . ' ' . Yii::t('hipanel', 'Show all'),
54
                'class' => 'btn btn-xs btn-default',
55
                'data' => [
56
                    'toggle' => 'button',
57
                ],
58
            ],
59
        ]);
60
    }
61
62
    protected function generateManagementButtons($model)
63
    {
64
        if (!Yii::$app->user->can('document.generate')) {
65
            return null;
66
        }
67
68
        $buttons[] = $this->renderSeeNewButton($model);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$buttons was never initialized. Although not strictly required by PHP, it is generally a good practice to add $buttons = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
69
        $buttons[] = $this->renderUpdateButton($model);
70
71
        return Html::tag('div', implode('', $buttons), ['class' => 'btn-group', 'style' => 'display: block;']);
72
    }
73
74
    public function renderSeeNewButton($model)
75
    {
76
        return DocumentByMonthButton::widget([
77
            'modalHeader' => Yii::t('hipanel:finance', 'See new'),
78
            'buttonLabel' => Yii::t('hipanel:finance', 'See new'),
79
            'model' => $model,
80
            'action' => $this->getRouteForSeeNew(),
81
            'type' => $this->type,
82
        ]);
83
    }
84
85
    protected function renderUpdateButton($model)
86
    {
87
        return DocumentByMonthButton::widget([
88
            'model' => $model,
89
            'modalHeader' => Yii::t('hipanel:finance', 'Confirm document updating'),
90
            'modalHeaderColor' => 'label-warning',
91
            'buttonLabel' => Yii::t('hipanel:finance', 'Update'),
92
            'action' => $this->getRouteForUpdate(),
93
            'type' => $this->type,
94
            'prepend' => implode('', [
95
                Html::activeHiddenInput($model, 'type', ['value' => $this->type]),
96
                Html::beginTag('blockquote', ['class' => 'text-warning']),
97
                    Html::tag('h5', Yii::t('hipanel:finance', 'Are you sure you want to update document?')),
98
                    Html::tag('h5', Yii::t('hipanel:finance', 'Current document will be substituted with newer version!')),
99
                Html::endTag('blockquote'),
100
            ]),
101
        ]);
102
    }
103
104
    protected function getRouteForSeeNew()
105
    {
106
        return [
107
            '@purse/pre-generate-document',
108
            'type' => $this->type,
109
        ];
110
    }
111
112
    protected function getRouteForUpdate()
113
    {
114
        return ['@purse/generate-and-save-document'];
115
    }
116
}
117