1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension; |
13
|
|
|
|
14
|
|
|
use Twig_Extension; |
15
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesColumn; |
16
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
17
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper; |
18
|
|
|
use WBW\Library\Core\Helper\Argument\StringHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract DataTables Twig extension. |
22
|
|
|
* |
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
24
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension |
25
|
|
|
* @abstract |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractDataTablesTwigExtension extends Twig_Extension { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* jQuery DataTables. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const JQUERY_DATATABLES = <<< 'EOTXT' |
35
|
|
|
<script type="text/javascript"> |
36
|
|
|
$(document).ready(function () { |
37
|
|
|
var %var% = $("%selector%").DataTable({ |
38
|
|
|
ajax: { |
39
|
|
|
type: "%method%", |
40
|
|
|
url: "%url%" |
41
|
|
|
}, |
42
|
|
|
columns: %columns%, |
43
|
|
|
language: { |
44
|
|
|
url: "/bundles/jquerydatatables/datatables-i18n-1.10.16/%language%.json" |
45
|
|
|
}, |
46
|
|
|
order: %order%, |
47
|
|
|
processing: %processing%, |
48
|
|
|
serverSide: %serverSide% |
49
|
|
|
}); |
50
|
|
|
}); |
51
|
|
|
</script> |
52
|
|
|
EOTXT; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* jQuery DataTables. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
const JQUERY_DATATABLES_STANDALONE = <<< 'EOTXT' |
60
|
|
|
<script type="text/javascript"> |
61
|
|
|
$(document).ready(function () { |
62
|
|
|
$("%selector%").DataTable(%options%); |
63
|
|
|
}); |
64
|
|
|
</script> |
65
|
|
|
EOTXT; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Constructor. |
69
|
|
|
*/ |
70
|
|
|
protected function __construct() { |
71
|
|
|
// NOTHING TO DO. |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Displays a jQuery DataTables. |
76
|
|
|
* |
77
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
78
|
|
|
* @param string $selector The selector. |
79
|
|
|
* @param string $language The language. |
80
|
|
|
* @return string Returns the jQuery DataTables. |
81
|
|
|
*/ |
82
|
|
|
protected function jQueryDataTables(DataTablesWrapper $dtWrapper, $selector, $language) { |
83
|
|
|
|
84
|
|
|
// Get the options. |
85
|
|
|
$dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper); |
86
|
|
|
|
87
|
|
|
// Initialize the parameters. |
88
|
|
|
$var = DataTablesWrapperHelper::getName($dtWrapper); |
89
|
|
|
$method = $dtOptions["ajax"]["method"]; |
90
|
|
|
$url = $dtOptions["ajax"]["url"]; |
91
|
|
|
$columns = json_encode($dtOptions["columns"]); |
92
|
|
|
$orders = json_encode($dtOptions["order"]); |
93
|
|
|
$processing = $dtOptions["processing"]; |
94
|
|
|
$serverSide = $dtOptions["serverSide"]; |
95
|
|
|
|
96
|
|
|
// |
97
|
|
|
$searches = ["%var%", "%selector%", "%method%", "%url%", "%columns%", "%language%", "%order%", "%processing%", "%serverSide%"]; |
98
|
|
|
$replaces = [$var, null === $selector ? "#" . $var : $selector, $method, $url, $columns, $language, $orders, $processing, $serverSide]; |
99
|
|
|
|
100
|
|
|
// Return the Javascript. |
101
|
|
|
return StringHelper::replace(self::JQUERY_DATATABLES, $searches, $replaces); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Displays a jQuery DataTables "Standalone". |
106
|
|
|
* |
107
|
|
|
* @param string $selector The selector. |
108
|
|
|
* @param string $language The language. |
109
|
|
|
* @param array $options The options. |
110
|
|
|
* @return string Returns the jQuery DataTables "Standalone". |
111
|
|
|
*/ |
112
|
|
|
protected function jQueryDataTablesStandalone($selector, $language, array $options) { |
113
|
|
|
|
114
|
|
|
// Initialize the language. |
115
|
|
|
$options["language"] = ["url" => "/bundles/jquerydatatables/datatables-i18n-1.10.16/" . $language . ".json"]; |
116
|
|
|
|
117
|
|
|
// Sort the options. |
118
|
|
|
ksort($options); |
119
|
|
|
|
120
|
|
|
// |
121
|
|
|
$searches = ["%selector%", "%options%"]; |
122
|
|
|
$replaces = [$selector, json_encode($options, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)]; |
123
|
|
|
|
124
|
|
|
// Re-indent. |
125
|
|
|
$replaces[1] = str_replace("\n", "\n ", $replaces[1]); |
126
|
|
|
|
127
|
|
|
// Return the Javascript. |
128
|
|
|
return StringHelper::replace(self::JQUERY_DATATABLES_STANDALONE, $searches, $replaces); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Render a DataTables. |
133
|
|
|
* |
134
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
135
|
|
|
* @param string $class The class. |
136
|
|
|
* @param boolean $includeTHead Include thead ?. |
137
|
|
|
* @param boolean $includeTFoot Include tfoot ? |
138
|
|
|
* @returns string Returns the rendered DataTables. |
139
|
|
|
*/ |
140
|
|
|
protected function renderDataTables(DataTablesWrapper $dtWrapper, $class, $includeTHead, $includeTFoot) { |
141
|
|
|
|
142
|
|
|
// Initialize the template. |
143
|
|
|
$template = "<table %attributes%>\n%innerHTML%</table>"; |
144
|
|
|
|
145
|
|
|
// Initialize the attributes. |
146
|
|
|
$attributes = []; |
147
|
|
|
|
148
|
|
|
$attributes["class"] = ["table", $class]; |
149
|
|
|
$attributes["id"] = DataTablesWrapperHelper::getName($dtWrapper); |
150
|
|
|
|
151
|
|
|
// Initialize the parameters. |
152
|
|
|
$thead = true === $includeTHead ? $this->renderDataTablesTHead($dtWrapper) : ""; |
153
|
|
|
$tfoot = true === $includeTFoot ? $this->renderDataTablesTFoot($dtWrapper) : ""; |
154
|
|
|
|
155
|
|
|
// Return the HTML. |
156
|
|
|
return StringHelper::replace($template, ["%attributes%", "%innerHTML%"], [StringHelper::parseArray($attributes), $thead . $tfoot]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Render a DataTables column. |
161
|
|
|
* |
162
|
|
|
* @param DataTablesColumn $dtColumn The column. |
163
|
|
|
* @return string Returns the rendered DataTables column. |
164
|
|
|
*/ |
165
|
|
|
private function renderDataTablesColumn(DataTablesColumn $dtColumn, $scopeRow = false) { |
166
|
|
|
|
167
|
|
|
// Check if the column is visible. |
168
|
|
|
if (false === $dtColumn->getVisible()) { |
169
|
|
|
return ""; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Initialize the template. |
173
|
|
|
$template = " <th%attributes%>%innerHTML%</th>"; |
174
|
|
|
|
175
|
|
|
// Initialize the attributes. |
176
|
|
|
$attributes = []; |
177
|
|
|
|
178
|
|
|
$attributes["scope"] = true === $scopeRow ? "row" : null; |
179
|
|
|
$attributes["class"] = $dtColumn->getClassname(); |
180
|
|
|
$attributes["width"] = $dtColumn->getWidth(); |
181
|
|
|
|
182
|
|
|
// Initialize the parameters. |
183
|
|
|
$innerHTML = $dtColumn->getTitle(); |
184
|
|
|
|
185
|
|
|
// Return the HTML. |
186
|
|
|
return StringHelper::replace($template, ["%attributes%", "%innerHTML%"], [preg_replace("/^\ $/", "", " " . StringHelper::parseArray($attributes)), $innerHTML]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Render a DataTables footer. |
191
|
|
|
* |
192
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
193
|
|
|
* @return string Returns the rendered DataTables footer. |
194
|
|
|
*/ |
195
|
|
|
private function renderDataTablesTFoot(DataTablesWrapper $dtWrapper) { |
196
|
|
|
|
197
|
|
|
// Initialize the template. |
198
|
|
|
$template = " <tfoot>\n <tr>\n%innerHTML% </tr>\n </tfoot>\n"; |
199
|
|
|
|
200
|
|
|
// Initialize the parameters. |
201
|
|
|
$innerHTML = ""; |
202
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
203
|
|
|
$column = $this->renderDataTablesColumn($dtColumn); |
204
|
|
|
if ("" === $column) { |
205
|
|
|
continue; |
206
|
|
|
} |
207
|
|
|
$innerHTML .= $column . "\n"; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Return the HTML. |
211
|
|
|
return "" === $innerHTML ? "" : StringHelper::replace($template, ["%innerHTML%"], [$innerHTML]); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Render a DataTables header. |
216
|
|
|
* |
217
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
218
|
|
|
* @return string Returns the rendered DataTables header. |
219
|
|
|
*/ |
220
|
|
|
private function renderDataTablesTHead(DataTablesWrapper $dtWrapper) { |
221
|
|
|
|
222
|
|
|
// Initialize the templates. |
223
|
|
|
$template = " <thead>\n <tr>\n%innerHTML% </tr>\n </thead>\n"; |
224
|
|
|
|
225
|
|
|
// Count the columns. |
226
|
|
|
$count = count($dtWrapper->getColumns()); |
227
|
|
|
|
228
|
|
|
// Initialize the parameters. |
229
|
|
|
$innerHTML = ""; |
230
|
|
|
for ($i = 0; $i < $count; ++$i) { |
231
|
|
|
$dtColumn = array_values($dtWrapper->getColumns())[$i]; |
232
|
|
|
$column = $this->renderDataTablesColumn($dtColumn, 0 === $i); |
233
|
|
|
if ("" === $column) { |
234
|
|
|
continue; |
235
|
|
|
} |
236
|
|
|
$innerHTML .= $column . "\n"; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// Return the HTML. |
240
|
|
|
return "" === $innerHTML ? "" : StringHelper::replace($template, ["%innerHTML%"], [$innerHTML]); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|