|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_table\output_kw; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_connect\core\ConnectException; |
|
7
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
|
8
|
|
|
use kalanis\kw_table\core\Table; |
|
9
|
|
|
use kalanis\kw_table\core\TableException; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class KwRenderer |
|
14
|
|
|
* @package kalanis\kw_table\output_kw |
|
15
|
|
|
* Render output in html templates from kw_template |
|
16
|
|
|
*/ |
|
17
|
|
|
class KwRenderer extends Table\AOutput |
|
18
|
|
|
{ |
|
19
|
|
|
protected Html\TableBase $templateBase; |
|
20
|
|
|
protected Html\TableCell $templateCell; |
|
21
|
|
|
protected Html\TableFoot $templateFoot; |
|
22
|
|
|
protected Html\TableHead $templateHead; |
|
23
|
|
|
protected Html\TableHeadSorted $templateHeadSorted; |
|
24
|
|
|
protected Html\TableRow $templateRow; |
|
25
|
|
|
protected Html\TableScript $templateScript; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Table $table) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($table); |
|
30
|
|
|
$this->templateBase = new Html\TableBase(); |
|
31
|
|
|
$this->templateCell = new Html\TableCell(); |
|
32
|
|
|
$this->templateFoot = new Html\TableFoot(); |
|
33
|
|
|
$this->templateHead = new Html\TableHead(); |
|
34
|
|
|
$this->templateHeadSorted = new Html\TableHeadSorted(); |
|
35
|
|
|
$this->templateRow = new Html\TableRow(); |
|
36
|
|
|
$this->templateScript = new Html\TableScript(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws ConnectException |
|
41
|
|
|
* @throws RenderException |
|
42
|
|
|
* @throws TableException |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function render(): string |
|
46
|
|
|
{ |
|
47
|
|
|
$this->renderPagers(); |
|
48
|
|
|
$this->renderFilter(); |
|
49
|
|
|
$this->renderScript(); |
|
50
|
|
|
return $this->templateBase->setData( |
|
51
|
|
|
$this->getCells(), |
|
52
|
|
|
$this->getHeader(), |
|
53
|
|
|
$this->getHeadFilter(), |
|
54
|
|
|
$this->getFootFilter(), |
|
55
|
|
|
$this->table->getClassesInString() |
|
56
|
|
|
)->render(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function renderPagers(): void |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
$paging = $this->table->getPager(); |
|
63
|
|
|
} catch (TableException $ex) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->table->showPagerOnHead()) { |
|
68
|
|
|
$this->templateBase->addPagerHead($paging->render()); |
|
69
|
|
|
} |
|
70
|
|
|
if ($this->table->showPagerOnFoot()) { |
|
71
|
|
|
$this->templateBase->addPagerFoot($paging->render()); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @throws RenderException |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function renderFilter(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$headerFilter = $this->table->getHeaderFilter(); |
|
81
|
|
|
$footerFilter = $this->table->getFooterFilter(); |
|
82
|
|
|
$this->templateBase->addFilter( |
|
83
|
|
|
$headerFilter ? $headerFilter->renderStart() : ($footerFilter ? $footerFilter->renderStart() : ''), |
|
84
|
|
|
$headerFilter ? $headerFilter->renderEnd() : ($footerFilter ? $footerFilter->renderEnd() : '') |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function renderScript(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$headerFilter = $this->table->getHeaderFilter(); |
|
91
|
|
|
$footerFilter = $this->table->getFooterFilter(); |
|
92
|
|
|
$formName = $this->table->getFormName(); |
|
93
|
|
|
if ($formName && ($headerFilter || $footerFilter)) { |
|
94
|
|
|
$this->templateBase->addScript( |
|
95
|
|
|
$this->templateScript->reset()->setData($formName)->render() |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @throws ConnectException |
|
102
|
|
|
* @throws TableException |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getCells(): string |
|
106
|
|
|
{ |
|
107
|
|
|
$cell = []; |
|
108
|
|
|
foreach ($this->table->getTableData() as $row) { |
|
109
|
|
|
/** @var Table\Internal\Row $row */ |
|
110
|
|
|
$this->templateRow->reset()->setData($row->getCellStyle($row->getSource())); |
|
|
|
|
|
|
111
|
|
|
foreach ($row as $column) { |
|
112
|
|
|
/** @var Table\Columns\AColumn $column */ |
|
113
|
|
|
$this->templateRow->addCell($this->templateCell->reset()->setData( |
|
114
|
|
|
$column->translate($row->getSource()), |
|
|
|
|
|
|
115
|
|
|
$column->getCellStyle($row->getSource()) |
|
116
|
|
|
)->render()); |
|
117
|
|
|
} |
|
118
|
|
|
$cell[] = $this->templateRow->render(); |
|
119
|
|
|
} |
|
120
|
|
|
return implode('', $cell); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function getHeader(): string |
|
124
|
|
|
{ |
|
125
|
|
|
$order = $this->table->getOrderOrNull(); |
|
126
|
|
|
$this->templateRow->reset()->setData(); |
|
127
|
|
|
foreach ($this->table->getColumns() as $column) { |
|
128
|
|
|
if ($order && $order->isInOrder($column)) { |
|
129
|
|
|
$this->templateRow->addCell($this->templateHeadSorted->reset()->setData( |
|
130
|
|
|
$order->getHeaderText($column), $order->getHref($column) |
|
131
|
|
|
)->render()); |
|
132
|
|
|
} else { |
|
133
|
|
|
$this->templateRow->addCell($this->templateHead->reset()->setData( |
|
134
|
|
|
$column->getHeaderText() |
|
135
|
|
|
)->render()); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
return $this->templateRow->render(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @throws RenderException |
|
143
|
|
|
* @throws TableException |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getHeadFilter(): string |
|
147
|
|
|
{ |
|
148
|
|
|
$headerFilter = $this->table->getHeaderFilter(); |
|
149
|
|
|
if (!$headerFilter) { |
|
150
|
|
|
return ''; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$this->templateRow->reset()->setData(); |
|
154
|
|
|
foreach ($this->table->getColumns() as $column) { |
|
155
|
|
|
$this->templateRow->addCell($this->templateHead->reset()->setData( |
|
156
|
|
|
$column->hasHeaderFilterField() ? $headerFilter->renderHeaderInput($column) : '' |
|
157
|
|
|
)->render()); |
|
158
|
|
|
} |
|
159
|
|
|
return $this->templateRow->render(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @throws RenderException |
|
164
|
|
|
* @throws TableException |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function getFootFilter(): string |
|
168
|
|
|
{ |
|
169
|
|
|
$footerFilter = $this->table->getFooterFilter(); |
|
170
|
|
|
if (!$footerFilter) { |
|
171
|
|
|
return ''; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->templateRow->reset()->setData(); |
|
175
|
|
|
foreach ($this->table->getColumns() as $column) { |
|
176
|
|
|
$this->templateRow->addCell($this->templateCell->reset()->setData( |
|
177
|
|
|
$column->hasFooterFilterField() ? $footerFilter->renderFooterInput($column) : '' |
|
178
|
|
|
)->render()); |
|
179
|
|
|
} |
|
180
|
|
|
return $this->templateRow->render(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|