1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Pfilsx\DataGrid\DataGridServiceContainer; |
8
|
|
|
|
9
|
|
|
class DataGridView |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var DataGrid |
13
|
|
|
*/ |
14
|
|
|
private $grid; |
15
|
|
|
/** |
16
|
|
|
* @var DataGridServiceContainer |
17
|
|
|
*/ |
18
|
|
|
private $container; |
19
|
|
|
|
20
|
|
|
private $renderStarted = false; |
21
|
|
|
private $headRendered = false; |
22
|
|
|
private $filtersRendered = false; |
23
|
|
|
private $bodyRendered = false; |
24
|
|
|
private $renderEnded = false; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function __construct(DataGrid $grid, DataGridServiceContainer $container) |
28
|
|
|
{ |
29
|
|
|
$this->grid = $grid; |
30
|
|
|
$this->container = $container; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function renderGridStart($attributes = []): ?string |
34
|
|
|
{ |
35
|
|
|
if (!$this->renderStarted) { |
36
|
|
|
$this->renderStarted = true; |
37
|
|
|
return $this->grid->getTemplate()->renderBlock('grid_start', [ |
38
|
|
|
'attr' => $attributes, |
39
|
|
|
'data_grid' => $this->grid, |
40
|
|
|
'request' => $this->container->getRequest()->getCurrentRequest() |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function renderGridHead(): ?string |
47
|
|
|
{ |
48
|
|
|
if (!$this->headRendered) { |
49
|
|
|
$this->headRendered = true; |
50
|
|
|
return $this->grid->getTemplate()->renderBlock('grid_head', [ |
51
|
|
|
'data_grid' => $this->grid, |
52
|
|
|
'request' => $this->container->getRequest()->getCurrentRequest() |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function renderGridFilters(): ?string |
59
|
|
|
{ |
60
|
|
|
if (!$this->filtersRendered) { |
61
|
|
|
$this->filtersRendered = true; |
62
|
|
|
return $this->grid->getTemplate()->renderBlock('grid_filters', [ |
63
|
|
|
'data_grid' => $this->grid, |
64
|
|
|
'request' => $this->container->getRequest()->getCurrentRequest() |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function renderGridBody(): ?string |
71
|
|
|
{ |
72
|
|
|
if (!$this->bodyRendered) { |
73
|
|
|
$this->bodyRendered = true; |
74
|
|
|
return $this->grid->getTemplate()->renderBlock('grid_body', [ |
75
|
|
|
'data_grid' => $this->grid, |
76
|
|
|
'request' => $this->container->getRequest()->getCurrentRequest() |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function renderGridEnd(): ?string |
83
|
|
|
{ |
84
|
|
|
if ($this->renderStarted && !$this->renderEnded) { |
85
|
|
|
$this->renderEnded = true; |
86
|
|
|
return $this->grid->getTemplate()->renderBlock('grid_end', [ |
87
|
|
|
'data_grid' => $this->grid, |
88
|
|
|
'request' => $this->container->getRequest()->getCurrentRequest() |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function renderGridPagination(): ?string |
95
|
|
|
{ |
96
|
|
|
return $this->grid->getTemplate()->renderBlock('grid_pagination', [ |
97
|
|
|
'data_grid' => $this->grid, |
98
|
|
|
'request' => $this->container->getRequest()->getCurrentRequest() |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function renderGridWidget(): ?string |
103
|
|
|
{ |
104
|
|
|
return implode("\n", [ |
105
|
|
|
'<thead>', |
106
|
|
|
$this->renderGridHead(), |
107
|
|
|
$this->renderGridFilters(), |
108
|
|
|
'</thead>', |
109
|
|
|
'<tbody>', |
110
|
|
|
$this->renderGridBody(), |
111
|
|
|
'</tbody>' |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function renderGridView($attributes = []): ?string |
116
|
|
|
{ |
117
|
|
|
return implode("\n", [ |
118
|
|
|
$this->renderGridStart($attributes), |
119
|
|
|
$this->renderGridWidget(), |
120
|
|
|
$this->renderGridEnd(), |
121
|
|
|
$this->renderGridPagination() |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
} |