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); |
|
|
|
|
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) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
return ['@purse/generate-and-save-document']; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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:
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 thebar
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.