Passed
Push — master ( 87f610...286eca )
by Plamen
02:07
created

thead::thAttributes()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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