|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class table_setter |
|
4
|
|
|
{ |
|
5
|
|
|
/** @param array Columns (config) */ |
|
6
|
|
|
public static $cols; |
|
7
|
|
|
/** @param bool Export flag (for current request) */ |
|
8
|
|
|
public static $export; |
|
9
|
|
|
/** @param array Collector (values for current table call) */ |
|
10
|
|
|
protected static $t = ['page' => 1, 'tables' => []]; |
|
11
|
|
|
/** @param array Re-settable values for current table call */ |
|
12
|
|
|
protected static $config; |
|
13
|
|
|
|
|
14
|
|
|
static function assets($path = "/public/table") |
|
15
|
|
|
{ |
|
16
|
|
|
return "<script src=\"{$path}/table.js\"></script>\n\t" . |
|
17
|
|
|
"<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n"; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public static function config($c) |
|
21
|
|
|
{ |
|
22
|
|
|
self::$config = self::$config ?: [ |
|
23
|
|
|
'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI |
|
24
|
|
|
'EMPTY_TABLE_MSG' => 'No results found.', |
|
25
|
|
|
'EXPORT_FILE_NAME' => ':app - :items export (:datetime)', |
|
26
|
|
|
'FILTER_CASE_SENSITIVE' => false, |
|
27
|
|
|
'SAVES' => ['CSV', 'Excel'], |
|
28
|
|
|
'UTF8_ASC_SYMBOL' => '▲', |
|
29
|
|
|
'UTF8_DESC_SYMBOL' => '▼', |
|
30
|
|
|
'UTF8_LEFT_SYMBOL' => '‹', |
|
31
|
|
|
'UTF8_RIGHT_SYMBOL' => '›' |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
$getValid = function($ex, $v) { //disallow empty and mis-type overrides |
|
35
|
|
|
return !empty($v) && gettype($ex) === gettype($v) ? $v : $ex; |
|
36
|
|
|
}; |
|
37
|
|
|
|
|
38
|
|
|
switch (gettype($c)) { |
|
39
|
|
|
case 'array'; |
|
40
|
|
|
foreach ($c as $k => $v) { |
|
41
|
|
|
self::$config[$k] = $getValid(self::$config[$k], $v); |
|
42
|
|
|
} |
|
43
|
|
|
break; |
|
44
|
|
|
case 'string': |
|
45
|
|
|
return self::$config[$c]; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** Converts array to space separated key value list |
|
50
|
|
|
* @param array $attributes |
|
51
|
|
|
* @return string */ |
|
52
|
|
|
protected static function attributes(array $attributes) |
|
53
|
|
|
{ |
|
54
|
|
|
$list = []; |
|
55
|
|
|
foreach ($attributes as $key => $value) { |
|
56
|
|
|
if (is_bool($value)) { |
|
57
|
|
|
if ((bool) $value) { |
|
58
|
|
|
$list[] = $key; |
|
59
|
|
|
} |
|
60
|
|
|
} else if (empty($value) && !is_int($value)) { |
|
61
|
|
|
$list[] = $key; |
|
62
|
|
|
} else { |
|
63
|
|
|
$list[] = $key . '="' . $value . '"'; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
return (count($list) > 0 ? ' ' : '') . join(' ', $list); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** Parses view to string |
|
70
|
|
|
* @param string $template |
|
71
|
|
|
* @param array $vars |
|
72
|
|
|
* @return string */ |
|
73
|
|
|
protected static function view($template, $vars = []) |
|
74
|
|
|
{ |
|
75
|
|
|
extract($vars); |
|
76
|
|
|
ob_start(); |
|
77
|
|
|
require $template; |
|
78
|
|
|
return (string) ob_get_clean(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected static function paging(&$v) |
|
82
|
|
|
{ |
|
83
|
|
|
if (self::$t['pages'] > 1) { |
|
84
|
|
|
$v['pages'] = self::$t['pages']; |
|
85
|
|
|
$v['page'] = self::$t['page']; |
|
86
|
|
|
$v['jumpL'] = self::pagingJumps(); |
|
87
|
|
|
$v['jumpR'] = self::pagingJumps(); |
|
88
|
|
|
$v['arrowL'] = self::config('UTF8_LEFT_SYMBOL'); |
|
89
|
|
|
$v['arrowR'] = self::config('UTF8_RIGHT_SYMBOL'); |
|
90
|
|
|
|
|
91
|
|
|
$limit = 10; |
|
92
|
|
|
|
|
93
|
|
|
$v['start'] = $v['page'] > ($limit / 2) ? |
|
94
|
|
|
($v['page'] - $limit / 2) : |
|
95
|
|
|
1; |
|
96
|
|
|
|
|
97
|
|
|
if ($v['page'] > ($v['pages'] - ($limit / 2))) { |
|
98
|
|
|
$v['final'] = $v['pages']; |
|
99
|
|
|
} else if ($v['page'] > ($limit / 2)) { |
|
100
|
|
|
$v['final'] = $v['start'] + $limit; |
|
101
|
|
|
} else { |
|
102
|
|
|
$v['final'] = $limit; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** Jump links -1M|-100K|-10K|-1K|100|{paging}|100|+1K|+10K|+100K|+1M |
|
108
|
|
|
* @param string $html Code to show |
|
109
|
|
|
* @param null|int $m multiplier |
|
110
|
|
|
* @return string */ |
|
111
|
|
|
protected static function pagingJumps($html = '', $m = null) |
|
112
|
|
|
{ |
|
113
|
|
|
static $direction; |
|
114
|
|
|
|
|
115
|
|
|
if (is_null($m)) { //on self::createPaginationLinks() calls only |
|
116
|
|
|
$direction = !isset($direction) ? '-' : '+'; |
|
117
|
|
|
$m = 100; |
|
118
|
|
|
} |
|
119
|
|
|
$jump2show = function($m) use ($direction) { |
|
120
|
|
|
if ($m >= 1000000) { |
|
121
|
|
|
return ($direction . ($m / 1000000) . "M"); |
|
122
|
|
|
} else if ($m >= 1000) { |
|
123
|
|
|
return ($direction . ($m / 1000) . "K"); |
|
124
|
|
|
} else { |
|
125
|
|
|
return ($direction . $m); |
|
126
|
|
|
} |
|
127
|
|
|
}; |
|
128
|
|
|
$add = '<li class="jump"><a>' . $jump2show($m) . '</a></li>'; |
|
129
|
|
|
|
|
130
|
|
|
if ($direction === '-') { |
|
131
|
|
|
$jump_exists = (self::$t['page'] - $m) > 0; |
|
132
|
|
|
} else { |
|
133
|
|
|
$jump_exists = (self::$t['page'] + $m) <= self::$t['pages']; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($jump_exists) { |
|
137
|
|
|
$html = $direction === '-' ? $add . $html : $html . $add; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $jump_exists ? self::pagingJumps($html, ($m * 10)) : $html; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected static function filterValues(&$f, &$opts = []) |
|
144
|
|
|
{ |
|
145
|
|
|
$f = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?: null; |
|
146
|
|
|
|
|
147
|
|
|
$by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT); |
|
148
|
|
|
foreach (self::$cols as $k => $v) { |
|
149
|
|
|
if (isset($v[2]['sort']) && $v[2]['sort'] === false) { |
|
150
|
|
|
continue; |
|
151
|
|
|
} |
|
152
|
|
|
if (empty($v)) { |
|
153
|
|
|
$v = [null]; |
|
154
|
|
|
} // fix for column requested as [] |
|
155
|
|
|
$selected = $by === $k ? ' selected' : null; |
|
156
|
|
|
$opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>"; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
protected static function requestFilter() |
|
161
|
|
|
{ |
|
162
|
|
|
$filter = filter_input(INPUT_GET, 'filter') ?: false; |
|
163
|
|
|
if ($filter) { |
|
164
|
|
|
$by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT); |
|
165
|
|
|
if ($by === false || is_null($by)) { |
|
166
|
|
|
$by = []; |
|
167
|
|
|
foreach (self::$cols as $v) { |
|
168
|
|
|
if (isset($v[2]['sort']) && $v[2]['sort'] === false) { |
|
169
|
|
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
$by[] = 'IFNULL(' . $v[1] . ', "")'; |
|
172
|
|
|
} |
|
173
|
|
|
$by = 'CONCAT(' . implode(',', $by) . ')'; |
|
174
|
|
|
} else { |
|
175
|
|
|
$by = self::$cols[$by][1]; |
|
176
|
|
|
} |
|
177
|
|
|
$by = 'CONCAT(" ",' . $by . ', " ")'; |
|
178
|
|
|
if (self::config('FILTER_CASE_SENSITIVE') !== true) { |
|
179
|
|
|
$by .= ' COLLATE ' . self::config('DB_COLLATION_CI'); |
|
180
|
|
|
} |
|
181
|
|
|
$filter = $by . ' LIKE ' . '"%' . $filter . '%"'; |
|
182
|
|
|
} |
|
183
|
|
|
return $filter; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
protected static function requestOrderCol() |
|
187
|
|
|
{ |
|
188
|
|
|
if (($col = filter_input(INPUT_GET, 'col', FILTER_VALIDATE_INT))) { |
|
189
|
|
|
return isset(self::$cols[$col][2]['sort']) ? |
|
190
|
|
|
self::$cols[$col][2]['sort'] : |
|
191
|
|
|
self::$cols[$col][1]; |
|
192
|
|
|
} |
|
193
|
|
|
return self::$t['order']['col']; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
protected static function requestOrderDir() |
|
197
|
|
|
{ |
|
198
|
|
|
$reset = filter_has_var(INPUT_GET, 'col') ? 'asc' : null; |
|
199
|
|
|
return in_array(filter_input(INPUT_GET, 'ord'), ['asc', 'desc']) ? |
|
200
|
|
|
filter_input(INPUT_GET, 'ord') : |
|
201
|
|
|
($reset ?: self::$t['order']['dir']); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
protected static function requestExport() |
|
205
|
|
|
{ |
|
206
|
|
|
$exp = filter_input(INPUT_GET, 'export', FILTER_SANITIZE_STRING); |
|
207
|
|
|
return in_array($exp, self::config('SAVES')) ? $exp : false; |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
protected static function requestPage() |
|
211
|
|
|
{ |
|
212
|
|
|
return filter_has_var(INPUT_GET, 'pg') && self::$export == false ? |
|
213
|
|
|
(int) filter_input(INPUT_GET, 'pg', FILTER_SANITIZE_NUMBER_INT) : |
|
214
|
|
|
self::$t['page']; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|