1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Renderer; |
4
|
|
|
|
5
|
|
|
use Dtc\GridBundle\Grid\Column\AbstractGridColumn; |
6
|
|
|
|
7
|
|
|
class JQGridRenderer extends AbstractJqueryRenderer |
8
|
|
|
{ |
9
|
|
|
protected $jqGridCss = []; |
10
|
|
|
protected $jqGridJs = []; |
11
|
|
|
protected $options = array( |
12
|
|
|
'datatype' => 'json', |
13
|
|
|
'jsonReader' => array( |
14
|
|
|
'root' => 'rows', |
15
|
|
|
'total' => 'total', |
16
|
|
|
'records' => 'records', |
17
|
|
|
'page' => 'page', |
18
|
|
|
'repeatitems' => false, |
19
|
|
|
), |
20
|
|
|
|
21
|
|
|
'url' => null, |
22
|
|
|
'cell' => '', |
23
|
|
|
'width' => '840', |
24
|
|
|
'height' => '400', |
25
|
|
|
'loadui' => 'disable', |
26
|
|
|
'altRows' => true, |
27
|
|
|
'viewrecords' => true, |
28
|
|
|
'multiselect' => true, |
29
|
|
|
'styleUI' => 'Bootstrap', |
30
|
|
|
|
31
|
|
|
// Paging params |
32
|
|
|
'prmNames' => array( |
33
|
|
|
'page' => 'page', |
34
|
|
|
'rows' => 'limit', |
35
|
|
|
'sort' => 'sort_column', |
36
|
|
|
'order' => 'sort_order', |
37
|
|
|
'nd' => null, |
38
|
|
|
), |
39
|
|
|
|
40
|
|
|
'ajaxGridOptions' => array( |
41
|
|
|
'cache' => false, |
42
|
|
|
'ifModified' => false, |
43
|
|
|
), |
44
|
|
|
|
45
|
|
|
// Pager Config |
46
|
|
|
'pager' => 'grid-pager', |
47
|
|
|
'recordtext' => 'View {0} - {1} of {2}', |
48
|
|
|
'emptyrecords' => 'No records to view', |
49
|
|
|
'loadtext' => 'Loading...', |
50
|
|
|
'pgtext' => 'Page {0} of {1}', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
protected function afterBind() |
54
|
|
|
{ |
55
|
|
|
$id = $this->gridSource->getDivId(); |
56
|
|
|
$this->options['pager'] = "{$id}-pager"; |
57
|
|
|
|
58
|
|
|
$params = array( |
59
|
|
|
'id' => $this->gridSource->getId(), |
60
|
|
|
'renderer' => 'jq_grid', |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$url = $this->router->generate('dtc_grid_data', $params); |
64
|
|
|
$this->options['url'] = $url; |
65
|
|
|
|
66
|
|
|
/** @var AbstractGridColumn $column */ |
67
|
|
|
foreach ($this->gridSource->getColumns() as $column) { |
68
|
|
|
$info = array(); |
69
|
|
|
$info['label'] = $column->getLabel(); |
70
|
|
|
$info['name'] = $column->getField(); |
71
|
|
|
$info['index'] = $column->getField(); |
72
|
|
|
$info['sortable'] = $column->getOption('sortable') ? true : false; |
73
|
|
|
$info = array_merge($info, $column->getOptions()); |
74
|
|
|
|
75
|
|
|
$this->options['colModel'][] = $info; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($sortInfo = $this->gridSource->getDefaultSort()) { |
79
|
|
|
if ($sortInfo['column']) { |
80
|
|
|
$this->options['sortname'] = $sortInfo['column']; |
81
|
|
|
$this->options['sortorder'] = strtolower($sortInfo['direction'] ?: 'ASC'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getData() |
87
|
|
|
{ |
88
|
|
|
$columns = $this->gridSource->getColumns(); |
89
|
|
|
$gridSource = $this->gridSource; |
90
|
|
|
$records = $gridSource->getRecords(); |
91
|
|
|
|
92
|
|
|
$retVal = array( |
93
|
|
|
'page' => $gridSource->getPager() |
94
|
|
|
->getCurrentPage(), |
95
|
|
|
'total' => $gridSource->getPager() |
96
|
|
|
->getTotalPages(), |
97
|
|
|
'records' => $gridSource->getCount(), |
98
|
|
|
'id' => $gridSource->getId(), // unique id |
99
|
|
|
); |
100
|
|
|
|
101
|
|
View Code Duplication |
foreach ($records as $record) { |
|
|
|
|
102
|
|
|
$info = array(); |
103
|
|
|
/** @var AbstractGridColumn $column */ |
104
|
|
|
foreach ($columns as $column) { |
105
|
|
|
if (method_exists($column, 'setRouter')) { |
106
|
|
|
$column->setRouter($this->router); |
107
|
|
|
} |
108
|
|
|
if (method_exists($column, 'setGridSourceId')) { |
109
|
|
|
$column->setGridSourceId($gridSource->getId()); |
110
|
|
|
} |
111
|
|
|
$info[$column->getField()] = $column->format($record, $this->gridSource); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$retVal['rows'][] = $info; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $retVal; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setJqGridCss(array $css) |
121
|
|
|
{ |
122
|
|
|
$this->jqGridCss = $css; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setJqGridJs(array $js) |
126
|
|
|
{ |
127
|
|
|
$this->jqGridJs = $js; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array|null $params |
132
|
|
|
*/ |
133
|
|
|
public function getParams(array &$params = null) |
134
|
|
|
{ |
135
|
|
|
if (null === $params) { |
136
|
|
|
$params = []; |
137
|
|
|
} |
138
|
|
|
parent::getParams($params); |
139
|
|
|
$params['dtc_grid_jq_grid_css'] = $this->jqGridCss; |
140
|
|
|
$params['dtc_grid_jq_grid_js'] = $this->jqGridJs; |
141
|
|
|
|
142
|
|
|
return $params; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function render() |
146
|
|
|
{ |
147
|
|
|
$id = $this->gridSource->getDivId(); |
148
|
|
|
|
149
|
|
|
$params = array( |
150
|
|
|
'options' => $this->options, |
151
|
|
|
'id' => $id, |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$template = 'DtcGridBundle:Grid:jq_grid.html.twig'; |
155
|
|
|
|
156
|
|
|
return $this->twigEngine->render($template, $params); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.