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
|
|
|
* Constructor. |
30
|
|
|
*/ |
31
|
|
|
public function __construct() { |
32
|
|
|
// NOTHING TO DO. |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Displays a DataTables column. |
37
|
|
|
* |
38
|
|
|
* @param DataTablesColumn $dtColumn The column. |
39
|
|
|
* @return string Returns the DataTables column. |
40
|
|
|
*/ |
41
|
|
|
private function dataTablesColumn(DataTablesColumn $dtColumn, $scopeRow = false) { |
42
|
|
|
|
43
|
|
|
// Check if the column is visible. |
44
|
|
|
if (false === $dtColumn->getVisible()) { |
45
|
|
|
return ""; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Initialize the template. |
49
|
|
|
$template = " <th%attributes%>%innerHTML%</th>"; |
50
|
|
|
|
51
|
|
|
// Initialize the attributes. |
52
|
|
|
$attributes = []; |
53
|
|
|
|
54
|
|
|
$attributes["scope"] = true === $scopeRow ? "row" : null; |
55
|
|
|
$attributes["class"] = $dtColumn->getClassname(); |
56
|
|
|
$attributes["width"] = $dtColumn->getWidth(); |
57
|
|
|
|
58
|
|
|
// Initialize the parameters. |
59
|
|
|
$innerHTML = $dtColumn->getTitle(); |
60
|
|
|
|
61
|
|
|
// Return the HTML. |
62
|
|
|
return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [preg_replace("/^\ $/", "", " " . StringUtility::parseArray($attributes)), $innerHTML]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Displays a DataTables name. |
67
|
|
|
* |
68
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
69
|
|
|
* @return string Returns the DataTables name. |
70
|
|
|
*/ |
71
|
|
|
protected function dataTablesName(DataTablesWrapper $dtWrapper) { |
72
|
|
|
return "dt" . preg_replace("/[^A-Za-z0-9]/", "", $dtWrapper->getName()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Displays a DataTables footer. |
77
|
|
|
* |
78
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
79
|
|
|
* @return string Returns the DataTables footer. |
80
|
|
|
*/ |
81
|
|
|
private function dataTablesTFoot(DataTablesWrapper $dtWrapper) { |
82
|
|
|
|
83
|
|
|
// Initialize the template. |
84
|
|
|
$template = " <tfoot>\n <tr>\n%innerHTML% </tr>\n </tfoot>\n"; |
85
|
|
|
|
86
|
|
|
// Initialize the parameters. |
87
|
|
|
$innerHTML = ""; |
88
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
89
|
|
|
$column = $this->dataTablesColumn($dtColumn); |
90
|
|
|
if ("" === $column) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
$innerHTML .= $column . "\n"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Return the HTML. |
97
|
|
|
return "" === $innerHTML ? "" : StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Displays a DataTables header. |
102
|
|
|
* |
103
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
104
|
|
|
* @return string Returns the DataTables header. |
105
|
|
|
*/ |
106
|
|
|
private function dataTablesTHead(DataTablesWrapper $dtWrapper) { |
107
|
|
|
|
108
|
|
|
// Initialize the templates. |
109
|
|
|
$template = " <thead>\n <tr>\n%innerHTML% </tr>\n </thead>\n"; |
110
|
|
|
|
111
|
|
|
// Count the columns. |
112
|
|
|
$count = count($dtWrapper->getColumns()); |
113
|
|
|
|
114
|
|
|
// Initialize the parameters. |
115
|
|
|
$innerHTML = ""; |
116
|
|
|
for ($i = 0; $i < $count; ++$i) { |
117
|
|
|
$dtColumn = array_values($dtWrapper->getColumns())[$i]; |
118
|
|
|
$column = $this->dataTablesColumn($dtColumn, 0 === $i); |
119
|
|
|
if ("" === $column) { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
$innerHTML .= $column . "\n"; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Return the HTML. |
126
|
|
|
return "" === $innerHTML ? "" : StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Displays a DataTables table. |
131
|
|
|
* |
132
|
|
|
* @param DataTablesWrapper $dtWrapper The wrapper. |
133
|
|
|
* @param string $class The class. |
134
|
|
|
* @param boolean $includeTHead Include thead ?. |
135
|
|
|
* @param boolean $includeTFoot Include tfoot ? |
136
|
|
|
* @returns string Returns the DataTables table. |
137
|
|
|
*/ |
138
|
|
|
protected function dataTablesTable(DataTablesWrapper $dtWrapper, $class, $includeTHead, $includeTFoot) { |
139
|
|
|
|
140
|
|
|
// Initialize the template. |
141
|
|
|
$template = "<table %attributes%>\n%innerHTML%</table>"; |
142
|
|
|
|
143
|
|
|
// Initialize the attributes. |
144
|
|
|
$attributes = []; |
145
|
|
|
|
146
|
|
|
$attributes["class"] = ["table", $class]; |
147
|
|
|
$attributes["id"] = $this->dataTablesName($dtWrapper); |
148
|
|
|
|
149
|
|
|
// Initialize the parameters. |
150
|
|
|
$thead = true === $includeTHead ? $this->dataTablesTHead($dtWrapper) : ""; |
151
|
|
|
$tfoot = true === $includeTFoot ? $this->dataTablesTFoot($dtWrapper) : ""; |
152
|
|
|
|
153
|
|
|
// Return the HTML. |
154
|
|
|
return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $thead . $tfoot]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|