1 | <?php |
||
18 | class DataTable { |
||
19 | |||
20 | /** @var Builder */ |
||
21 | private $_queryBuilder; |
||
22 | |||
23 | /** @var array */ |
||
24 | private $_headerFormatters = []; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $_components = []; |
||
28 | |||
29 | /** @var string */ |
||
30 | private $_classes; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $_columns = []; |
||
34 | |||
35 | /** @var Model */ |
||
36 | private $_model; |
||
37 | |||
38 | /** @var array */ |
||
39 | private $_relations = []; |
||
40 | |||
41 | /** @var string */ |
||
42 | private $_noDataHtml = '<div>no data</div>'; |
||
43 | |||
44 | /** |
||
45 | * Set the base model whose data is displayed in the table. |
||
46 | * |
||
47 | * @param string $modelName |
||
48 | * @param array $columns |
||
49 | * @return $this |
||
50 | * @throws \RuntimeException |
||
51 | */ |
||
52 | 54 | public function model(string $modelName, array $columns = []) : DataTable |
|
53 | { |
||
54 | 54 | if (!\is_subclass_of($modelName, Model::class)) |
|
55 | { |
||
56 | 2 | throw new RuntimeException('Class "' . $modelName . '" is not an active record!'); |
|
57 | } |
||
58 | |||
59 | 52 | $this->_model = new $modelName; |
|
60 | 52 | $this->_queryBuilder = $this->_model->newQuery(); |
|
61 | 52 | $this->_columns = $this->_fetchColumns($columns); |
|
62 | |||
63 | 52 | return $this; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Returns an array of Column objects which may be bound to a formatter. |
||
68 | * |
||
69 | * @param array $columns |
||
70 | * @return array |
||
71 | */ |
||
72 | 53 | private function _fetchColumns(array $columns) : array |
|
73 | { |
||
74 | 53 | $columnModels = []; |
|
75 | 53 | foreach ($columns as $column => $formatter) |
|
76 | { |
||
77 | 47 | [$column, $formatter] = $this->_setColumnFormatter($column, $formatter); |
|
78 | 47 | $columnModels[] = new Column($column, $formatter, $this->_model); |
|
79 | } |
||
80 | |||
81 | 53 | return $columnModels; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param $column |
||
86 | * @param $formatter |
||
87 | * @return array |
||
88 | */ |
||
89 | 47 | private function _setColumnFormatter($column, $formatter) : array |
|
90 | { |
||
91 | 47 | if (\is_int($column)) |
|
92 | { |
||
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 | * @return $this |
||
136 | */ |
||
137 | 28 | public function addComponent(DataComponent $component) : DataTable |
|
138 | { |
||
139 | 28 | $this->_components[] = $component->init($this); |
|
140 | |||
141 | 28 | return $this; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Add a formatter for the column headers. |
||
146 | * |
||
147 | * @param HeaderFormatter $headerFormatter |
||
148 | * @return DataTable |
||
149 | */ |
||
150 | 12 | public function formatHeaders(HeaderFormatter $headerFormatter) : DataTable |
|
151 | { |
||
152 | 12 | $this->_headerFormatters[] = $headerFormatter; |
|
153 | |||
154 | 12 | return $this; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Add a formatter for a column. |
||
159 | * |
||
160 | * @param string $columnName |
||
161 | * @param ColumnFormatter $columnFormatter |
||
162 | * @return DataTable |
||
163 | */ |
||
164 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter) : DataTable |
|
165 | { |
||
166 | /** @var Column $column */ |
||
167 | 2 | $column = \array_first( |
|
168 | 2 | $this->_columns, |
|
169 | function($index, $column) use ($columnName) { |
||
170 | /** @var Column $column */ |
||
171 | 2 | return $column->getName() === $columnName; |
|
172 | 2 | } |
|
173 | ); |
||
174 | |||
175 | 2 | if ($column !== null) |
|
176 | { |
||
177 | 2 | $column->setFormatter($columnFormatter); |
|
178 | } |
||
179 | |||
180 | 2 | return $this; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Add classes to the table. |
||
185 | * |
||
186 | * @param string $classes |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | 1 | public function classes(string $classes) : DataTable |
|
191 | { |
||
192 | 1 | $this->_classes = $classes; |
|
193 | |||
194 | 1 | return $this; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Add a relation to the table. |
||
199 | * |
||
200 | * @param array $relations |
||
201 | * @return DataTable |
||
202 | */ |
||
203 | 4 | public function with(array $relations) : DataTable |
|
204 | { |
||
205 | 4 | $this->_relations += $relations; |
|
206 | |||
207 | 4 | return $this; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Set the HTML which should be displayed when the dataset is empty. |
||
212 | * |
||
213 | * @param string $html |
||
214 | * @return DataTable |
||
215 | */ |
||
216 | 1 | public function noDataHtml(string $html) : DataTable |
|
217 | { |
||
218 | 1 | $this->_noDataHtml = $html; |
|
219 | |||
220 | 1 | return $this; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Set a view which should be displayed when the dataset is empty. |
||
225 | * |
||
226 | * @param string $viewName |
||
227 | * @return DataTable |
||
228 | * @throws \Throwable |
||
229 | */ |
||
230 | 1 | public function noDataView(string $viewName) : DataTable |
|
236 | |||
237 | /** |
||
238 | * Renders the table. |
||
239 | * |
||
240 | * @return string |
||
241 | * @throws \RuntimeException |
||
242 | */ |
||
243 | 40 | public function render() : string |
|
244 | { |
||
245 | 40 | $data = $this->_getData(); |
|
246 | |||
259 | |||
260 | /** |
||
261 | * Get data which should be displayed in the table. |
||
262 | * |
||
263 | * @return Collection |
||
264 | * |
||
265 | * @throws \RuntimeException |
||
266 | */ |
||
267 | 40 | private function _getData() : Collection |
|
284 | |||
285 | 39 | private function _addRelations() : void |
|
299 | |||
300 | /** |
||
301 | * @param string $relation |
||
302 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relationship |
||
303 | */ |
||
304 | 4 | private function _addJoin(string $relation, \Illuminate\Database\Eloquent\Relations\Relation $relationship) : void |
|
316 | |||
317 | /** |
||
318 | * @return DataTable |
||
319 | */ |
||
320 | 39 | private function _setSelection() : DataTable |
|
333 | |||
334 | /** |
||
335 | * Formats the column names to be used in a select statement. |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | 39 | private function _getSelectStatements() : array |
|
348 | |||
349 | 34 | private function _getColumnNames() : array |
|
369 | |||
370 | 35 | private function _initColumns() : void |
|
377 | |||
378 | /** |
||
379 | * @return array |
||
380 | */ |
||
381 | 35 | private function _fetchHeaders() : array |
|
391 | } |