Passed
Branchmaster (099bb3)
by Plamen
01:35
created

thead::filterValues()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 7
c 4
b 0
f 0
nc 4
nop 2
dl 0
loc 11
rs 9.2222
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
        $sortable = !isset($arg['sort']) || $arg['sort'] !== false;
90
91
        $sort = $sortable ?
92
                (isset($arg['sort']) ? $arg['sort'] : $col) :
93
                null;
94
95
        if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
96
            $sort = null;
97
        }
98
99
        return array_diff_key((array) $arg, ['sort', 'type', 'width']);
100
    }
101
102
    private static function th($i, $attr, $sort, $del, $lbl)
103
    {
104
        $th = '<th' . self::attributes($attr) . '>';
105
        if ($sort) {
106
            $th .= '<a onclick="table.Sort(' . $i . ',this);">';
107
        }
108
        if (!$del) {
109
            if ($sort == self::$t['order']['col']) {
110
                $span = self::$t['order']['dir'] === 'desc' ?
111
                        self::config('UTF8_DESC_SYMBOL') :
112
                        self::config('UTF8_ASC_SYMBOL');
113
            } else {
114
                $span = "";
115
            }
116
            $th .= '<span>' . $span . '</span>' . $lbl;
117
        } else {
118
            $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
119
                    ' onclick=\"checkAllDeleteCheckboxes(this,' .
120
                    ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
121
        }
122
        if ($sort) {
123
            $th .= '</a>';
124
        }
125
        $th .= '</th>';
126
        return $th;
127
    }
128
129
    protected static function reset($tableId)
130
    {
131
        if (!in_array($tableId, self::$t["tables"])) {
132
            self::$t["tables"][] = $tableId;
133
            self::$cols = [];
134
            self::$t['rows'] = null;
135
        } else {
136
            echo 'Existing table-id used in table::create(): ' . $tableId;
137
        }
138
    }
139
}
140