1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: execut |
4
|
|
|
* Date: 07.07.15 |
5
|
|
|
* Time: 11:26 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace execut\actions\action\adapter; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use execut\actions\action\Adapter; |
12
|
|
|
use execut\actions\action\adapter\viewRenderer\DynaGrid; |
13
|
|
|
use execut\yii\helpers\Html; |
14
|
|
|
use yii\base\Model; |
15
|
|
|
use yii\data\ArrayDataProvider; |
16
|
|
|
use yii\db\ActiveRecord; |
17
|
|
|
use yii\web\Response; |
|
|
|
|
18
|
|
|
use yii\widgets\ActiveForm; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class GridView |
22
|
|
|
* @package execut\actions\action |
23
|
|
|
* @property Model $filter |
24
|
|
|
*/ |
25
|
|
|
class GridView extends \execut\actions\action\adapter\Form |
26
|
|
|
{ |
27
|
|
|
public $attributes = null; |
28
|
|
|
|
29
|
|
|
protected $_isValidate = false; |
30
|
|
|
|
31
|
|
|
public $scenario = ActiveRecord::SCENARIO_DEFAULT; |
32
|
|
|
|
33
|
|
|
protected $handlers = []; |
34
|
|
|
|
35
|
|
|
protected function getAttributes() { |
36
|
|
|
$attributes = $this->attributes; |
37
|
|
|
if ($attributes === null) { |
38
|
|
|
$class = $this->model->className(); |
39
|
|
|
$attributes = [ |
40
|
|
|
'id' => current($class::primaryKey()), |
41
|
|
|
'text' => 'name', |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $attributes; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setHandlers($handlers) { |
49
|
|
|
foreach ($handlers as $key => $handler) { |
50
|
|
|
if (is_array($handler)) { |
51
|
|
|
$handlers[$key] = \yii::createObject($handler); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->handlers = $handlers; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getHandlers() { |
59
|
|
|
return $this->handlers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function _run() { |
63
|
|
|
/** |
64
|
|
|
* @var ActiveRecord $filter |
65
|
|
|
*/ |
66
|
|
|
$filter = $this->model; |
67
|
|
|
// if ($filter->getBehavior('relationsSaver')) { |
|
|
|
|
68
|
|
|
// $filter->detachBehavior('relationsSaver'); |
69
|
|
|
// } |
70
|
|
|
|
71
|
|
|
if ($this->scenario !== ActiveRecord::SCENARIO_DEFAULT) { |
72
|
|
|
$filter->scenario = $this->scenario; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
parent::_run(); |
76
|
|
|
|
77
|
|
|
$isValid = $filter->validate(); |
78
|
|
|
/** |
79
|
|
|
* @var ArrayDataProvider $dataProvider |
80
|
|
|
*/ |
81
|
|
|
$dataProvider = $filter->search(); |
82
|
|
|
if (!$isValid) { |
83
|
|
|
$dataProvider->query->andWhere('false'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!empty($this->actionParams->get['handle'])) { |
87
|
|
|
$handlerKey = $this->actionParams->get['handle']; |
88
|
|
|
$handlers = $this->handlers; |
89
|
|
|
if (!empty($handlers[$handlerKey])) { |
90
|
|
|
$handler = $handlers[$handlerKey]; |
91
|
|
|
$handler->dataProvider = $dataProvider; |
92
|
|
|
if (($result = $handler->run()) !== null) { |
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$actionParams = $this->actionParams; |
99
|
|
|
$response = $this->getResponse(); |
100
|
|
|
if ($actionParams->isAjax && !$actionParams->isPjax && !$this->isDisableAjax && $dataProvider) { |
101
|
|
|
if (isset($this->actionParams->get['depdrop_parents'])) { |
102
|
|
|
$key = 'output'; |
103
|
|
|
$dataProvider->pagination->pageSize = 500; |
104
|
|
|
} else { |
105
|
|
|
$key = 'results'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$result = []; |
109
|
|
|
foreach ($dataProvider->models as $row) { |
110
|
|
|
$attributes = $this->getAttributes(); |
111
|
|
|
$modelAttributes = array_values($attributes); |
112
|
|
|
if ($row instanceof Model) { |
|
|
|
|
113
|
|
|
$row = $row->getAttributes($modelAttributes); |
114
|
|
|
} |
115
|
|
|
$res = []; |
116
|
|
|
foreach ($attributes as $targetKey => $attribute) { |
117
|
|
|
if (is_int($targetKey)) { |
118
|
|
|
$targetKey = $attribute; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$res[$targetKey] = $row[$attribute]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$result[] = $res; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$pagination = $dataProvider->pagination; |
128
|
|
|
$response->content = [ |
129
|
|
|
$key => $result, |
130
|
|
|
'pagination' => [ |
131
|
|
|
'more' => $pagination ? ($pagination->page < $pagination->pageCount - 1) : false, |
132
|
|
|
], |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$response->format = Response::FORMAT_JSON; |
136
|
|
|
} else { |
137
|
|
|
$response->content = [ |
138
|
|
|
'filter' => $filter, |
139
|
|
|
'dataProvider' => $dataProvider, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $response; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getDefaultViewRendererConfig() |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
'class' => DynaGrid::className(), |
150
|
|
|
// 'title' => $this->model->getModelLabelOld(2), |
|
|
|
|
151
|
|
|
'modelClass' => $this->model->className(), |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: