Passed
Branchmaster (991622)
by Plamen
01:31
created

thead::ths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 5
b 0
f 0
nc 3
nop 0
dl 0
loc 18
rs 9.9332
1
<?php
2
3
class thead extends tbody
4
{
5
    /** @param array Attributes for the table Html tag */
6
    public static $attributes = [];
7
    /** @param null|str Extension (from $_SERVER['REQUEST_URI']) */
8
    public static $pageExt;
9
10
    protected static function load()
11
    {
12
        $items = self::$t['items'];
13
        $v = ['items' => $items];
14
15
        if (self::config('FILTER_ACTIVE')) {
16
            self::filterValues($v['f_value'], $v['f_options']);
17
        }
18
19
        $v['div_attributes'] = self::tagAttributes('div', $items);
20
21
        $v['table_attributes'] = self::tagAttributes('table', $items);
22
23
        $v['ths'] = self::$cols ? self::ths() : null;
24
25
        $v['body'] = self::rowsTbody();
26
27
        $v['footer'] = self::rowsTfoot();
28
29
        return self::view('views/table.html', $v);
30
    }
31
32
    protected static function filterValues(&$f, &$opts = [])
33
    {
34
        $f = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?: null;
35
36
        $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
37
        foreach (self::$cols as $k => $v) {
38
            if (isset($v[2]['sort']) && $v[2]['sort'] === false) {
39
                continue;
40
            }
41
            if (empty($v)) {
42
                $v = [null];
43
            } // fix for column requested as []
44
            $selected = $by === $k ? ' selected' : null;
45
            $opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>";
46
        }
47
    }
48
49
    private static function tagAttributes($tag, $items)
50
    {
51
        $attr = $tag==='div' ?
52
                ['id' => $items . '-list', 'class' => 'table'] : 
53
                ['id' => $items . '-table', 'data-table' => 'js'];
54
        if (array_key_exists($tag, self::$attributes)) {
55
            $attr += self::$attributes[$tag];
56
        }
57
        if (isset(self::$attributes[$tag]['class'])) {
58
            $attr['class'] .= ' ' . self::$attributes[$tag]['class'];
59
        }
60
        return self::attributes($attr);
61
    }
62
63
    protected static function ths()
64
    {
65
        $ths = [];
66
        $length = sizeof(self::$cols);
67
        for ($i = 0; $i < $length; $i++) {
68
            list($lbl, $col, $arg) = array_pad(self::$cols[$i], 3, null);
69
70
            if (is_null($col)) {
71
                $arg['sort'] = false;
72
            }
73
            
74
            $sort = $del = null;
75
            
76
            $attr = self::thAttributes($col, $arg, $sort, $del);
77
78
            $ths[] = self::th($i, $attr, $sort, $del, $lbl);
79
        }
80
        return implode('', $ths);
81
    }
82
83
    private static function thAttributes($col, $arg, &$sort, &$del)
84
    {
85
        $sortable = !isset($arg['sort']) || $arg['sort'] !== false;
86
87
        if (isset($arg['width'])) { // Width attribute -> style
88
            $width = 'width:' . $arg['width'] . ';';
89
            $arg['style'] = isset($arg['style']) ?
90
                    $width . $arg['style'] :
91
                    $width;
92
        }
93
94
        if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
95
            $sortable = false;
96
            if (false === strpos($arg['style'], 'width:')) {
97
                $arg['width'] = '1%';
98
            }
99
        }
100
101
        $sort = $sortable ?
102
                (isset($arg['sort']) ? $arg['sort'] : $col) :
103
                null;
104
105
        return array_diff_key((array) $arg, ['sort', 'type', 'width']);
106
    }
107
108
    private static function th($i, $attr, $sort, $del, $lbl)
109
    {
110
        $th = '<th' . self::attributes($attr) . '>';
111
        if ($sort) {
112
            $th .= '<a onclick="table.Sort(' . $i . ',this);">';
113
        }
114
        if (!$del) {
115
            if ($sort == self::$t['order']['col']) {
116
                $span = self::$t['order']['dir'] === 'desc' ?
117
                        self::config('UTF8_DESC_SYMBOL') :
118
                        self::config('UTF8_ASC_SYMBOL');
119
            } else {
120
                $span = "";
121
            }
122
            $th .= '<span>' . $span . '</span>' . $lbl;
123
        } else {
124
            $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
125
                    ' onclick=\"checkAllDeleteCheckboxes(this,' .
126
                    ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
127
        }
128
        if ($sort) {
129
            $th .= '</a>';
130
        }
131
        $th .= '</th>';
132
        return $th;
133
    }
134
135
    protected static function reset($tableId)
136
    {
137
        if (!in_array($tableId, self::$t["tables"])) {
138
            self::$t["tables"][] = $tableId;
139
            self::$cols = [];
140
            self::$t['rows'] = null;
141
        } else {
142
            echo 'Existing table-id used in table::create(): ' . $tableId;
143
        }
144
    }
145
}
146