Passed
Branchmaster (1bd863)
by Plamen
01:24
created

thead::filterValues()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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