|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Woo\GridView; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
7
|
|
|
use Illuminate\Pagination\Paginator; |
|
8
|
|
|
use Woo\GridView\Columns\AttributeColumn; |
|
9
|
|
|
use Woo\GridView\Columns\BaseColumn; |
|
10
|
|
|
use Woo\GridView\Columns\CallbackColumn; |
|
11
|
|
|
use Woo\GridView\DataProviders\BaseDataProvider; |
|
12
|
|
|
use Woo\GridView\Renderers\DefaultRenderer; |
|
13
|
|
|
use Woo\GridView\Renderers\BaseRenderer; |
|
14
|
|
|
use Woo\GridView\Traits\Configurable; |
|
15
|
|
|
|
|
16
|
|
|
class GridView |
|
17
|
|
|
{ |
|
18
|
|
|
use Configurable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Counter for ids |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $counter = 0; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Grid id (used for request handling, for |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $id; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* DataProvider provides gridview with the data for representation |
|
34
|
|
|
* @var BaseDataProvider |
|
35
|
|
|
*/ |
|
36
|
|
|
public $dataProvider; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Columns config. You may specify array or GridColumn instance |
|
40
|
|
|
* @var BaseColumn[] |
|
41
|
|
|
*/ |
|
42
|
|
|
public $columns = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Common options for all columns, will be appended to all columns configs |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
public $columnOptions = [ |
|
49
|
|
|
'class' => AttributeColumn::class, |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Renders the final UI |
|
54
|
|
|
* @var string|BaseRenderer |
|
55
|
|
|
*/ |
|
56
|
|
|
public $renderer = DefaultRenderer::class; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Allows to pass some options into renderer/customize rendered behavior |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
public $rendererOptions = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Controls amount of data per page |
|
66
|
|
|
* @var int |
|
67
|
|
|
*/ |
|
68
|
|
|
public $rowsPerPage = 25; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Allows to tune the <table> tag with html options |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
public $tableHtmlOptions = [ |
|
75
|
|
|
'class' => 'table table-bordered gridview-table', |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Indicate if filters will be shown or not |
|
80
|
|
|
* @var bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public $showFilters = true; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var Paginator |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $pagination; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var GridViewRequest |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $request; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* GridView constructor. |
|
96
|
|
|
* @param array $config |
|
97
|
|
|
* @throws Exceptions\GridViewConfigException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function __construct(array $config) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->id = self::$counter++; |
|
102
|
|
|
|
|
103
|
|
|
$this->loadConfig($config); |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Making renderer |
|
107
|
|
|
*/ |
|
108
|
|
|
if (!is_object($this->renderer)) { |
|
109
|
|
|
$className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
110
|
|
|
$this->renderer = new $className(array_merge( |
|
111
|
|
|
$this->rendererOptions, [ |
|
112
|
|
|
'gridView' => $this, |
|
113
|
|
|
] |
|
114
|
|
|
)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Build columns from config |
|
119
|
|
|
*/ |
|
120
|
|
|
$this->buildColumns(); |
|
121
|
|
|
|
|
122
|
|
|
$this->request = GridViewRequest::parse($this->id); |
|
123
|
|
|
|
|
124
|
|
|
$this->pagination = new LengthAwarePaginator( |
|
|
|
|
|
|
125
|
|
|
$this->dataProvider->getData( |
|
126
|
|
|
$this->request->filters, |
|
127
|
|
|
$this->request->sortColumn ?? '', |
|
128
|
|
|
$this->request->sortOrder ?? 'DESC', |
|
129
|
|
|
$this->request->page, $this->rowsPerPage |
|
130
|
|
|
), |
|
131
|
|
|
$this->dataProvider->getCount(), |
|
132
|
|
|
$this->rowsPerPage, |
|
133
|
|
|
$this->request->page |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function configTests(): array |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
|
|
'dataProvider' => BaseDataProvider::class, |
|
144
|
|
|
'columns' => 'array', |
|
145
|
|
|
'renderer' => BaseRenderer::class, |
|
146
|
|
|
'rowsPerPage' => 'int', |
|
147
|
|
|
'tableHtmlOptions' => 'array', |
|
148
|
|
|
'showFilters' => 'boolean', |
|
149
|
|
|
]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Build columns into objects |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function buildColumns() |
|
156
|
|
|
{ |
|
157
|
|
|
foreach ($this->columns as $key => &$columnOptions) { |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* In case of when column is already build |
|
161
|
|
|
*/ |
|
162
|
|
|
if (is_object($columnOptions)) { |
|
163
|
|
|
continue; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* When only attribute name/value passed |
|
168
|
|
|
*/ |
|
169
|
|
|
if (is_string($columnOptions)) { |
|
170
|
|
|
$columnOptions = [ |
|
171
|
|
|
'value' => $columnOptions, |
|
172
|
|
|
]; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($columnOptions instanceof Closure) { |
|
176
|
|
|
$columnOptions = [ |
|
177
|
|
|
'class' => CallbackColumn::class, |
|
178
|
|
|
'value' => $columnOptions, |
|
179
|
|
|
'title' => GridViewHelper::columnTitle($key), |
|
180
|
|
|
]; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Inline column declaration detector |
|
185
|
|
|
*/ |
|
186
|
|
|
if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
187
|
|
|
$columnOptions['class'] = 'view'; |
|
188
|
|
|
$columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
192
|
|
|
|
|
193
|
|
|
$className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
194
|
|
|
$columnOptions = new $className($columnOptions); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Draws widget and return html code |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function render() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->renderer->render($this); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function getPagination() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->pagination; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function getRequest() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->request; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function getId() |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->id; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function compileTableHtmlOptions() |
|
223
|
|
|
{ |
|
224
|
|
|
return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
225
|
|
|
} |
|
226
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..