1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
trait trait_setter |
4
|
|
|
{ |
5
|
|
|
/** @param array Columns (config) */ |
6
|
|
|
public static $cols; |
7
|
|
|
/** @param array Query result, allows altering before table::load() */ |
8
|
|
|
public static $data = []; |
9
|
|
|
/** @param bool Export flag (for current request) */ |
10
|
|
|
public static $export; |
11
|
|
|
/** @param bool Export is allowed (shows buttons in the tfoot) */ |
12
|
|
|
public static $exportActive = true; |
13
|
|
|
/** @param bool Export data altered (use false for pure result) */ |
14
|
|
|
public static $exportDataAsDisplayed = true; |
15
|
|
|
/** @param array Collector (values for current table call) */ |
16
|
|
|
protected static $t = ['page' => 1, 'tables' => []]; |
17
|
|
|
/** @param array Re-settable values for current table call */ |
18
|
|
|
protected static $config = [ |
19
|
|
|
'APP' => 'App', |
20
|
|
|
'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI |
21
|
|
|
'EMPTY_TABLE_MSG' => 'No results found.', |
22
|
|
|
'EXPORT_FILE_NAME' => ':app - :items export (:datetime)', |
23
|
|
|
'FILTER_ACTIVE' => true, |
24
|
|
|
'FILTER_CASE_SENSITIVE' => false, |
25
|
|
|
'SAVES' => ['CSV', 'Excel'], |
26
|
|
|
'UTF8_ASC_SYMBOL' => '▲', |
27
|
|
|
'UTF8_DESC_SYMBOL' => '▼', |
28
|
|
|
'UTF8_LEFT_SYMBOL' => '‹', |
29
|
|
|
'UTF8_RIGHT_SYMBOL' => '›' |
30
|
|
|
]; |
31
|
|
|
/** @param array Errors collector */ |
32
|
|
|
protected static $errors = []; |
33
|
|
|
|
34
|
|
|
static function assets($path = "/public/table") |
35
|
|
|
{ |
36
|
|
|
return "<script src=\"{$path}/table_helper.js\" defer></script>\n\t" . |
37
|
|
|
"<script src=\"{$path}/table.js\" defer></script>\n\t" . |
38
|
|
|
"<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n"; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** Set/Get config value |
42
|
|
|
* @param mixed $c (string) Get if exists, (array) Set if valid |
43
|
|
|
* @return mixed */ |
44
|
|
|
public static function config($c) |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
|
|
switch (gettype($c)) { |
48
|
|
|
case 'array'; |
49
|
|
|
foreach ($c as $k => $v) { |
50
|
|
|
self::$config[$k] = self::getValid((string)$k, $v); |
51
|
|
|
} |
52
|
|
|
break; |
53
|
|
|
case 'string': |
54
|
|
|
return self::getValid($c); |
55
|
|
|
default: |
56
|
|
|
throw new Exception("Invalid value type."); |
57
|
|
|
} |
58
|
|
|
} catch (Exception $e) { |
59
|
|
|
self::err('ERROR: ' . $e->getMessage()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private static function getValid($k, $v = null) |
64
|
|
|
{ |
65
|
|
|
if (!array_key_exists($k, self::$config)) { |
66
|
|
|
throw new Exception('Request to undefined value: ' . $k); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($v !== null) { |
70
|
|
|
if (empty($v) || gettype($v) !== gettype(self::$config[$k])) { |
71
|
|
|
throw new Exception("Setting invalid value: $v (:$k)"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $v === null ? self::$config[$k] : $v; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** Adds conditions, orderBy and Limits to query. |
79
|
|
|
* @param string $q |
80
|
|
|
* @param mixed $cond |
81
|
|
|
* @param array $order |
82
|
|
|
* @param mixed $limit -> 5 or [$start, $count] |
83
|
|
|
* @param bool $h Having flag - to use HAVING instead of WHERE |
84
|
|
|
* @return (string) */ |
85
|
|
|
protected static function q($q, $cond = null, array $order = [], $limit = 0, $h = false) |
86
|
|
|
{ |
87
|
|
|
//Condition: '' | ' (HAVING|WHERE|AND) ' . $cond |
88
|
|
|
self::qConditions($q, $cond, $h); |
89
|
|
|
|
90
|
|
|
//Order: '' | 'ORDER BY ' . array_keys($order)[0] . ' ' . $order[0] |
91
|
|
|
self::qOrder($q, $order); |
92
|
|
|
|
93
|
|
|
//Limit: '' | ' LIMIT ' . '(20, 40|20)' |
94
|
|
|
self::qLimit($q, $limit); |
95
|
|
|
|
96
|
|
|
return $q; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private static function qConditions(&$q, $cond, $h) |
100
|
|
|
{ |
101
|
|
|
if (!empty($cond)) { |
102
|
|
|
$clause = !$h ? 'WHERE' : 'HAVING'; |
103
|
|
|
$clue = !strpos($q, $clause) ? $clause : 'AND'; |
104
|
|
|
$q .= (' ' . $clue . ' ' . $cond); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private static function qOrder(&$q, $order) |
109
|
|
|
{ |
110
|
|
|
if (!empty($order)) { |
111
|
|
|
$arr = array_map(function(&$v, $k) { |
112
|
|
|
return $k . ' ' . strtoupper($v); |
113
|
|
|
}, $order, array_keys($order)); |
114
|
|
|
$q .= (' ORDER BY ' . implode(', ', $arr)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private static function qLimit(&$q, $limit) |
119
|
|
|
{ |
120
|
|
|
if (!empty($limit)) { |
121
|
|
|
$l = (is_array($limit) ? implode(', ', $limit) : $limit); |
122
|
|
|
$q .= (' LIMIT ' . $l); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected static function err($message = null) |
127
|
|
|
{ |
128
|
|
|
if (isset($message)) { |
129
|
|
|
self::$errors[] = $message; |
130
|
|
|
} else { |
131
|
|
|
return empty(self::$errors) ? null : |
132
|
|
|
'<p class="tbl-err">' . implode('</br>', self::$errors) . '</p>'; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** Array to space separated key value list |
137
|
|
|
* @param array $attributes |
138
|
|
|
* @return string */ |
139
|
|
|
protected static function attributes(array $attributes) |
140
|
|
|
{ |
141
|
|
|
$list = [' ']; |
142
|
|
|
foreach ($attributes as $key => $value) { |
143
|
|
|
if ($value === true || (empty($value) && $value != 0)) { |
144
|
|
|
$list[] = $key; |
145
|
|
|
} else { |
146
|
|
|
$list[] = $key . '="' . $value . '"'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
return rtrim(implode(' ', $list)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** Parses view to string |
153
|
|
|
* @param string $template |
154
|
|
|
* @param array $vars |
155
|
|
|
* @return string */ |
156
|
|
|
protected static function view($template, $vars = []) |
157
|
|
|
{ |
158
|
|
|
extract($vars); |
159
|
|
|
ob_start(); |
160
|
|
|
require $template; |
161
|
|
|
return (string)ob_get_clean(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** Needed for more than one table on page |
165
|
|
|
* @param string $items */ |
166
|
|
|
protected static function reset($items) |
167
|
|
|
{ |
168
|
|
|
if (!in_array($items, self::$t["tables"])) { |
169
|
|
|
self::$t["tables"][] = $items; |
170
|
|
|
self::$cols = []; |
171
|
|
|
self::$t['rows'] = null; |
172
|
|
|
} else { |
173
|
|
|
echo 'Existing table-id used in table::create(): ' . $items; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|