Completed
Push — master ( bdc854...308562 )
by Dmitry
14:33
created

DocumentsColumn::generateManagementButtons()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid;
12
13
use hipanel\widgets\ArraySpoiler;
14
use hipanel\helpers\FontIcon;
15
use hipanel\widgets\ModalButton;
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
    protected function getHeaderCellLabel()
34
    {
35
        $label = parent::getHeaderCellLabel();
36
37
        $models = $this->grid->dataProvider->getModels();
38
        if (count($models) === 1) {
39
            $this->encodeLabel = false;
40
            $label .= '<br />' . $this->generateManagementButtons($models[0]);
41
        }
42
43
        return $label;
44
    }
45
46
    public function getDataCellValue($model, $key, $index)
47
    {
48
        return ArraySpoiler::widget([
49
            'mode' => ArraySpoiler::MODE_SPOILER,
50
            'data' => parent::getDataCellValue($model, $key, $index),
51
            'delimiter' => ' ',
52
            'formatter' => function ($doc) {
53
                return Html::a(
54
                    FontIcon::i('fa-file-pdf-o fa-2x') . date(' M Y', strtotime($doc->validity_start)),
55
                    ["/file/{$doc->file_id}/{$doc->filename}", 'nocache' => 1],
56
                    [
57
                        'target' => '_blank',
58
                        'class' => 'text-info text-nowrap col-xs-6 col-sm-6 col-md-6 col-lg-3',
59
                        'style' => 'width: 8em;'
60
                    ]
61
                );
62
            },
63
            'template' => '{button}{visible}{hidden}',
64
            'visibleCount' => 2,
65
            'button' => [
66
                'label' => FontIcon::i('fa-history fa-2x') . ' ' . Yii::t('hipanel', 'History'),
67
                'class' => 'pull-right text-nowrap',
68
            ],
69
        ]);
70
    }
71
72
    protected function generateManagementButtons($model)
73
    {
74
        if (!Yii::$app->user->can('manage')) {
75
            return null;
76
        }
77
78
        $buttons[] = $this->renderSeeNewLink($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...
79
        $buttons[] = $this->renderUpdateButton($model);
80
81
        return Html::tag('div', implode('', $buttons), ['class' => 'btn-group']);
82
    }
83
84
    protected function renderSeeNewLink($model)
85
    {
86
        return Html::a(
87
            Yii::t('hipanel:finance', 'See new'),
88
            $this->getSeeNewRoute($model),
89
            ['class' => 'btn btn-default btn-xs', 'target' => 'new-invoice']
90
        );
91
    }
92
93
    protected function renderUpdateButton($model)
94
    {
95
        return ModalButton::widget([
96
            'id' => "modal-{$model->id}-{$this->type}",
97
            'model' => $model,
98
            'form' => [
99
                'action' => $this->getUpdateButtonRoute($model),
100
            ],
101
            'button' => [
102
                'label' => Yii::t('hipanel:finance', 'Update'),
103
                'class' => 'btn btn-default btn-xs',
104
            ],
105
            'body' => implode('', [
106
                Html::activeHiddenInput($model, 'type', ['value' => $this->type]),
107
                Yii::t('hipanel:finance', 'Are you sure you want to update document?') . '<br>',
108
                Yii::t('hipanel:finance', 'Current document will be substituted with newer version!'),
109
            ]),
110
            'modal' => [
111
                'header' => Html::tag('h4', Yii::t('hipanel:finance', 'Confirm document updating')),
112
                'headerOptions' => ['class' => 'label-warning'],
113
                'footer' => [
114
                    'label' => Yii::t('hipanel', 'Update'),
115
                    'class' => 'btn btn-warning',
116
                    'data-loading-text' => Yii::t('hipanel', 'Updating...'),
117
                ],
118
            ],
119
        ]);
120
    }
121
122
    protected function getSeeNewRoute($model)
123
    {
124
        return ['@purse/generate-document', 'id' => $model->id, 'type' => $this->type];
125
    }
126
127
    protected function getUpdateButtonRoute($model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        return ['@purse/generate-and-save-document'];
130
    }
131
}
132