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\Library\Core\Utility\Argument\StringUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract DataTables Twig extension. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
23
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension |
24
|
|
|
* @abstract |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractDataTablesTwigExtension extends Twig_Extension { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* jQuery DataTables. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
const JQUERY_DATATABLES = <<< 'EOTXT' |
34
|
|
|
<script type="text/javascript"> |
35
|
|
|
$(document).ready(function () { |
36
|
|
|
var %var% = $("%selector%").DataTable({ |
37
|
|
|
ajax: { |
38
|
|
|
type: "%method%", |
39
|
|
|
url: "%url%" |
40
|
|
|
}, |
41
|
|
|
columns: %columns%, |
42
|
|
|
language: { |
43
|
|
|
url: "/bundles/jquerydatatables/datatables-i18n-1.10.16/%language%.json" |
44
|
|
|
}, |
45
|
|
|
order: %order%, |
46
|
|
|
processing: %processing%, |
47
|
|
|
serverSide: %serverSide% |
48
|
|
|
}); |
49
|
|
|
}); |
50
|
|
|
</script> |
51
|
|
|
EOTXT; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor. |
55
|
|
|
*/ |
56
|
|
|
protected function __construct() { |
57
|
|
|
// NOTHING TO DO. |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get a DataTables name. |
62
|
|
|
* |
63
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
64
|
|
|
* @return string Returns the DataTables name. |
65
|
|
|
*/ |
66
|
|
|
protected function getDataTablesName(DataTablesWrapper $dtWrapper) { |
67
|
|
|
return "dt" . preg_replace("/[^A-Za-z0-9]/", "", $dtWrapper->getName()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Displays a jQuery DataTables. |
72
|
|
|
* |
73
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
74
|
|
|
* @param string $selector The selector. |
75
|
|
|
* @param string $language The language. |
76
|
|
|
* @return string Returns the jQuery DataTables. |
77
|
|
|
*/ |
78
|
|
|
protected function jQueryDataTables(DataTablesWrapper $dtWrapper, $selector, $language) { |
79
|
|
|
|
80
|
|
|
// Initialize the parameters. |
81
|
|
|
$var = $this->getDataTablesName($dtWrapper); |
82
|
|
|
$method = $dtWrapper->getMethod(); |
83
|
|
|
$url = $dtWrapper->getUrl(); |
84
|
|
|
$columns = json_encode(array_values($dtWrapper->getColumns())); |
85
|
|
|
$orders = json_encode(array_values($dtWrapper->getOrder())); |
86
|
|
|
$processing = StringUtility::parseBoolean($dtWrapper->getProcessing()); |
87
|
|
|
$serverSide = StringUtility::parseBoolean($dtWrapper->getServerSide()); |
88
|
|
|
|
89
|
|
|
// |
90
|
|
|
$searches = ["%var%", "%selector%", "%method%", "%url%", "%columns%", "%language%", "%order%", "%processing%", "%serverSide%"]; |
91
|
|
|
$replaces = [$var, null === $selector ? "#" . $var : $selector, $method, $url, $columns, $language, $orders, $processing, $serverSide]; |
92
|
|
|
|
93
|
|
|
// Return the Javascript. |
94
|
|
|
return StringUtility::replace(self::JQUERY_DATATABLES, $searches, $replaces); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Render a DataTables. |
99
|
|
|
* |
100
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
101
|
|
|
* @param string $class The class. |
102
|
|
|
* @param boolean $includeTHead Include thead ?. |
103
|
|
|
* @param boolean $includeTFoot Include tfoot ? |
104
|
|
|
* @returns string Returns the rendered DataTables. |
105
|
|
|
*/ |
106
|
|
|
protected function renderDataTables(DataTablesWrapper $dtWrapper, $class, $includeTHead, $includeTFoot) { |
107
|
|
|
|
108
|
|
|
// Initialize the template. |
109
|
|
|
$template = "<table %attributes%>\n%innerHTML%</table>"; |
110
|
|
|
|
111
|
|
|
// Initialize the attributes. |
112
|
|
|
$attributes = []; |
113
|
|
|
|
114
|
|
|
$attributes["class"] = ["table", $class]; |
115
|
|
|
$attributes["id"] = $this->getDataTablesName($dtWrapper); |
116
|
|
|
|
117
|
|
|
// Initialize the parameters. |
118
|
|
|
$thead = true === $includeTHead ? $this->renderDataTablesTHead($dtWrapper) : ""; |
119
|
|
|
$tfoot = true === $includeTFoot ? $this->renderDataTablesTFoot($dtWrapper) : ""; |
120
|
|
|
|
121
|
|
|
// Return the HTML. |
122
|
|
|
return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $thead . $tfoot]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Render a DataTables column. |
127
|
|
|
* |
128
|
|
|
* @param DataTablesColumn $dtColumn The column. |
129
|
|
|
* @return string Returns the rendered DataTables column. |
130
|
|
|
*/ |
131
|
|
|
private function renderDataTablesColumn(DataTablesColumn $dtColumn, $scopeRow = false) { |
132
|
|
|
|
133
|
|
|
// Check if the column is visible. |
134
|
|
|
if (false === $dtColumn->getVisible()) { |
135
|
|
|
return ""; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Initialize the template. |
139
|
|
|
$template = " <th%attributes%>%innerHTML%</th>"; |
140
|
|
|
|
141
|
|
|
// Initialize the attributes. |
142
|
|
|
$attributes = []; |
143
|
|
|
|
144
|
|
|
$attributes["scope"] = true === $scopeRow ? "row" : null; |
145
|
|
|
$attributes["class"] = $dtColumn->getClassname(); |
146
|
|
|
$attributes["width"] = $dtColumn->getWidth(); |
147
|
|
|
|
148
|
|
|
// Initialize the parameters. |
149
|
|
|
$innerHTML = $dtColumn->getTitle(); |
150
|
|
|
|
151
|
|
|
// Return the HTML. |
152
|
|
|
return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [preg_replace("/^\ $/", "", " " . StringUtility::parseArray($attributes)), $innerHTML]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Render a DataTables footer. |
157
|
|
|
* |
158
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
159
|
|
|
* @return string Returns the rendered DataTables footer. |
160
|
|
|
*/ |
161
|
|
|
private function renderDataTablesTFoot(DataTablesWrapper $dtWrapper) { |
162
|
|
|
|
163
|
|
|
// Initialize the template. |
164
|
|
|
$template = " <tfoot>\n <tr>\n%innerHTML% </tr>\n </tfoot>\n"; |
165
|
|
|
|
166
|
|
|
// Initialize the parameters. |
167
|
|
|
$innerHTML = ""; |
168
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
169
|
|
|
$column = $this->renderDataTablesColumn($dtColumn); |
170
|
|
|
if ("" === $column) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
$innerHTML .= $column . "\n"; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Return the HTML. |
177
|
|
|
return "" === $innerHTML ? "" : StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Render a DataTables header. |
182
|
|
|
* |
183
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
184
|
|
|
* @return string Returns the rendered DataTables header. |
185
|
|
|
*/ |
186
|
|
|
private function renderDataTablesTHead(DataTablesWrapper $dtWrapper) { |
187
|
|
|
|
188
|
|
|
// Initialize the templates. |
189
|
|
|
$template = " <thead>\n <tr>\n%innerHTML% </tr>\n </thead>\n"; |
190
|
|
|
|
191
|
|
|
// Count the columns. |
192
|
|
|
$count = count($dtWrapper->getColumns()); |
193
|
|
|
|
194
|
|
|
// Initialize the parameters. |
195
|
|
|
$innerHTML = ""; |
196
|
|
|
for ($i = 0; $i < $count; ++$i) { |
197
|
|
|
$dtColumn = array_values($dtWrapper->getColumns())[$i]; |
198
|
|
|
$column = $this->renderDataTablesColumn($dtColumn, 0 === $i); |
199
|
|
|
if ("" === $column) { |
200
|
|
|
continue; |
201
|
|
|
} |
202
|
|
|
$innerHTML .= $column . "\n"; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Return the HTML. |
206
|
|
|
return "" === $innerHTML ? "" : StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|