Completed
Push — master ( 064006...20444a )
by WEBEWEB
04:40
created

AbstractDataTablesTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 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\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, $rowScope = 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["row"]   = true === $rowScope ? "scope" : 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 footer.
67
     *
68
     * @param DataTablesWrapper $dtWrapper The wrapper.
69
     * @return string Returns the DataTables footer.
70
     */
71
    private function dataTablesTFoot(DataTablesWrapper $dtWrapper) {
72
73
        // Initialize the template.
74
        $template = "<tfoot>\n<tr>\n%innerHTML%</tr>\n</tfoot>\n";
75
76
        // Initialize the parameters.
77
        $innerHTML = "";
78
        foreach ($dtWrapper->getColumns() as $dtColumn) {
79
            $column = $this->dataTablesColumn($dtColumn);
80
            if ("" === $column) {
81
                continue;
82
            }
83
            $innerHTML .= $column . "\n";
84
        }
85
86
        // Return the HTML.
87
        return "" === $innerHTML ? "" : StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]);
88
    }
89
90
    /**
91
     * Displays a DataTables header.
92
     *
93
     * @param DataTablesWrapper $dtWrapper The wrapper.
94
     * @return string Returns the DataTables header.
95
     */
96
    private function dataTablesTHead(DataTablesWrapper $dtWrapper) {
97
98
        // Initialize the templates.
99
        $template = "<thead>\n<tr>\n%innerHTML%</tr>\n</thead>\n";
100
101
        // Count the columns.
102
        $count = count($dtWrapper->getColumns());
103
104
        // Initialize the parameters.
105
        $innerHTML = "";
106
        for ($i = 0; $i < $count; ++$i) {
107
            $dtColumn = array_values($dtWrapper->getColumns())[$i];
108
            $column   = $this->dataTablesColumn($dtColumn, 0 === $i);
109
            if ("" === $column) {
110
                continue;
111
            }
112
            $innerHTML .= $column . "\n";
113
        }
114
115
        // Return the HTML.
116
        return "" === $innerHTML ? "" : StringUtility::replace($template, ["%innerHTML%"], [$innerHTML]);
117
    }
118
119
    /**
120
     * Displays a DataTables table.
121
     *
122
     * @param DataTablesWrapper $dtWrapper The wrapper.
123
     * @param string $class The class.
124
     * @param boolean $includeTHead Include thead ?.
125
     * @param boolean $includeTFoot Include tfoot ?
126
     * @returns string Returns the DataTables table.
127
     */
128
    protected function dataTablesTable(DataTablesWrapper $dtWrapper, $class, $includeTHead, $includeTFoot) {
129
130
        // Initialize the template.
131
        $template = "<table %attributes%>\n%innerHTML%</table>";
132
133
        // Initialize the attributes.
134
        $attributes = [];
135
136
        $attributes["class"] = ["table", $class];
137
138
        // Initialize the parameters.
139
        $thead = true === $includeTHead ? $this->dataTablesTHead($dtWrapper) : "";
140
        $tfoot = true === $includeTFoot ? $this->dataTablesTFoot($dtWrapper) : "";
141
142
        // Return the HTML.
143
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $thead . $tfoot]);
144
    }
145
146
}
147