Completed
Push — master ( b026d2...e5ac1d )
by WEBEWEB
01:39
created

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