1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: execut |
4
|
|
|
* Date: 21.07.16 |
5
|
|
|
* Time: 13:32 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace execut\actions\action\adapter\viewRenderer; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use yii\db\ActiveRecord; |
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
use yii\helpers\Html; |
14
|
|
|
use kartik\export\ExportMenu; |
15
|
|
|
use yii\bootstrap\Alert; |
16
|
|
|
use yii\data\BaseDataProvider; |
17
|
|
|
use yii\helpers\Url; |
18
|
|
|
use yii\web\JsExpression; |
19
|
|
|
|
20
|
|
|
class DynaGrid extends Widget |
21
|
|
|
{ |
22
|
|
|
public $title = null; |
23
|
|
|
public $modelClass = null; |
24
|
|
|
/** |
25
|
|
|
* @var BaseDataProvider |
26
|
|
|
*/ |
27
|
|
|
public $dataProvider = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ActiveRecord |
31
|
|
|
*/ |
32
|
|
|
public $filter = null; |
33
|
|
|
public $uniqueId = null; |
34
|
|
|
public $urlAttributes = null; |
35
|
|
|
public $isAllowedAdding = true; |
36
|
|
|
public $isAllowedMassEdit = false; |
37
|
|
|
public $refreshAttributes = []; |
38
|
|
|
public $handleButtons = []; |
39
|
|
|
public $isRenderFlashes = true; |
40
|
|
|
public $urlAttributesExcluded = []; |
41
|
|
|
public $defaultHandleButtons = [ |
42
|
|
|
'visible' => [ |
43
|
|
|
'icon' => 'eye-open', |
44
|
|
|
'label' => 'Mark visible', |
45
|
|
|
'confirmMessage' => 'You sure want mark # records as visible?', |
46
|
|
|
'enable' => false, |
47
|
|
|
], |
48
|
|
|
'unvisible' => [ |
49
|
|
|
'icon' => 'eye-close', |
50
|
|
|
'label' => 'Mark unvisible', |
51
|
|
|
'confirmMessage' => 'You sure want mark # records as unvisible?', |
52
|
|
|
'enable' => false, |
53
|
|
|
], |
54
|
|
|
'delete' => [ |
55
|
|
|
'icon' => 'trash', |
56
|
|
|
'label' => 'Delete', |
57
|
|
|
'button' => 'danger', |
58
|
|
|
'confirmMessage' => 'You sure want delete # records?', |
59
|
|
|
'enable' => false, |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
public function getDefaultWidgetOptions() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'class' => \execut\actions\widgets\DynaGrid::class, |
66
|
|
|
'filter' => $this->filter, |
67
|
|
|
'dataProvider' => $this->dataProvider, |
68
|
|
|
'gridOptions' => [ |
69
|
|
|
'updateUrl' => $this->getUpdateUrlParams(), |
70
|
|
|
'layout' => '{alertBlock}<div class="dyna-grid-footer">{summary}{pager}<div class="dyna-grid-toolbar">{toolbar}</div></div>{items}', |
71
|
|
|
'toolbar' => $this->getToolbarConfig(), |
72
|
|
|
// 'panel' => [ |
|
|
|
|
73
|
|
|
// 'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-cog"></i> ' . \yii::t('execut.actions', 'List') . ' ' . $this->lcfirst($this->title) . '</h3>', |
74
|
|
|
// 'before' => $alertBlock, |
75
|
|
|
// ], |
76
|
|
|
'options' => [ |
77
|
|
|
'id' => $this->getGridId(), |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
'options' => [ |
81
|
|
|
'id' => $this->getDynaGridId(), |
82
|
|
|
], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getWidgetOptions() |
87
|
|
|
{ |
88
|
|
|
$options = parent::getWidgetOptions(); |
89
|
|
|
if (!empty($options['gridOptions']['layout'])) { |
90
|
|
|
$options['gridOptions']['layout'] = strtr($options['gridOptions']['layout'], [ |
91
|
|
|
'{alertBlock}' => $this->renderAlertBlock(), |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $options; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getDynaGridId() { |
99
|
|
|
$m = $this->modelClass; |
100
|
|
|
$tableName = str_replace('\\', '_', $m) . '1'; |
101
|
|
|
$userId = ''; |
102
|
|
|
if (\yii::$app->user) { |
103
|
|
|
$userId .= \yii::$app->user->id; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return 'dynagrid-' . $tableName . '-' . $userId; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getGridId() { |
110
|
|
|
return 'grid-' . $this->getDynaGridId(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
protected function renderAlertBlock() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
if (!$this->isRenderFlashes) { |
116
|
|
|
return ''; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$session = \Yii::$app->session; |
120
|
|
|
$flashes = $session->getAllFlashes(); |
121
|
|
|
$alertContainerOptions = [ |
122
|
|
|
'style' => 'max-width:400px' |
123
|
|
|
]; |
124
|
|
|
if (count($flashes) === 0) { |
125
|
|
|
Html::addCssStyle($alertContainerOptions, 'display:none;'); |
126
|
|
|
} |
127
|
|
|
$out = Html::beginTag('div', $alertContainerOptions); |
128
|
|
|
foreach ($flashes as $type => $message) { |
129
|
|
|
if (is_array($message)) { |
130
|
|
|
$message = implode('<br>', $message); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$alertWidgetOptions = []; |
134
|
|
|
$alertWidgetOptions['body'] = $message; |
135
|
|
|
$alertWidgetOptions['options'] = [ |
136
|
|
|
'class' => ['alert', 'alert-success'], |
137
|
|
|
'style' => 'padding-left:10px;padding-right:10px;' |
138
|
|
|
]; |
139
|
|
|
$out .= "\n" . Alert::widget($alertWidgetOptions); |
140
|
|
|
$session->removeFlash($type); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$out .= "\n</div>"; |
144
|
|
|
|
145
|
|
|
return $out; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $refreshUrlParams |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function getToolbarConfig(): array |
153
|
|
|
{ |
154
|
|
|
$refreshUrlParams = [ |
155
|
|
|
$this->adapter->uniqueId, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
foreach ($this->refreshAttributes as $key) { |
159
|
|
|
if (!empty($this->adapter->actionParams->get[$key])) { |
160
|
|
|
$refreshUrlParams[$key] = $this->adapter->actionParams->get[$key]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return [ |
165
|
|
|
'massEdit' => ['content' => $this->renderMassEditButton()], |
166
|
|
|
'massVisible' => ['content' => $this->renderVisibleButtons()], |
167
|
|
|
'add' => ['content' => $this->renderAddButton()], |
168
|
|
|
'refresh' => [ |
169
|
|
|
'content' => Html::a('<i class="glyphicon glyphicon-repeat"></i>', $refreshUrlParams, ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid']), |
170
|
|
|
], |
171
|
|
|
'dynaParams' => ['content' => '{dynagridFilter}{dynagridSort}{dynagrid}'], |
172
|
|
|
'toggleData' => '{toggleData}', |
173
|
|
|
'export' => '{export}', |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function lcfirst($string, $encoding = "UTF-8") |
178
|
|
|
{ |
179
|
|
|
$first = mb_convert_case(mb_substr($string, 0, 1, $encoding), MB_CASE_LOWER, $encoding); |
180
|
|
|
|
181
|
|
|
return $first . mb_substr($string, 1, null, $encoding); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getUniqueId() { |
185
|
|
|
if ($this->uniqueId) { |
186
|
|
|
return $this->uniqueId; |
187
|
|
|
} else { |
188
|
|
|
return $this->adapter->actionParams->getUniqueId(['module', 'controller']); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function getUrlAttributes() { |
193
|
|
|
|
194
|
|
|
if ($this->urlAttributes === null) { |
195
|
|
|
$filterAttributes = $this->filter->attributes; |
196
|
|
|
foreach ($this->filter->getRelatedRecords() as $relation => $records) { |
197
|
|
|
if (empty($records)) { |
198
|
|
|
continue; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (!is_array($records)) { |
202
|
|
|
$filterAttributes[$relation] = $records; |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$relationAttributes = []; |
207
|
|
|
foreach ($records as $key => $record) { |
208
|
|
|
$recordAttributes = array_filter($record->attributes); |
209
|
|
|
if (!empty($recordAttributes)) { |
210
|
|
|
$relationAttributes[$key] = $recordAttributes; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (!empty($relationAttributes)) { |
215
|
|
|
$filterAttributes[$relation] = $relationAttributes; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$formName = $this->filter->formName(); |
220
|
|
|
$result = [$formName => []]; |
221
|
|
|
foreach ($filterAttributes as $attribute => $value) { |
222
|
|
|
if (in_array($attribute, $this->urlAttributesExcluded)) { |
223
|
|
|
continue; |
224
|
|
|
} |
225
|
|
|
if (!empty($value)) { |
226
|
|
|
$result[$formName][$attribute] = $value; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->urlAttributes; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
protected function renderAddButton() |
240
|
|
|
{ |
241
|
|
|
if ($this->isAllowedAdding) { |
242
|
|
|
$lcfirstTitle = $this->title; |
243
|
|
|
// var_dump($lcfirstTitle); |
|
|
|
|
244
|
|
|
// exit; |
245
|
|
|
return Html::a(\yii::t('execut.actions', 'Add') . ' ' . $lcfirstTitle, Url::to($this->getUpdateUrlParams()), [ |
246
|
|
|
'type' => 'button', |
247
|
|
|
'data-pjax' => 0, |
248
|
|
|
'title' => \yii::t('execut.actions', 'Add') . ' ' . $lcfirstTitle, |
249
|
|
|
'class' => 'btn btn-success' |
250
|
|
|
]) . ' '; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
protected function renderMassEditButton() |
258
|
|
|
{ |
259
|
|
|
if ($this->isAllowedMassEdit) { |
260
|
|
|
$lcfirstTitle = $this->title; |
|
|
|
|
261
|
|
|
|
262
|
|
|
return \mickgeek\actionbar\Widget::widget([ |
263
|
|
|
'renderContainer' => false, |
264
|
|
|
'grid' => $this->getGridId(), |
265
|
|
|
'templates' => [ |
266
|
|
|
'{bulk-actions}' => [ |
267
|
|
|
// 'class' => 'col-xs-4' |
|
|
|
|
268
|
|
|
], |
269
|
|
|
// '{create}' => ['class' => 'col-xs-8 text-right'], |
|
|
|
|
270
|
|
|
], |
271
|
|
|
'bulkActionsItems' => [ |
272
|
|
|
'General' => ['mass-update' => 'Mass edit',], |
273
|
|
|
], |
274
|
|
|
'bulkActionsOptions' => [ |
275
|
|
|
'options' => [ |
276
|
|
|
'mass-update' => [ |
277
|
|
|
'url' => Url::toRoute(['mass-update']), |
278
|
|
|
'method' => 'get', |
279
|
|
|
'name' => 'id', |
280
|
|
|
], |
281
|
|
|
], |
282
|
|
|
'class' => 'form-control', |
283
|
|
|
], |
284
|
|
|
]); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
protected function renderVisibleButtons(): string |
292
|
|
|
{ |
293
|
|
|
$buttons = ''; |
294
|
|
|
$handleButtons = ArrayHelper::merge($this->defaultHandleButtons, $this->handleButtons); |
295
|
|
|
foreach ($handleButtons as $handle => $buttonOptions) { |
296
|
|
|
if (isset($buttonOptions['enable']) && $buttonOptions['enable'] === false) { |
297
|
|
|
continue; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$urlParams = \yii::$app->request->getQueryParams(); |
301
|
|
|
$urlParams[0] = ''; |
302
|
|
|
$urlParams['handle'] = $handle; |
303
|
|
|
$buttonClass = 'default'; |
304
|
|
|
if (!empty($buttonOptions['button'])) { |
305
|
|
|
$buttonClass = $buttonOptions['button']; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$icon = $buttonOptions['icon']; |
309
|
|
|
$confirmMessage = strtr($buttonOptions['confirmMessage'], ['#' => (string) $this->dataProvider->getTotalCount()]); |
310
|
|
|
$buttons .= Html::a('<i class="glyphicon glyphicon-' . $icon . '"></i>', Url::to($urlParams), [ |
311
|
|
|
'type' => 'button', |
312
|
|
|
'onclick' => new JsExpression(<<<JS |
313
|
|
|
return confirm('$confirmMessage'); |
314
|
|
|
JS |
315
|
|
|
), |
316
|
|
|
'data-pjax' => 0, |
317
|
|
|
'title' => $buttonOptions['label'], |
318
|
|
|
'class' => 'btn btn-' . $buttonClass |
319
|
|
|
]); |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $buttons; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
|
|
protected function getUpdateUrlParams(): array |
330
|
|
|
{ |
331
|
|
|
return array_merge([ |
332
|
|
|
'/' . trim($this->getUniqueId(), '/') . '/update', |
333
|
|
|
], $this->getUrlAttributes()); |
334
|
|
|
} |
335
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.