Completed
Push — master ( edbdaf...4748dc )
by WEBEWEB
01:30
created

jQueryDataTables()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 3
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\BootstrapBundle\Twig\Extension\BootstrapRendererTwigExtension;
16
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesColumn;
17
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper;
18
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper;
19
use WBW\Library\Core\Argument\StringHelper;
20
use WBW\Library\Core\Exception\IO\FileNotFoundException;
21
22
/**
23
 * Abstract DataTables Twig extension.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension
27
 * @abstract
28
 */
29
abstract class AbstractDataTablesTwigExtension extends Twig_Extension {
30
31
    /**
32
     * jQuery DataTables.
33
     *
34
     * @var string
35
     */
36
    const JQUERY_DATATABLES = <<<'EOT'
37
    $(document).ready(function () {
38
        var %var% = $("%selector%").DataTable(%options%);
39
    });
40
EOT;
41
42
    /**
43
     * jQuery DataTables.
44
     *
45
     * @var string
46
     */
47
    const JQUERY_DATATABLES_STANDALONE = <<<'EOT'
48
    $(document).ready(function () {
49
        $("%selector%").DataTable(%options%);
50
    });
51
EOT;
52
53
    /**
54
     * Renderer
55
     *
56
     * @var BootstrapRendererTwigExtension
57
     */
58
    private $renderer;
59
60
    /**
61
     * Constructor.
62
     *
63
     * @param BootstrapRendererTwigExtension $renderer The renderer.
64
     */
65
    public function __construct(BootstrapRendererTwigExtension $renderer) {
66
        $this->setRenderer($renderer);
67
    }
68
69
    /**
70
     * Encode the options.
71
     *
72
     * @param array $options The options.
73
     * @return string Returns the encoded options.
74
     */
75
    protected function encodeOptions(array $options) {
76
        if (0 === count($options)) {
77
            return "{}";
78
        }
79
        ksort($options);
80
        $output = json_encode($options, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
81
        return str_replace("\n", "\n        ", $output);
82
    }
83
84
    /**
85
     * Get the renderer.
86
     *
87
     * @return BootstrapRendererTwigExtension Returns the renderer.
88
     */
89
    public function getRenderer() {
90
        return $this->renderer;
91
    }
92
93
    /**
94
     * Displays a jQuery DataTables.
95
     *
96
     * @param DataTablesWrapper $dtWrapper The wrapper.
97
     * @param string $selector The selector.
98
     * @param string $language The language.
99
     * @return string Returns the jQuery DataTables.
100
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
101
     */
102
    protected function jQueryDataTables(DataTablesWrapper $dtWrapper, $selector, $language) {
103
104
        // Get the options.
105
        $dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper);
106
107
        // Initialize the parameters.
108
        $var     = DataTablesWrapperHelper::getName($dtWrapper);
109
        $options = $dtOptions;
110
        if (null !== $language) {
111
            $options["language"] = ["url" => DataTablesWrapperHelper::getLanguageURL($language)];
112
        }
113
114
        //
115
        $searches = ["%var%", "%selector%", "%options%"];
116
        $replaces = [$var, null === $selector ? "#" . $var : $selector, $this->encodeOptions($options)];
117
118
        // Return the Javascript.
119
        $javascript = StringHelper::replace(self::JQUERY_DATATABLES, $searches, $replaces);
120
        return $this->getRenderer()->bootstrapScriptFilter($javascript);
121
    }
122
123
    /**
124
     * Displays a jQuery DataTables "Standalone".
125
     *
126
     * @param string $selector The selector.
127
     * @param string $language The language.
128
     * @param array $options The options.
129
     * @return string Returns the jQuery DataTables "Standalone".
130
     * @throws FileNotFoundException Throws a file not found exception if the language file does not exist.
131
     */
132
    protected function jQueryDataTablesStandalone($selector, $language, array $options) {
133
134
        // Initialize the language.
135
        if (null !== $language) {
136
            $options["language"] = ["url" => DataTablesWrapperHelper::getLanguageURL($language)];
137
        }
138
139
        //
140
        $searches = ["%selector%", "%options%"];
141
        $replaces = [$selector, $this->encodeOptions($options)];
142
143
        // Return the Javascript.
144
        $javascript = StringHelper::replace(self::JQUERY_DATATABLES_STANDALONE, $searches, $replaces);
145
        return $this->getRenderer()->bootstrapScriptFilter($javascript);
146
    }
147
148
    /**
149
     * Render a DataTables.
150
     *
151
     * @param DataTablesWrapper $dtWrapper The wrapper.
152
     * @param string $class The class.
153
     * @param boolean $includeTHead Include thead ?.
154
     * @param boolean $includeTFoot Include tfoot ?
155
     * @returns string Returns the rendered DataTables.
156
     */
157
    protected function renderDataTables(DataTablesWrapper $dtWrapper, $class, $includeTHead, $includeTFoot) {
158
159
        // Initialize the template.
160
        $template = "<table %attributes%>\n%innerHTML%</table>";
161
162
        // Initialize the attributes.
163
        $attributes = [];
164
165
        $attributes["class"] = ["table", $class];
166
        $attributes["id"]    = DataTablesWrapperHelper::getName($dtWrapper);
167
168
        // Initialize the parameters.
169
        $thead = true === $includeTHead ? $this->renderDataTablesTHead($dtWrapper) : "";
170
        $tfoot = true === $includeTFoot ? $this->renderDataTablesTFoot($dtWrapper) : "";
171
172
        // Return the HTML.
173
        return StringHelper::replace($template, ["%attributes%", "%innerHTML%"], [StringHelper::parseArray($attributes), $thead . $tfoot]);
174
    }
175
176
    /**
177
     * Render a column.
178
     *
179
     * @param DataTablesColumn $dtColumn The column.
180
     * @return string Returns the rendered column.
181
     */
182
    private function renderDataTablesColumn(DataTablesColumn $dtColumn, $scopeRow = false) {
183
184
        // Check if the column is visible.
185
        if (false === $dtColumn->getVisible()) {
186
            return "";
187
        }
188
189
        // Initialize the template.
190
        $template = "            <th%attributes%>%innerHTML%</th>";
191
192
        // Initialize the attributes.
193
        $attributes = [];
194
195
        $attributes["scope"] = true === $scopeRow ? "row" : null;
196
        $attributes["class"] = $dtColumn->getClassname();
197
        $attributes["width"] = $dtColumn->getWidth();
198
199
        // Initialize the parameters.
200
        $innerHTML = $dtColumn->getTitle();
201
202
        // Return the HTML.
203
        return StringHelper::replace($template, ["%attributes%", "%innerHTML%"], [preg_replace("/^\ $/", "", " " . StringHelper::parseArray($attributes)), $innerHTML]);
204
    }
205
206
    /**
207
     * Render a footer.
208
     *
209
     * @param DataTablesWrapper $dtWrapper The wrapper.
210
     * @return string Returns the rendered footer.
211
     */
212
    private function renderDataTablesTFoot(DataTablesWrapper $dtWrapper) {
213
214
        // Initialize the template.
215
        $template = "    <tfoot>\n        <tr>\n%innerHTML%        </tr>\n    </tfoot>\n";
216
217
        // Initialize the parameters.
218
        $innerHTML = "";
219
        foreach ($dtWrapper->getColumns() as $dtColumn) {
220
            $column = $this->renderDataTablesColumn($dtColumn);
221
            if ("" === $column) {
222
                continue;
223
            }
224
            $innerHTML .= $column . "\n";
225
        }
226
227
        // Return the HTML.
228
        return "" === $innerHTML ? "" : StringHelper::replace($template, ["%innerHTML%"], [$innerHTML]);
229
    }
230
231
    /**
232
     * Render a header.
233
     *
234
     * @param DataTablesWrapper $dtWrapper The wrapper.
235
     * @return string Returns the rendered header.
236
     */
237
    private function renderDataTablesTHead(DataTablesWrapper $dtWrapper) {
238
239
        // Initialize the templates.
240
        $template = "    <thead>\n        <tr>\n%innerHTML%        </tr>\n    </thead>\n";
241
242
        // Count the columns.
243
        $count = count($dtWrapper->getColumns());
244
245
        // Initialize the parameters.
246
        $innerHTML = "";
247
        for ($i = 0; $i < $count; ++$i) {
248
            $dtColumn = array_values($dtWrapper->getColumns())[$i];
249
            $column   = $this->renderDataTablesColumn($dtColumn, 0 === $i);
250
            if ("" === $column) {
251
                continue;
252
            }
253
            $innerHTML .= $column . "\n";
254
        }
255
256
        // Return the HTML.
257
        return "" === $innerHTML ? "" : StringHelper::replace($template, ["%innerHTML%"], [$innerHTML]);
258
    }
259
260
    /**
261
     * Set the renderer.
262
     *
263
     * @param BootstrapRendererTwigExtension $renderer The renderer.
264
     * @return AbstractDataTablesTwigExtension Returns this Twig extension.
265
     */
266
    protected function setRenderer(BootstrapRendererTwigExtension $renderer) {
267
        $this->renderer = $renderer;
268
        return $this;
269
    }
270
271
}
272