1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfcDatagrid\Renderer\JqGrid; |
4
|
|
|
|
5
|
|
|
use Zend\Http\PhpEnvironment\Request as HttpRequest; |
6
|
|
|
use Zend\View\Model\JsonModel; |
7
|
|
|
use ZfcDatagrid\Column; |
8
|
|
|
use ZfcDatagrid\Renderer\AbstractRenderer; |
9
|
|
|
|
10
|
|
|
class Renderer extends AbstractRenderer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public function getName() |
16
|
|
|
{ |
17
|
|
|
return 'jqGrid'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public function isHtml() |
24
|
|
|
{ |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public function isExport() |
32
|
|
|
{ |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return HttpRequest |
38
|
|
|
* |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function getRequest() |
42
|
|
|
{ |
43
|
|
|
$request = parent::getRequest(); |
44
|
|
|
if (!$request instanceof HttpRequest) { |
45
|
|
|
throw new \Exception('Request must be an instance of Zend\Http\PhpEnvironment\Request for HTML rendering'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $request; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see \ZfcDatagrid\Renderer\AbstractRenderer::getSortConditions() |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
* |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function getSortConditions() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (is_array($this->sortConditions)) { |
61
|
|
|
return $this->sortConditions; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$request = $this->getRequest(); |
65
|
|
|
|
66
|
|
|
$optionsRenderer = $this->getOptionsRenderer(); |
67
|
|
|
$parameterNames = $optionsRenderer['parameterNames']; |
68
|
|
|
|
69
|
|
|
$sortConditions = []; |
70
|
|
|
|
71
|
|
|
$sortColumns = $request->getPost($parameterNames['sortColumns'], $request->getQuery($parameterNames['sortColumns'])); |
72
|
|
|
$sortDirections = $request->getPost($parameterNames['sortDirections'], $request->getQuery($parameterNames['sortDirections'])); |
73
|
|
|
if ($sortColumns != '') { |
74
|
|
|
$sortColumns = explode(',', $sortColumns); |
75
|
|
|
$sortDirections = explode(',', $sortDirections); |
76
|
|
|
|
77
|
|
|
if (count($sortColumns) != count($sortDirections)) { |
78
|
|
|
throw new \Exception('Count missmatch order columns/direction'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($sortColumns as $key => $sortColumn) { |
82
|
|
|
$sortDirection = strtoupper($sortDirections[$key]); |
83
|
|
|
|
84
|
|
|
if ($sortDirection != 'ASC' && $sortDirection != 'DESC') { |
85
|
|
|
$sortDirection = 'ASC'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($this->getColumns() as $column) { |
89
|
|
|
/* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
90
|
|
|
if ($column->getUniqueId() == $sortColumn) { |
91
|
|
|
$sortConditions[] = [ |
92
|
|
|
'sortDirection' => $sortDirection, |
93
|
|
|
'column' => $column, |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$column->setSortActive($sortDirection); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!empty($sortConditions)) { |
103
|
|
|
$this->sortConditions = $sortConditions; |
104
|
|
|
} else { |
105
|
|
|
// No user sorting -> get default sorting |
106
|
|
|
$this->sortConditions = $this->getSortConditionsDefault(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->sortConditions; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
* |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
|
public function getFilters() |
118
|
|
|
{ |
119
|
|
|
if (is_array($this->filters)) { |
120
|
|
|
// set from cache! (for export) |
121
|
|
|
return $this->filters; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$filters = []; |
125
|
|
|
|
126
|
|
|
$optionsRenderer = $this->getOptionsRenderer(); |
127
|
|
|
$parameterNames = $optionsRenderer['parameterNames']; |
128
|
|
|
|
129
|
|
|
$request = $this->getRequest(); |
130
|
|
|
$isSearch = $request->getPost($parameterNames['isSearch'], $request->getQuery($parameterNames['isSearch'])); |
131
|
|
|
if ('true' == $isSearch) { |
132
|
|
|
// User filtering |
133
|
|
|
foreach ($this->getColumns() as $column) { |
134
|
|
|
/* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
135
|
|
|
if ($request->getPost($column->getUniqueId(), $request->getQuery($column->getUniqueId())) != '') { |
136
|
|
|
$value = $request->getPost($column->getUniqueId(), $request->getQuery($column->getUniqueId())); |
137
|
|
|
|
138
|
|
|
$filter = new \ZfcDatagrid\Filter(); |
139
|
|
|
$filter->setFromColumn($column, $value); |
140
|
|
|
|
141
|
|
|
$filters[] = $filter; |
142
|
|
|
|
143
|
|
|
$column->setFilterActive($filter->getDisplayColumnValue()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (empty($filters)) { |
149
|
|
|
// No user sorting -> get default sorting |
150
|
|
|
$filters = $this->getFiltersDefault(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->filters = $filters; |
154
|
|
|
|
155
|
|
|
return $this->filters; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getCurrentPageNumber() |
159
|
|
|
{ |
160
|
|
|
$optionsRenderer = $this->getOptionsRenderer(); |
161
|
|
|
$parameterNames = $optionsRenderer['parameterNames']; |
162
|
|
|
|
163
|
|
|
$request = $this->getRequest(); |
164
|
|
|
if ($request instanceof HttpRequest) { |
165
|
|
|
$currentPage = $request->getPost($parameterNames['currentPage'], $request->getQuery($parameterNames['currentPage'])); |
166
|
|
|
if ($currentPage != '') { |
167
|
|
|
$this->currentPageNumber = (int) $currentPage; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return (int) $this->currentPageNumber; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function execute() |
175
|
|
|
{ |
176
|
|
|
$request = $this->getRequest(); |
177
|
|
|
if ($request->isXmlHttpRequest() === true && $request->getPost('nd', $request->getQuery('nd')) != '') { |
178
|
|
|
// AJAX Request...load only data... |
179
|
|
|
$viewModel = new JsonModel(); |
180
|
|
|
$viewModel->setVariable('data', $this->getDataJqGrid()); |
181
|
|
|
} else { |
182
|
|
|
$viewModel = $this->getViewModel(); |
183
|
|
|
$viewModel->setTemplate($this->getTemplate()); |
184
|
|
|
$viewModel->setVariable('data', $this->getDataJqGrid()); |
185
|
|
|
|
186
|
|
|
$columnsRowClickDisabled = []; |
187
|
|
|
$columns = $viewModel->getVariable('columns'); |
188
|
|
|
foreach ($columns as $column) { |
189
|
|
|
/* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
190
|
|
|
|
191
|
|
|
if ($column->isRowClickEnabled() !== true) { |
192
|
|
|
$columnsRowClickDisabled[] = $column->getUniqueId(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$viewModel->setVariable('columnsRowClickDisabled', $columnsRowClickDisabled); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $viewModel; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getData() |
203
|
|
|
{ |
204
|
|
|
$data = parent::getData(); |
205
|
|
|
|
206
|
|
|
foreach ($data as &$row) { |
207
|
|
|
foreach ($this->getColumns() as $column) { |
208
|
|
|
if ($column instanceof Column\Select) { |
|
|
|
|
209
|
|
|
// $row[$column->getUniqueId()] = nl2br($row[$column->getUniqueId()], true); |
|
|
|
|
210
|
|
View Code Duplication |
} elseif ($column instanceof Column\Action) { |
|
|
|
|
211
|
|
|
/* @var $column \ZfcDatagrid\Column\Action */ |
212
|
|
|
|
213
|
|
|
$actions = []; |
214
|
|
|
foreach ($column->getActions() as $action) { |
215
|
|
|
/* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ |
216
|
|
|
if ($action->isDisplayed($row) === true) { |
217
|
|
|
$action->setTitle($this->translate($action->getTitle())); |
218
|
|
|
$actions[] = $action->toHtml($row); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$row[$column->getUniqueId()] = implode(' ', $actions); |
223
|
|
|
} elseif ($column instanceof Column\Action\Icon) { |
224
|
|
|
$row[$column->getUniqueId()] = $column->getIconClass(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $data; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
private function getDataJqGrid() |
233
|
|
|
{ |
234
|
|
|
return [ |
235
|
|
|
'rows' => $this->getData(), |
236
|
|
|
'page' => $this->getPaginator()->getCurrentPageNumber(), |
237
|
|
|
'total' => $this->getPaginator()->count(), |
238
|
|
|
'records' => $this->getPaginator()->getTotalItemCount(), |
239
|
|
|
]; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.