Thead::thTag()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 16
nop 5
dl 0
loc 26
rs 9.0111
c 0
b 0
f 0
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|string 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
61
        return self::attributes($attr);
62
    }
63
64
    protected static function ths()
65
    {
66
        $ths = [];
67
        $length = count(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
            $sort = $del = null;
76
77
            $attr = self::thAttributes($col, $arg, $sort, $del);
78
79
            $ths[] = self::thTag($i, $attr, $sort, $del, $lbl);
80
        }
81
82
        return implode('', $ths);
83
    }
84
85
    private static function thAttributes($col, $arg, &$sort, &$del)
86
    {
87
        if (isset($arg['width'])) { // Width attribute -> style
88
            $width = 'width:'.$arg['width'].';';
89
            $arg['style'] = isset($arg['style']) ?
90
                $width.$arg['style'] :
91
                $width;
92
        }
93
94
        if (($del = isset($arg['type']) && $arg['type'] == 'delete')) {
95
            $sort = false;
96
        } else {
97
            $sort = isset($arg['sort']) ? $arg['sort'] : $col;
98
        }
99
100
        return array_diff_key((array) $arg, ['sort', 'type', 'width']);
101
    }
102
103
    private static function thTag($index, $attr, $sort, $del, $lbl)
104
    {
105
        $tag = '<th'.self::attributes($attr).'>';
106
        if ($sort) {
107
            $tag .= '<a onclick="table.Sort('.$index.',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
            $tag .= '<span>'.$span.'</span>'.$lbl;
118
        } else {
119
            $tag .= '<input id="'.self::$t['items'].'CheckDeleteAll"'.
120
                ' onclick=\"checkAllDeleteCheckboxes(this,'.
121
                ' \''.self::$t['items'].'\')" type="checkbox"/>';
122
        }
123
        if ($sort) {
124
            $tag .= '</a>';
125
        }
126
        $tag .= '</th>';
127
128
        return $tag;
129
    }
130
}
131