1 | <?php |
||
28 | abstract class AbstractDataTablesTwigExtension extends Twig_Extension { |
||
29 | |||
30 | /** |
||
31 | * jQuery DataTables. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | const JQUERY_DATATABLES = <<< 'EOTXT' |
||
36 | <script type="text/javascript"> |
||
37 | $(document).ready(function () { |
||
38 | var %var% = $("%selector%").DataTable(%options%); |
||
39 | }); |
||
40 | </script> |
||
41 | EOTXT; |
||
42 | |||
43 | /** |
||
44 | * jQuery DataTables. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | const JQUERY_DATATABLES_STANDALONE = <<< 'EOTXT' |
||
49 | <script type="text/javascript"> |
||
50 | $(document).ready(function () { |
||
51 | $("%selector%").DataTable(%options%); |
||
52 | }); |
||
53 | </script> |
||
54 | EOTXT; |
||
55 | |||
56 | /** |
||
57 | * Constructor. |
||
58 | */ |
||
59 | protected function __construct() { |
||
60 | // NOTHING TO DO. |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Encode the options. |
||
65 | * |
||
66 | * @param array $options The options. |
||
67 | * @return string Returns the encoded options. |
||
68 | */ |
||
69 | protected function encodeOptions(array $options) { |
||
70 | if (0 === count($options)) { |
||
71 | return "{}"; |
||
72 | } |
||
73 | ksort($options); |
||
74 | $output = json_encode($options, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
||
75 | return str_replace("\n", "\n ", $output); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Displays a jQuery DataTables. |
||
80 | * |
||
81 | * @param DataTablesWrapper $dtWrapper The wrapper. |
||
82 | * @param string $selector The selector. |
||
83 | * @param string $language The language. |
||
84 | * @return string Returns the jQuery DataTables. |
||
85 | * @throws FileNotFoundException Throws a file not found exception if the language file does not exist. |
||
86 | */ |
||
87 | protected function jQueryDataTables(DataTablesWrapper $dtWrapper, $selector, $language) { |
||
88 | |||
89 | // Get the options. |
||
90 | $dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper); |
||
91 | |||
92 | // Initialize the parameters. |
||
93 | $var = DataTablesWrapperHelper::getName($dtWrapper); |
||
94 | $options = $dtOptions; |
||
95 | if (null !== $language) { |
||
96 | $options["language"] = ["url" => DataTablesWrapperHelper::getLanguageURL($language)]; |
||
97 | } |
||
98 | |||
99 | // |
||
100 | $searches = ["%var%", "%selector%", "%options%"]; |
||
101 | $replaces = [$var, null === $selector ? "#" . $var : $selector, $this->encodeOptions($options)]; |
||
102 | |||
103 | // Return the Javascript. |
||
104 | return StringHelper::replace(self::JQUERY_DATATABLES, $searches, $replaces); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Displays a jQuery DataTables "Standalone". |
||
109 | * |
||
110 | * @param string $selector The selector. |
||
111 | * @param string $language The language. |
||
112 | * @param array $options The options. |
||
113 | * @return string Returns the jQuery DataTables "Standalone". |
||
114 | * @throws FileNotFoundException Throws a file not found exception if the language file does not exist. |
||
115 | */ |
||
116 | protected function jQueryDataTablesStandalone($selector, $language, array $options) { |
||
117 | |||
118 | // Initialize the language. |
||
119 | if (null !== $language) { |
||
120 | $options["language"] = ["url" => DataTablesWrapperHelper::getLanguageURL($language)]; |
||
121 | } |
||
122 | |||
123 | // |
||
124 | $searches = ["%selector%", "%options%"]; |
||
125 | $replaces = [$selector, $this->encodeOptions($options)]; |
||
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) { |
||
158 | |||
159 | /** |
||
160 | * Render a column. |
||
161 | * |
||
162 | * @param DataTablesColumn $dtColumn The column. |
||
163 | * @return string Returns the rendered column. |
||
164 | */ |
||
165 | private function renderDataTablesColumn(DataTablesColumn $dtColumn, $scopeRow = false) { |
||
188 | |||
189 | /** |
||
190 | * Render a footer. |
||
191 | * |
||
192 | * @param DataTablesWrapper $dtWrapper The wrapper. |
||
193 | * @return string Returns the rendered footer. |
||
194 | */ |
||
195 | private function renderDataTablesTFoot(DataTablesWrapper $dtWrapper) { |
||
213 | |||
214 | /** |
||
215 | * Render a header. |
||
216 | * |
||
217 | * @param DataTablesWrapper $dtWrapper The wrapper. |
||
218 | * @return string Returns the rendered header. |
||
219 | */ |
||
220 | private function renderDataTablesTHead(DataTablesWrapper $dtWrapper) { |
||
242 | |||
243 | } |
||
244 |