Passed
Push — master ( 74b95b...af185e )
by Plamen
01:33
created

Thead::thTag()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
c 0
b 0
f 0
nc 16
nop 5
dl 0
loc 25
rs 9.0111
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
        $vars = ['items' => $items];
14
15
        if (self::config('FILTER_ACTIVE')) {
16
            self::filterValues($vars['f_value'], $vars['f_options']);
17
        }
18
19
        $vars['div_attributes'] = self::tagAttributes('div', $items);
20
21
        $vars['table_attributes'] = self::tagAttributes('table', $items);
22
23
        $vars['ths'] = self::$cols ? self::ths() : null;
24
25
        $vars['body'] = self::rowsTbody();
26
27
        $vars['footer'] = self::rowsTfoot();
28
29
        return self::view('views/table.html', $vars);
30
    }
31
32
    protected static function filterValues(&$filter, &$opts = [])
33
    {
34
        $filter = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?:
35
            null;
36
37
        $filterBy = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
38
        foreach (self::$cols as $key => $value) {
39
            if (isset($value[2]['sort']) && $value[2]['sort'] === false) {
40
                continue;
41
            }
42
            $selected = $filterBy === $key ? ' selected' : null;
43
            $opts[] = "<option value=\"{$key}\"{$selected}>{$value[0]}</option>";
44
        }
45
    }
46
47
    private static function tagAttributes($tag, $items)
48
    {
49
        $attr = $tag === 'div' ?
50
            ['id' => $items . '-list', 'class' => 'table'] :
51
            ['id' => $items . '-table', 'data-table' => 'js',
52
            'data-sort-a' => self::config('UTF8_ASC_SYMBOL'),
53
            'data-sort-d' => self::config('UTF8_DESC_SYMBOL')];
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
        $ths = [];
66
        $length = sizeof(self::$cols);
67
        for ($i = 0; $i < $length; $i++) {
68
            list($lbl, $col, $arg) = array_pad(self::$cols[$i], 3, null);
69
70
            if (is_null($col)) {
71
                $arg['sort'] = false;
72
            }
73
74
            $sort = $del = null;
75
76
            $attr = self::thAttributes($col, $arg, $sort, $del);
77
78
            $ths[] = self::thTag($i, $attr, $sort, $del, $lbl);
79
        }
80
        return implode('', $ths);
81
    }
82
83
    private static function thAttributes($col, $arg, &$sort, &$del)
84
    {
85
        if (isset($arg['width'])) { // Width attribute -> style
86
            $width = 'width:' . $arg['width'] . ';';
87
            $arg['style'] = isset($arg['style']) ?
88
                $width . $arg['style'] :
89
                $width;
90
        }
91
92
        if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
93
            $sort = false;
94
        } else {
95
            $sort = isset($arg['sort']) ? $arg['sort'] : $col;
96
        }
97
98
        return array_diff_key((array) $arg, ['sort', 'type', 'width']);
99
    }
100
101
    private static function thTag($index, $attr, $sort, $del, $lbl)
102
    {
103
        $tag = '<th' . self::attributes($attr) . '>';
104
        if ($sort) {
105
            $tag .= '<a onclick="table.Sort(' . $index . ',this);">';
106
        }
107
        if (!$del) {
108
            if ($sort == self::$t['order']['col']) {
109
                $span = self::$t['order']['dir'] === 'desc' ?
110
                    self::config('UTF8_DESC_SYMBOL') :
111
                    self::config('UTF8_ASC_SYMBOL');
112
            } else {
113
                $span = "";
114
            }
115
            $tag .= '<span>' . $span . '</span>' . $lbl;
116
        } else {
117
            $tag .= '<input id="' . self::$t['items'] . 'CheckDeleteAll"' .
118
                ' onclick=\"checkAllDeleteCheckboxes(this,' .
119
                ' \'' . self::$t['items'] . '\')" type="checkbox"/>';
120
        }
121
        if ($sort) {
122
            $tag .= '</a>';
123
        }
124
        $tag .= '</th>';
125
        return $tag;
126
    }
127
}
128