/
DataTableHelper.php
304 lines (283 loc) · 8.01 KB
/
DataTableHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
App::uses('HtmlHelper', 'View/Helper');
/**
* DataTable Helper
*
* @package Plugin.DataTable
* @subpackage Plugin.DataTable.View.Helper
* @author Tigran Gabrielyan
*
* @property HtmlHelper $Html
*/
class DataTableHelper extends HtmlHelper {
/**
* Settings
*
* - `table` See `render()` method for setting info
* - `scriptBlock` String for block name or false to disable output of default init script
* - `js` See `script()` method for setting info
*
* @var array
*/
public $settings = array(
'table' => array(
'class' => 'dataTable',
'trOptions' => array(),
'thOptions' => array(),
'theadOptions' => array(),
'tbody' => '',
'tbodyOptions' => array(),
'tfoot' => '',
'tfootOptions' => array(),
),
'scriptBlock' => 'script',
'js' => array(
'sAjaxSource' => array(),
'bServerSide' => true,
),
);
/**
* Table header labels
*
* @var array
*/
protected $_labels = array();
/**
* Column data passed from controller
*
* @var array
*/
protected $_dtColumns;
/**
* Javascript settings for all paginated models
*
* @var
*/
protected $_dtSettings = array();
/**
* All models setup for pagination
*
* @var array
*/
protected $_paginatedModels = array();
/**
* Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
if (isset($this->_View->viewVars['dtColumns'])) {
$dtColumns = $this->_View->viewVars['dtColumns'];
foreach($dtColumns as $model => $columns) {
$this->_paginatedModels[] = $model;
$this->_parseSettings($model, $columns);
}
}
}
/**
* Output dataTable settings to script block
*
* @param string $viewFile
*/
public function afterRender($viewFile) {
foreach($this->_paginatedModels as $model) {
if (!array_key_exists($model, $this->_dtSettings)) {
$this->jsSettings($model);
}
}
$jsVar = sprintf('var dataTableSettings = %s;', json_encode($this->_dtSettings));
$this->scriptBlock($jsVar, array('block' => 'dataTableSettings'));
if ($this->settings['scriptBlock'] !== false) {
$initScript = <<< INIT_SCRIPT
$(document).ready(function() {
$('.dataTable').each(function() {
var table = $(this);
var model = table.attr('data-model');
var settings = dataTableSettings[model];
table.dataTable(settings);
});
});
INIT_SCRIPT;
$this->scriptBlock($initScript, array('block' => $this->settings['scriptBlock']));
}
}
/**
* Sets label at the given index.
*
* @param int $index of column to change
* @param string $label new label to be set. `__LABEL__` string will be replaced by the original label
* @return bool true if set, false otherwise
*/
public function setLabel($model, $index, $label) {
$model = $this->_getModel($model);
$oldLabel = $this->_labels[$model][$index];
$oldOptions = $options = array();
if (is_array($oldLabel)) {
list($oldLabel, $oldOptions) = $oldLabel;
}
if (is_array($label)) {
list($label, $options) = $label;
}
$this->_labels[$model][$index] = array(
$this->_parseLabel($label, $oldLabel),
array_merge($oldOptions, $options),
);
return true;
}
/**
* Renders a DataTable
*
* Options take on the following values:
* - `class` For table. Default: `dataTable`
* - `trOptions` Array of options for tr
* - `thOptions` Array of options for th
* - `theadOptions` Array of options for thead
* - `tbody` Content for tbody
* - `tbodyOptions` Array of options for tbody
*
* The rest of the keys wil be passed as options for the table
*
* @param string $model Model to paginate
* @param array $options Options for table
* @param array $js Options for js var
* @return string
*/
public function render($model = null, $options = array(), $js = array()) {
$model = $this->_getModel($model);
$options = array_merge($this->settings['table'], $options);
$trOptions = $options['trOptions'];
$thOptions = $options['thOptions'];
unset($options['trOptions'], $options['thOptions']);
$theadOptions = $options['theadOptions'];
$tbodyOptions = $options['tbodyOptions'];
$tfootOptions = $options['tfootOptions'];
unset($options['theadOptions'], $options['tbodyOptions'], $options['tfootOptions']);
$tbody = $options['tbody'];
$tfoot = $options['tfoot'];
unset($options['tbody'], $options['tfoot']);
$tableHeaders = $this->tableHeaders($this->_labels[$model], $trOptions, $thOptions);
$tableHead = $this->tag('thead', $tableHeaders, $theadOptions);
$tableBody = $this->tag('tbody', $tbody, $tbodyOptions);
$tableFooter = $this->tag('tfoot', $tfoot, $tfootOptions);
$options['data-model'] = $model;
$table = $this->tag('table', $tableHead . $tableBody . $tableFooter, $options);
$this->jsSettings($model, $js);
return $table;
}
/**
* Renders table headers with column-specific attribute support
*
* @param $names
* @param null $trOptions
* @param null $thOptions
* @return string
*/
public function tableHeaders($names, $trOptions = null, $thOptions = null) {
$out = array();
foreach ($names as $name) {
$arg = $name;
$options = array();
if (is_array($name)) {
list($arg, $options) = $name;
}
$options = array_merge((array)$thOptions, $options);
$out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($options), $arg);
}
return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
}
/**
* Returns js settings either as an array or json-encoded string
*
* @param array $settings
* @param bool $encode
* @return array|string
*/
public function jsSettings($model, $settings = array(), $encode = false) {
$model = $this->_getModel($model);
$settings = array_merge($this->settings['js'], (array)$settings);
if (!empty($settings['bServerSide'])) {
if (!isset($settings['sAjaxSource']) || $settings['sAjaxSource'] === true) {
$settings['sAjaxSource'] = $this->request->here();
}
if (!is_string($settings['sAjaxSource'])) {
if (!isset($settings['sAjaxSource']['model'])) {
if (isset($settings['model'])) {
$model = $this->_getModel($settings['model']);
unset($settings['model']);
}
$settings['sAjaxSource']['?'] = array(
'model' => $model
);
}
$settings['sAjaxSource'] = Router::url($settings['sAjaxSource']);
}
}
if (isset($settings['aoColumns']) && $settings['aoColumns'] === true) {
$settings['aoColumns'] = $this->_dtColumns[$model];
}
$this->_dtSettings[$model] = $settings;
return ($encode) ? json_encode($settings) : $settings;
}
/**
* Parse settings
*
* @param string $model
* @param array $columns
* @return array
*/
protected function _parseSettings($model, $columns) {
foreach($columns as $field => $options) {
if ($options === null) {
$label = $field;
$options = array(
'bSearchable' => false,
'bSortable' => false,
);
} else {
$label = $options['label'];
unset($options['label']);
if (isset($options['bSearchable'])) {
$options['bSearchable'] = (boolean)$options['bSearchable'];
}
}
$this->_labels[$model][] = $this->_parseLabel($label);
$this->_dtColumns[$model][] = $options;
}
return $this->_dtColumns[$model];
}
/**
* Parse a label with its options
*
* @param $label
* @param string $oldLabel
* @return string
*/
protected function _parseLabel($label, $oldLabel = '') {
$replacements = array(
'__CHECKBOX__' => '<input type="checkbox" class="check-all">',
'__LABEL__' => $oldLabel,
);
foreach($replacements as $search => $replace) {
$label = str_replace($search, $replace, $label);
}
return $label;
}
/**
* Validates model is a paginated model
*
* @param $model
* @return string
*/
protected function _getModel($model) {
if ($model === null && !empty($this->request->params['models'])) {
$model = current(array_keys($this->request->params['models']));
}
if (!in_array($model, $this->_paginatedModels)) {
trigger_error("DataTableHelper: Model {$model} is not setup for pagination", E_USER_ERROR);
return null;
}
return $model;
}
}