1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Renderer; |
4
|
|
|
|
5
|
|
|
use Dtc\GridBundle\Grid\Column\AbstractGridColumn; |
6
|
|
|
|
7
|
|
|
class DataTablesRenderer extends AbstractJqueryRenderer |
8
|
|
|
{ |
9
|
|
|
protected $dataTablesCss = []; |
10
|
|
|
protected $dataTablesJs = []; |
11
|
|
|
|
12
|
|
|
protected $options = array( |
13
|
|
|
'bProcessing' => true, |
14
|
|
|
'searchDelay' => 350, |
15
|
|
|
'table_attr' => array( |
16
|
|
|
'class' => 'display table table-striped table-bordered small-font', |
17
|
|
|
), |
18
|
|
|
'sPaginationType' => 'bootstrap', |
19
|
|
|
'bServerSide' => true, |
20
|
|
|
'oLanguage' => array( |
21
|
|
|
'sLengthMenu' => '_MENU_ records per page', |
22
|
|
|
), |
23
|
|
|
'aoColumns' => array(array( |
24
|
|
|
'bSortable' => false, |
25
|
|
|
'sWidth' => '20%', |
26
|
|
|
'aTargets' => array(-1), |
27
|
|
|
)), |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
const MODE_AJAX = 1; |
31
|
|
|
const MODE_SERVER = 2; |
32
|
|
|
|
33
|
|
|
protected $mode = 1; |
34
|
|
|
|
35
|
|
|
public function setMode($mode) |
36
|
|
|
{ |
37
|
|
|
$this->mode = $mode; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the type (bootstrap, bootstrap4, foundation, etc.). |
42
|
|
|
* |
43
|
|
|
* @param $type |
44
|
|
|
*/ |
45
|
|
|
public function setDataTablesCss($css) |
46
|
|
|
{ |
47
|
|
|
$this->dataTablesCss = $css; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getDataTablesCss() |
51
|
|
|
{ |
52
|
|
|
return $this->dataTablesCss; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setDataTablesJs($js) |
56
|
|
|
{ |
57
|
|
|
$this->dataTablesJs = $js; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getDataTablesJs() |
61
|
|
|
{ |
62
|
|
|
return $this->dataTablesJs; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array|null $params |
67
|
|
|
*/ |
68
|
|
|
public function getParams(array &$params = null) |
69
|
|
|
{ |
70
|
|
|
if (null === $params) { |
71
|
|
|
$params = []; |
72
|
|
|
} |
73
|
|
|
parent::getParams($params); |
74
|
|
|
$params['dtc_grid_datatables_css'] = $this->dataTablesCss; |
75
|
|
|
$params['dtc_grid_datatables_js'] = $this->dataTablesJs; |
76
|
|
|
$cssList = ['css/dtc_grid_spinner.css']; |
77
|
|
|
$jsList = ['js/jquery.datatable/DT_bootstrap.js', |
78
|
|
|
'js/jquery.datatable/DT_action.js', |
79
|
|
|
'js/jquery.datatable/jquery.jqtable.js', ]; |
80
|
|
|
|
81
|
|
|
foreach ($cssList as $css) { |
82
|
|
|
$mtime = filemtime(__DIR__.'/../../Resources/public/'.$css); |
83
|
|
|
$params['dtc_grid_local_css'][] = $css.'?v='.$mtime; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($jsList as $js) { |
87
|
|
|
$mtime = filemtime(__DIR__.'/../../Resources/public/'.$js); |
88
|
|
|
$params['dtc_grid_local_js'][] = $js.'?v='.$mtime; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $params; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function afterBind() |
95
|
|
|
{ |
96
|
|
|
$id = $this->gridSource->getDivId(); |
97
|
|
|
$this->options['pager'] = "{$id}-pager"; |
98
|
|
|
|
99
|
|
|
$fields = array_keys($this->gridSource->getColumns()); |
100
|
|
|
|
101
|
|
|
// We need to pass filter information here. |
102
|
|
|
$params = array( |
103
|
|
|
'id' => $this->gridSource->getId(), |
104
|
|
|
'renderer' => 'datatables', |
105
|
|
|
'filter' => $this->gridSource->getFilter(), |
106
|
|
|
'parameters' => $this->gridSource->getParameters(), |
107
|
|
|
'order' => $this->gridSource->getOrderBy(), |
108
|
|
|
'fields' => $fields, |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$url = $this->router->generate('dtc_grid_data', $params); |
112
|
|
|
$this->options['sAjaxSource'] = $url; |
113
|
|
|
|
114
|
|
|
$columnsDef = array(); |
115
|
|
|
/** @var AbstractGridColumn $column */ |
116
|
|
|
foreach ($this->gridSource->getColumns() as $index => $column) { |
117
|
|
|
$info = array(); |
118
|
|
|
$info['bSortable'] = $column->getOption('sortable') ? true : false; |
119
|
|
|
$info['sName'] = $column->getField(); |
120
|
|
|
|
121
|
|
|
if ($width = $column->getOption('width')) { |
122
|
|
|
$info['sWidth'] = $width; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$info['aTargets'] = array($index); |
126
|
|
|
$info = array_merge($info, $column->getOptions()); |
127
|
|
|
$columnsDef[] = $info; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->options['aoColumns'] = $columnsDef; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getData() |
134
|
|
|
{ |
135
|
|
|
$columns = $this->gridSource->getColumns(); |
136
|
|
|
$gridSource = $this->gridSource; |
137
|
|
|
$records = $gridSource->getRecords(); |
138
|
|
|
$count = $gridSource->getCount(); |
139
|
|
|
|
140
|
|
|
$retVal = array( |
141
|
|
|
'page' => $gridSource->getPager() |
142
|
|
|
->getCurrentPage(), |
143
|
|
|
'total_pages' => $gridSource->getPager() |
144
|
|
|
->getTotalPages(), |
145
|
|
|
'iTotalRecords' => (int) $count, |
146
|
|
|
'iTotalDisplayRecords' => $count, |
147
|
|
|
'id' => $gridSource->getId(), // unique id |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$data = array(); |
151
|
|
View Code Duplication |
foreach ($records as $record) { |
|
|
|
|
152
|
|
|
$info = array(); |
153
|
|
|
/** @var AbstractGridColumn $column */ |
154
|
|
|
foreach ($columns as $column) { |
155
|
|
|
if (method_exists($column, 'setRouter')) { |
156
|
|
|
$column->setRouter($this->router); |
157
|
|
|
} |
158
|
|
|
if (method_exists($column, 'setGridSourceId')) { |
159
|
|
|
$column->setGridSourceId($gridSource->getId()); |
160
|
|
|
} |
161
|
|
|
$info[] = $column->format($record, $gridSource); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$data[] = $info; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$retVal['aaData'] = $data; |
168
|
|
|
|
169
|
|
|
return $retVal; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function render() |
173
|
|
|
{ |
174
|
|
|
$id = $this->gridSource->getDivId(); |
175
|
|
|
|
176
|
|
|
$options = $this->options; |
177
|
|
|
unset($options['table_attr']); |
178
|
|
|
|
179
|
|
|
$params = array( |
180
|
|
|
'options' => $options, |
181
|
|
|
'table_attr' => $this->options['table_attr'], |
182
|
|
|
'columns' => $this->gridSource->getColumns(), |
183
|
|
|
'id' => $id, |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$template = 'DtcGridBundle:Grid:datatables.html.twig'; |
187
|
|
|
|
188
|
|
|
return $this->twigEngine->render($template, $params); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.