1 | <?php |
||
20 | class DataTable |
||
21 | { |
||
22 | |||
23 | /** @var Builder */ |
||
24 | private $_queryBuilder; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $_headerFormatters = []; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $_components = []; |
||
31 | |||
32 | /** @var string */ |
||
33 | private $_classes; |
||
34 | |||
35 | /** @var array */ |
||
36 | private $_columns = []; |
||
37 | |||
38 | /** @var Model */ |
||
39 | private $_model; |
||
40 | |||
41 | /** @var array */ |
||
42 | private $_relations = []; |
||
43 | |||
44 | /** @var string */ |
||
45 | private $_noDataHtml = '<div>no data</div>'; |
||
46 | |||
47 | /** |
||
48 | * Set the base model whose data is displayed in the table. |
||
49 | * |
||
50 | * @param string $modelName |
||
51 | * @param array $columns |
||
52 | * @return $this |
||
53 | * @throws \RuntimeException |
||
54 | */ |
||
55 | 59 | public function model(string $modelName, array $columns = []): DataTable |
|
56 | { |
||
57 | 59 | if (!\is_subclass_of($modelName, Model::class)) { |
|
58 | 2 | throw new RuntimeException('Class "' . $modelName . '" is not an active record!'); |
|
59 | } |
||
60 | |||
61 | 57 | $this->_model = new $modelName; |
|
62 | 57 | $this->_queryBuilder = $this->_model->newQuery(); |
|
63 | 57 | $this->_columns = $this->_fetchColumns($columns); |
|
64 | |||
65 | 57 | return $this; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Returns an array of Column objects which may be bound to a formatter. |
||
70 | * |
||
71 | * @param array $columns |
||
72 | * @return array |
||
73 | */ |
||
74 | 58 | private function _fetchColumns(array $columns): array |
|
75 | { |
||
76 | 58 | $columnModels = []; |
|
77 | 58 | foreach ($columns as $column => $formatter) { |
|
78 | 47 | [$column, $formatter] = $this->_setColumnFormatter($column, $formatter); |
|
79 | 47 | $columnModels[] = new Column($column, $formatter, $this->_model); |
|
80 | } |
||
81 | |||
82 | 58 | return $columnModels; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $column |
||
87 | * @param $formatter |
||
88 | * @return array |
||
89 | */ |
||
90 | 47 | private function _setColumnFormatter($column, $formatter): array |
|
91 | { |
||
92 | 47 | if (\is_int($column)) { |
|
93 | 45 | $column = $formatter; |
|
94 | 45 | $formatter = null; |
|
95 | } |
||
96 | |||
97 | 47 | return [$column, $formatter]; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | 5 | public function getColumns(): array |
|
104 | { |
||
105 | 5 | return $this->_columns; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return Builder |
||
110 | */ |
||
111 | 38 | public function query(): Builder |
|
115 | |||
116 | /** |
||
117 | * Displayed columns |
||
118 | * |
||
119 | * @param array $columns |
||
120 | * @return $this |
||
121 | */ |
||
122 | 3 | public function columns(array $columns): DataTable |
|
128 | |||
129 | /** |
||
130 | * Add a component to the data table. |
||
131 | * For example a "Paginator" or a "Sorter". |
||
132 | * |
||
133 | * @param DataComponent $component |
||
134 | * |
||
135 | * @param string|null $name |
||
136 | * @return $this|DataTable |
||
137 | */ |
||
138 | 32 | public function addComponent(DataComponent $component, ?string $name = null): DataTable |
|
146 | |||
147 | /** |
||
148 | * Check whether a component exists for the given data table. |
||
149 | * @param string $componentName |
||
150 | * @return bool |
||
151 | */ |
||
152 | 32 | public function componentExists(string $componentName): bool |
|
156 | |||
157 | /** |
||
158 | * Add a formatter for the column headers. |
||
159 | * |
||
160 | * @param HeaderFormatter $headerFormatter |
||
161 | * @return DataTable |
||
162 | */ |
||
163 | 12 | public function formatHeaders(HeaderFormatter $headerFormatter): DataTable |
|
169 | |||
170 | /** |
||
171 | * Add a formatter for a column. |
||
172 | * |
||
173 | * @param string $columnName |
||
174 | * @param ColumnFormatter $columnFormatter |
||
175 | * @return DataTable |
||
176 | */ |
||
177 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter): DataTable |
|
194 | |||
195 | /** |
||
196 | * Add classes to the table. |
||
197 | * |
||
198 | * @param string $classes |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | 1 | public function classes(string $classes): DataTable |
|
208 | |||
209 | /** |
||
210 | * Add a relation to the table. |
||
211 | * |
||
212 | * @param array $relations |
||
213 | * @return DataTable |
||
214 | */ |
||
215 | 4 | public function with(array $relations): DataTable |
|
221 | |||
222 | /** |
||
223 | * Set the HTML which should be displayed when the dataset is empty. |
||
224 | * |
||
225 | * @param string $html |
||
226 | * @return DataTable |
||
227 | */ |
||
228 | 1 | public function noDataHtml(string $html): DataTable |
|
234 | |||
235 | /** |
||
236 | * Set a view which should be displayed when the dataset is empty. |
||
237 | * |
||
238 | * @param string $viewName |
||
239 | * @return DataTable |
||
240 | * @throws \Throwable |
||
241 | */ |
||
242 | 1 | public function noDataView(string $viewName): DataTable |
|
248 | |||
249 | /** |
||
250 | * Renders the table. |
||
251 | * |
||
252 | * @return string |
||
253 | * @throws \RuntimeException |
||
254 | */ |
||
255 | 40 | public function render(): string |
|
270 | |||
271 | /** |
||
272 | * Get data which should be displayed in the table. |
||
273 | * |
||
274 | * @return Collection |
||
275 | * |
||
276 | * @throws \RuntimeException |
||
277 | */ |
||
278 | 40 | private function _getData(): Collection |
|
293 | |||
294 | 39 | private function _addRelations(): void |
|
306 | |||
307 | /** |
||
308 | * @param string $relation |
||
309 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relationship |
||
310 | */ |
||
311 | 4 | private function _addJoin(string $relation, \Illuminate\Database\Eloquent\Relations\Relation $relationship): void |
|
323 | |||
324 | /** |
||
325 | * @return DataTable |
||
326 | */ |
||
327 | 39 | private function _setSelection(): DataTable |
|
344 | |||
345 | /** |
||
346 | * @return array |
||
347 | */ |
||
348 | 39 | private function _getColumnsForSelect(): array |
|
357 | |||
358 | 35 | private function _initColumns(): void |
|
364 | |||
365 | /** |
||
366 | * @return array |
||
367 | */ |
||
368 | 35 | private function _fetchHeaders(): array |
|
378 | |||
379 | /** |
||
380 | * @param $name |
||
381 | * @return mixed |
||
382 | */ |
||
383 | 2 | public function __get($name) |
|
389 | } |