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
|
|
|
protected static function request() |
128
|
|
|
{ |
129
|
|
|
$export = function(){ |
130
|
|
|
$exp = filter_input(INPUT_GET, 'export', FILTER_SANITIZE_STRING); |
131
|
|
|
return in_array($exp, self::config('SAVES')) ? $exp : false; |
|
|
|
|
132
|
|
|
}; |
133
|
|
|
$order_dir = function(){ |
134
|
|
|
$reset = filter_has_var(INPUT_GET, 'col') ? 'asc' : null; |
135
|
|
|
return |
136
|
|
|
in_array(filter_input(INPUT_GET, 'ord'), ['asc', 'desc']) ? |
137
|
|
|
filter_input(INPUT_GET, 'ord') : |
138
|
|
|
($reset ?: self::$t['order']['dir']); |
139
|
|
|
}; |
140
|
|
|
$order_col = function(){ |
141
|
|
|
$col = filter_input(INPUT_GET, 'col', FILTER_VALIDATE_INT); |
142
|
|
|
if ($col) { |
143
|
|
|
return isset(self::$cols[$col][2]['sort']) ? |
144
|
|
|
self::$cols[$col][2]['sort'] : |
145
|
|
|
self::$cols[$col][1]; |
146
|
|
|
} |
147
|
|
|
return self::$t['order']['col']; |
148
|
|
|
}; |
149
|
|
|
$filter = function(){ |
150
|
|
|
$filter = filter_input(INPUT_GET, 'filter') ?: false; |
151
|
|
|
$filterByAll = function(){ |
152
|
|
|
$by = []; |
153
|
|
|
foreach (self::$cols as $v) { |
154
|
|
|
if (isset($v[2]['sort']) && $v[2]['sort'] === false) { |
155
|
|
|
continue; |
156
|
|
|
} |
157
|
|
|
$by[] = 'IFNULL(' . $v[1] . ', "")'; |
158
|
|
|
} |
159
|
|
|
return 'CONCAT(' . implode(',', $by) . ')'; |
160
|
|
|
}; |
161
|
|
|
if ($filter) { |
162
|
|
|
$by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT); |
163
|
|
|
if ($by === false || is_null($by)) { |
164
|
|
|
$by = $filterByAll(); |
165
|
|
|
} else { |
166
|
|
|
$by = self::$cols[$by][1]; |
167
|
|
|
} |
168
|
|
|
$by = 'CONCAT(" ",' . $by . ', " ")'; |
169
|
|
|
if (self::config('FILTER_CASE_SENSITIVE') !== true) { |
170
|
|
|
$by .= ' COLLATE ' . self::config('DB_COLLATION_CI'); |
171
|
|
|
} |
172
|
|
|
$filter = $by . ' LIKE ' . '"%' . $filter . '%"'; |
173
|
|
|
} |
174
|
|
|
return $filter; |
175
|
|
|
}; |
176
|
|
|
$page = function(){ |
177
|
|
|
return filter_has_var(INPUT_GET, 'pg') && self::$export == false ? |
178
|
|
|
(int)filter_input(INPUT_GET, 'pg', FILTER_SANITIZE_NUMBER_INT) : |
179
|
|
|
self::$t['page']; |
180
|
|
|
}; |
181
|
|
|
|
182
|
|
|
self::$export = $export(); |
183
|
|
|
|
184
|
|
|
$t = [ |
185
|
|
|
'order' => [ |
186
|
|
|
'dir' => $order_dir(), |
187
|
|
|
'col' => $order_col() |
188
|
|
|
], |
189
|
|
|
'filter' => $filter(), |
190
|
|
|
'page' => $page() |
191
|
|
|
]; |
192
|
|
|
//dd(array_merge(self::$t, $t)); |
193
|
|
|
return array_merge(self::$t, $t); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|