Passed
Branchmaster (893a8b)
by Plamen
01:33
created

thead::filterValues()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 3
b 0
f 0
nc 6
nop 2
dl 0
loc 14
rs 8.8333
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::ths();
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
        if (self::$cols) {
66
            $ths = [];
67
            $length = sizeof(self::$cols);
68
            for ($i = 0; $i < $length; $i++) {
69
                list($lbl, $col, $arg) = array_pad(self::$cols[$i], 3, null);
70
71
                if (is_null($col)) {
72
                    $arg['sort'] = false;
73
                }
74
                
75
                $sortable = !isset($arg['sort']) || $arg['sort'] !== false;
76
77
                if (isset($arg['width'])) { // Width attribute -> style
78
                    $width = 'width:' . $arg['width'] . ';';
79
                    $arg['style'] = isset($arg['style']) ?
80
                            $width . $arg['style'] :
81
                            $width;
82
                }
83
                
84
                if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
85
                    $sortable = false;
86
                    if (false === strpos($arg['style'], 'width:')) {
87
                        $arg['width'] = '1%';
88
                    }
89
                }
90
91
                $sort = $sortable ?
92
                        (isset($arg['sort']) ? $arg['sort'] : $col) :
93
                        null;
94
95
                $attr = array_diff_key((array) $arg, ['sort', 'type', 'width']);
96
97
                $ths[] = self::th($i, $attr, $sort, $del, $lbl);
98
            }
99
            return implode('', $ths);
100
        }
101
    }
102
    
103
    private static function th($i, $attr, $sort, $del, $lbl)
104
    {
105
        $th = '<th' . self::attributes($attr) . '>';
106
        if ($sort) {
107
            $th .= '<a onclick="table.Sort(' . $i . ',this);">';
108
        }
109
        if (!$del) {
110
            if ($sort == self::$t['order']['col']) {
111
                $span = self::$t['order']['dir'] === 'desc' ?
112
                        self::config('UTF8_DESC_SYMBOL') :
113
                        self::config('UTF8_ASC_SYMBOL');
114
            } else {
115
                $span = "";
116
            }
117
            $th .= '<span>' . $span . '</span>' . $lbl;
118
        } else {
119
            $th .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
120
                    ' onclick=\"checkAllDeleteCheckboxes(this,' .
121
                    ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
122
        }
123
        if ($sort) {
124
            $th .= '</a>';
125
        }
126
        $th .= '</th>';
127
        return $th;
128
    }
129
130
    protected static function reset($tableId)
131
    {
132
        if (!in_array($tableId, self::$t["tables"])) {
133
            self::$t["tables"][] = $tableId;
134
            self::$cols = [];
135
            self::$t['rows'] = null;
136
        } else {
137
            echo 'Existing table-id used in table::create(): ' . $tableId;
138
        }
139
    }
140
}
141