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
|
31 |
|
public function __construct(Request $request) |
25
|
|
|
{ |
26
|
31 |
|
$this->_request = $request; |
27
|
31 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Starts the table. |
31
|
|
|
* |
32
|
|
|
* @param null|string $classes |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
31 |
|
public function open(? string $classes = null) : string |
36
|
|
|
{ |
37
|
31 |
|
return '<table class="' . ($classes ?? 'table') . '">'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Closes the table. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
31 |
|
public function close() : string |
46
|
|
|
{ |
47
|
31 |
|
return '</table>'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Renders the column headers. |
52
|
|
|
* |
53
|
|
|
* @param array $headers |
54
|
|
|
* @param array $formatters |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
31 |
|
public function renderHeaders(array $headers, array $formatters = []) : string |
58
|
|
|
{ |
59
|
31 |
|
$html = '<tr>'; |
60
|
|
|
|
61
|
|
|
/** @var Header $header */ |
62
|
31 |
|
foreach ($headers as $header) |
63
|
|
|
{ |
64
|
31 |
|
$html .= $header->formatArray($formatters, $this->_request)->print(); |
65
|
|
|
} |
66
|
31 |
|
$html .= '</tr>'; |
67
|
|
|
|
68
|
31 |
|
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
|
31 |
|
public function renderBody(Collection $data, array $columns = []) : string |
80
|
|
|
{ |
81
|
31 |
|
$html = ''; |
82
|
31 |
|
foreach ($data as $row) |
83
|
|
|
{ |
84
|
31 |
|
$html .= $this->_renderRow($row, $columns); |
85
|
|
|
} |
86
|
|
|
|
87
|
31 |
|
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
|
31 |
|
private function _renderRow(Model $rowModel, array $columns) : string |
99
|
|
|
{ |
100
|
31 |
|
$html = '<tr>'; |
101
|
|
|
/** @var Column $column */ |
102
|
31 |
|
foreach ($columns as $column) |
103
|
|
|
{ |
104
|
|
|
// TODO: simplify.. |
105
|
|
|
$html .= '<td>' . |
106
|
31 |
|
$column->format( |
107
|
31 |
|
$column->getRelation() !== null ? |
108
|
3 |
|
$this->_getColumnValueFromRelation($rowModel, $column) : |
109
|
31 |
|
$this->_getColumnValue($rowModel, $column) |
110
|
|
|
) . |
111
|
31 |
|
'</td>'; |
112
|
|
|
} |
113
|
31 |
|
$html .= '</tr>'; |
114
|
|
|
|
115
|
31 |
|
return $html; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the value of a column. |
120
|
|
|
* |
121
|
|
|
* @param Model $rowModel |
122
|
|
|
* @param Column $column |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
31 |
|
private function _getColumnValue(Model $rowModel, Column $column) : string |
126
|
|
|
{ |
127
|
31 |
|
$columnName = $column->getName(); |
128
|
31 |
|
if(!property_exists($rowModel, $columnName)) |
129
|
|
|
{ |
130
|
31 |
|
return ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return (string) $rowModel->{$columnName}; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Model $model |
138
|
|
|
* @param Column $column |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
3 |
|
private function _getColumnValueFromRelation(Model $model, Column $column) : string |
142
|
|
|
{ |
143
|
3 |
|
$columnRelation = $column->getRelation(); |
144
|
3 |
|
$relation = $model->getRelation($columnRelation->name); |
145
|
|
|
|
146
|
3 |
|
if ($relation instanceof Model) |
147
|
|
|
{ |
148
|
1 |
|
return $relation->{$column->getName()}; |
149
|
|
|
} |
150
|
|
|
|
151
|
2 |
|
return $columnRelation->getValue($column->getName(), $relation); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|