Completed
Push — master ( 7e22e4...75d8b8 )
by WEBEWEB
01:46
created

AbstractDataTablesTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Argument\StringHelper;
19
use WBW\Library\Core\Exception\IO\FileNotFoundException;
20
21
/**
22
 * Abstract DataTables Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension
26
 * @abstract
27
 */
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) {
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 column.
161
     *
162
     * @param DataTablesColumn $dtColumn The column.
163
     * @return string Returns the rendered 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 footer.
191
     *
192
     * @param DataTablesWrapper $dtWrapper The wrapper.
193
     * @return string Returns the rendered 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 header.
216
     *
217
     * @param DataTablesWrapper $dtWrapper The wrapper.
218
     * @return string Returns the rendered 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