1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Helpers; |
4
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Models\Column; |
6
|
|
|
use hamburgscleanest\DataTables\Models\Header; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TableRenderer |
13
|
|
|
* @package hamburgscleanest\DataTables\Helpers |
14
|
|
|
*/ |
15
|
|
|
class TableRenderer { |
16
|
|
|
|
17
|
|
|
/** @var Request */ |
18
|
|
|
private $_request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* TableRenderer constructor. |
22
|
|
|
* @param Request $request |
23
|
|
|
*/ |
24
|
29 |
|
public function __construct(Request $request) |
|
|
|
|
25
|
|
|
{ |
26
|
29 |
|
$this->_request = $request; |
27
|
29 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Starts the table. |
31
|
|
|
* |
32
|
|
|
* @param null|string $classes |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
29 |
|
public function open(?string $classes = null) : string |
36
|
|
|
{ |
37
|
29 |
|
return '<table class="' . ($classes ?? 'table') . '">'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Closes the table. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
29 |
|
public function close() : string |
46
|
|
|
{ |
47
|
29 |
|
return '</table>'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Renders the column headers. |
52
|
|
|
* |
53
|
|
|
* @param array $headers |
54
|
|
|
* @param array $formatters |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
29 |
|
public function renderHeaders(array $headers, array $formatters = []) : string |
58
|
|
|
{ |
59
|
29 |
|
$html = '<tr>'; |
60
|
|
|
|
61
|
|
|
/** @var Header $header */ |
62
|
29 |
|
foreach ($headers as $header) |
63
|
|
|
{ |
64
|
29 |
|
$html .= $header->formatArray($formatters, $this->_request)->print(); |
65
|
|
|
} |
66
|
29 |
|
$html .= '</tr>'; |
67
|
|
|
|
68
|
29 |
|
return $html; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Displays the table body. |
73
|
|
|
* |
74
|
|
|
* @param Collection $data |
75
|
|
|
* |
76
|
|
|
* @param array $columns |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
29 |
|
public function renderBody(Collection $data, array $columns = []) : string |
80
|
|
|
{ |
81
|
29 |
|
$html = ''; |
82
|
29 |
|
foreach ($data as $row) |
83
|
|
|
{ |
84
|
29 |
|
$html .= $this->_renderRow($row, $columns); |
85
|
|
|
} |
86
|
|
|
|
87
|
29 |
|
return $html; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Displays a single row. |
92
|
|
|
* |
93
|
|
|
* @param Model $rowModel |
94
|
|
|
* |
95
|
|
|
* @param array $columns |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
29 |
|
private function _renderRow(Model $rowModel, array $columns) : string |
99
|
|
|
{ |
100
|
29 |
|
$attributes = $rowModel->getAttributes() + $this->_getMutatedAttributes($rowModel, $this->_getColumnNames($columns)); |
101
|
|
|
|
102
|
29 |
|
$html = '<tr>'; |
103
|
|
|
/** @var Column $column */ |
104
|
29 |
|
foreach ($columns as $column) |
105
|
|
|
{ |
106
|
29 |
|
$html .= '<td>' . $column->format($column->getRelation() !== null ? $this->_getColumnValueFromRelation($rowModel, $column) : ($attributes[$column->getName()] ?? '')) . '</td>'; |
107
|
|
|
} |
108
|
29 |
|
$html .= '</tr>'; |
109
|
|
|
|
110
|
29 |
|
return $html; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get all the mutated attributes which are needed. |
115
|
|
|
* |
116
|
|
|
* @param Model $model |
117
|
|
|
* @param array $columns |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
29 |
|
private function _getMutatedAttributes(Model $model, array $columns = []) : array |
121
|
|
|
{ |
122
|
29 |
|
$attributes = []; |
123
|
29 |
|
foreach (\array_intersect_key($model->getMutatedAttributes(), $columns) as $attribute) |
124
|
|
|
{ |
125
|
29 |
|
$attributes[$attribute] = $model->{$attribute}; |
126
|
|
|
} |
127
|
|
|
|
128
|
29 |
|
return $attributes; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get all column names. |
133
|
|
|
* |
134
|
|
|
* @param array $columns |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
29 |
|
private function _getColumnNames(array $columns) : array |
138
|
|
|
{ |
139
|
|
|
return \array_map(function($column) |
140
|
|
|
{ |
141
|
|
|
/** @var Column $column */ |
142
|
29 |
|
return $column->getName(); |
143
|
29 |
|
}, |
144
|
29 |
|
$this->_getColumnsWithoutRelations($columns) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get only the columns which are attributes from the base model. |
150
|
|
|
* |
151
|
|
|
* @param array $columns |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
29 |
|
private function _getColumnsWithoutRelations(array $columns) : array |
155
|
|
|
{ |
156
|
29 |
|
return \array_filter( |
157
|
|
|
$columns, |
158
|
29 |
|
function($column) |
159
|
|
|
{ |
160
|
|
|
/** @var Column $column */ |
161
|
29 |
|
return $column->getRelation() === null; |
162
|
29 |
|
} |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Model $model |
168
|
|
|
* @param Column $column |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
3 |
|
private function _getColumnValueFromRelation(Model $model, Column $column) : string |
172
|
|
|
{ |
173
|
3 |
|
$columnRelation = $column->getRelation(); |
174
|
3 |
|
$relation = $model->getRelation($columnRelation->name); |
175
|
|
|
|
176
|
3 |
|
if ($relation instanceof Model) |
177
|
|
|
{ |
178
|
1 |
|
return $relation->{$column->getName()}; |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
return $columnRelation->getValue($column->getName(), $relation); |
182
|
|
|
} |
183
|
|
|
} |