-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.php
361 lines (297 loc) · 8.72 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
namespace Pionect\Backoffice\ContentElements\Tables;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Pionect\Backoffice\ContentElements\ContentContract;
use Pionect\Backoffice\Models\BaseRepository;
abstract class Table extends Fillable implements ContentContract, TableBuilderContract
{
/**
* @var string
*/
public $primaryKey = 'id';
/**
* @var array
*/
protected $fillable = [
'id', 'columns', 'filters','name', 'type', 'searchQuery', 'from', 'size', 'currentPage', 'rows',
'path', 'visibleColumns', 'selectedFilters', 'filtersVisible', 'paginator', 'total',
'sortTargetOrder', 'sortColumnName',
];
/**
* @var array
*/
protected $attributes = [
'total' => 0, 'size' => 25, 'currentPage' => 1
];
/**
* @var array
*/
protected $searchFields;
/**
* @var string
*/
public $baseColumnClass;
/**
* @var string
*/
public $baseFilterClass;
/**
* @var BaseRepository
*/
protected $repository;
/**
* @var Collection
*/
protected $collection;
/**
* @var TablePresenter
*/
protected $tablePresenter;
/**
* @var LengthAwarePaginator
*/
protected $paginator;
/**
* @var QueryCache
*/
protected $queryCache;
public function __construct(QueryCache $queryCache)
{
$this->queryCache = $queryCache;
if($this->tablePresenter !== null) {
$this->tablePresenter->setQueryCache($this->queryCache);
}
}
/**
* @param Request $request
* @param $tableId
* @param bool $loadingContent
* @return $this|mixed
*/
public function build(Request $request, $tableId, $loadingContent = false)
{
$jsonAllVisibleColumns = json_decode($request->cookie('visible_columns'), true);
$visibleColumns = is_array($jsonAllVisibleColumns) && array_key_exists($tableId, $jsonAllVisibleColumns)
? $jsonAllVisibleColumns[$tableId] : null;
$jsonAllSelectedFilters = json_decode($request->cookie('selected_filters'), true);
$selectedFilters = is_array($jsonAllSelectedFilters) && array_key_exists($tableId, $jsonAllSelectedFilters)
? $jsonAllSelectedFilters[$tableId] : null;
$config = [
'id' => $tableId,
'type' => $tableId,
'path' => $tableId,
'sortColumnName' => request()->get('sortColumnName'),
'sortTargetOrder' => request()->get('sortTargetOrder'),
'searchQuery' => request()->get('search'),
'currentPage' => request()->get('page'),
'total' => request()->get('total'),
'visibleColumns' => $visibleColumns,
'selectedFilters' => $selectedFilters
];
$this->fill($config);
$this->buildColumns($config);
if(!$loadingContent){
$this->buildFilters($this->getFilterConfig(), $this->searchForAggregates());
}
return $this;
}
/**
* @return string
* @throws \Throwable
*/
public function render()
{
return view('backoffice::table.table', $this->attributes)->render();
}
/**
* @return string
* @throws \Throwable
*/
public function renderContent()
{
$this->collection = $this->getCollection();
$this->buildRows();
$this->attributes['paginator'] = $this->paginator();
return view('backoffice::table.table-content', $this->attributes)->render();
}
/**
* @return array
*/
public function getExportData()
{
$exportData = [];
$this->collection = $this->getCollection(false);
foreach ($this->collection as $entity) {
$exportDataElement = [];
foreach ($this->columns as $column) {
/**
* @var Column $column
*/
if($column->visible){
$exportDataElement[$column->description] = $column->getExportValue($entity);
}
}
$exportData[] = $exportDataElement;
}
return $exportData;
}
/**
* @return Collection
*/
protected function buildColumns($config)
{
$this->columns = collect();
$sortQuery = [
'field' => $config['sortColumnName'],
'order' => $config['sortTargetOrder'],
];
$queryParams = [
'sort' => $sortQuery,
'searchQuery' => $config['searchQuery']
];
foreach($this->getColumnConfig() as $columnConfig){
$columnConfig['visible'] = $this->columnVisible($columnConfig, $config);
$this->columns->push($this->makeColumn($columnConfig, $queryParams));
}
return $this->columns;
}
/**
* @param $config
* @return mixed
*/
protected function makeColumn($config, $queryParams)
{
if(array_key_exists('class', $config)){
$column = new $config['class']($config, $queryParams);
} else {
$column = new $this->baseColumnClass($config, $queryParams);
}
return $column;
}
/**
* Determining the visibility of the column:
*
* First check if the tables visible columns are configured (which is determined by the cookie).
* Otherwise the 'default' config in the Table is checked.
* Finally if none is defined the visibility reverts to false.
*
* @param $columnConfig
* @param $tableConfig
* @return bool
*/
protected function columnVisible($columnConfig, $tableConfig)
{
if(array_key_exists('visibleColumns', $tableConfig) && $tableConfig['visibleColumns'] !== null) {
$response = array_key_exists('name', $columnConfig)
&& in_array($columnConfig['name'], $tableConfig['visibleColumns']);
} else {
$response = array_key_exists('default', $columnConfig) ? $columnConfig['default']
: false;
}
return $response;
}
/**
* @param array $filterConfig
* @param array $aggregates
* @return Collection
*/
protected function buildFilters(array $filterConfig, array $aggregates)
{
$this->filters = collect();
foreach($filterConfig as $config)
{
if(array_key_exists('class', $config)){
$filter = new $config['class']($config, $aggregates, $this->selectedFilters, $this->queryCache);
} else {
$filter = new $this->baseFilterClass($config, $aggregates, $this->selectedFilters, $this->queryCache);
}
$this->filters->push($filter);
}
return $this->filters;
}
/**
* @description Build the Table.
*/
protected function buildRows()
{
$this->rows = collect();
foreach ($this->collection as $item) {
$row = new Row($item, $this->columns->toArray(), $this->buildPath(), $this->tablePresenter, $this->getRowStylingClass($item));
$this->rows->push($row);
}
}
/**
* @param null $item
* @return string
*/
public function getRowStylingClass($item = null)
{
return '';
}
/**
* @description Validates sorting
*/
protected function sorted()
{
if (is_array($this->sort)
&& array_key_exists('field', $this->sort)
&& array_key_exists('order', $this->sort)) {
return true;
}
return false;
}
/**
* @return LengthAwarePaginator
*/
protected function paginator()
{
$total = $this->collection ? $this->collection->amountOfHits : 0;
return new LengthAwarePaginator(
$this->rows,
$total,
$this->size,
$this->currentPage
);
}
/**
* @return float|int|null
*/
public function pageFirstItem()
{
return $this->total > 0 ? ($this->currentPage - 1) * $this->size + 1 : null;
}
/**
* @return string
*/
protected function buildPath()
{
return '/' . $this->path;
}
public function authorize(): bool
{
return true;
}
public function __toString()
{
return $this->render();
}
/**
* @param bool $paginated
* @return mixed
*/
abstract protected function getCollection($paginated = true);
/**
* @return array
*/
abstract protected function getColumnConfig();
/**
* @return array
*/
abstract protected function getFilterConfig();
/**
* @return array
*/
abstract protected function searchForAggregates();
}