Passed
Branchmaster (485dbb)
by Plamen
01:33
created

thead::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
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
            $selected = $by === $k ? ' selected' : null;
42
            $opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>";
43
        }
44
    }
45
46
    private static function tagAttributes($tag, $items)
47
    {
48
        $attr = $tag === 'div' ?
49
                ['id' => $items . '-list', 'class' => 'table'] :
50
                ['id' => $items . '-table', 'data-table' => 'js'];
51
        if (array_key_exists($tag, self::$attributes)) {
52
            $attr += self::$attributes[$tag];
53
        }
54
        if (isset(self::$attributes[$tag]['class'])) {
55
            $attr['class'] .= ' ' . self::$attributes[$tag]['class'];
56
        }
57
        return self::attributes($attr);
58
    }
59
60
    protected static function ths()
61
    {
62
        $ths = [];
63
        $length = sizeof(self::$cols);
64
        for ($i = 0; $i < $length; $i++) {
65
            list($lbl, $col, $arg) = array_pad(self::$cols[$i], 3, null);
66
67
            if (is_null($col)) {
68
                $arg['sort'] = false;
69
            }
70
71
            $sort = $del = null;
72
73
            $attr = self::thAttributes($col, $arg, $sort, $del);
74
75
            $ths[] = self::th($i, $attr, $sort, $del, $lbl);
76
        }
77
        return implode('', $ths);
78
    }
79
80
    private static function thAttributes($col, $arg, &$sort, &$del)
81
    {
82
        if (isset($arg['width'])) { // Width attribute -> style
83
            $width = 'width:' . $arg['width'] . ';';
84
            $arg['style'] = isset($arg['style']) ?
85
                    $width . $arg['style'] :
86
                    $width;
87
        }
88
89
        if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
90
            $sort = false;
91
        } else {
92
            $sort = isset($arg['sort']) ? $arg['sort'] : $col;
93
        }
94
95
        return array_diff_key((array) $arg, ['sort', 'type', 'width']);
96
    }
97
98
    private static function th($i, $attr, $sort, $del, $lbl)
99
    {
100
        $th = '<th' . self::attributes($attr) . '>';
101
        if ($sort) {
102
            $th .= '<a onclick="table.Sort(' . $i . ',this);">';
103
        }
104
        if (!$del) {
105
            if ($sort == self::$t['order']['col']) {
106
                $span = self::$t['order']['dir'] === 'desc' ?
107
                        self::config('UTF8_DESC_SYMBOL') :
108
                        self::config('UTF8_ASC_SYMBOL');
109
            } else {
110
                $span = "";
111
            }
112
            $th .= '<span>' . $span . '</span>' . $lbl;
113
        } else {
114
            $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
115
                    ' onclick=\"checkAllDeleteCheckboxes(this,' .
116
                    ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
117
        }
118
        if ($sort) {
119
            $th .= '</a>';
120
        }
121
        $th .= '</th>';
122
        return $th;
123
    }
124
}
125