1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
trait TraitHelper |
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
|
|
|
|
18
|
|
|
public static function assets($path = '/public/table') |
19
|
|
|
{ |
20
|
|
|
return "<script src=\"{$path}/table_helper.js\" defer></script>\n\t". |
21
|
|
|
"<script src=\"{$path}/table.js\" defer></script>\n\t". |
22
|
|
|
"<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n"; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Array to space separated key value list. |
27
|
|
|
* |
28
|
|
|
* @param array $attributes |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
protected static function attributes(array $attributes) |
33
|
|
|
{ |
34
|
|
|
$list = [' ']; |
35
|
|
|
foreach ($attributes as $key => $value) { |
36
|
|
|
if ($value === true || (empty($value) && $value != 0)) { |
37
|
|
|
$list[] = $key; |
38
|
|
|
} else { |
39
|
|
|
$list[] = $key.'="'.$value.'"'; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return rtrim(implode(' ', $list)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parses view to string. |
48
|
|
|
* |
49
|
|
|
* @param string $template |
50
|
|
|
* @param array $vars |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected static function view($template, $vars = []) |
55
|
|
|
{ |
56
|
|
|
extract($vars); |
57
|
|
|
ob_start(); |
58
|
|
|
require $template; |
59
|
|
|
|
60
|
|
|
return (string) ob_get_clean(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Needed for more than one table on page. |
65
|
|
|
* |
66
|
|
|
* @param string $items |
67
|
|
|
*/ |
68
|
|
|
protected static function reset($items) |
69
|
|
|
{ |
70
|
|
|
if (!in_array($items, self::$t['tables'])) { |
71
|
|
|
self::$t['tables'][] = $items; |
72
|
|
|
self::$cols = []; |
73
|
|
|
self::$t['rows'] = null; |
74
|
|
|
} else { |
75
|
|
|
echo 'Existing table-id used in table::create(): '.$items; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests value protected value checker. |
81
|
|
|
*/ |
82
|
|
|
public static function confirmValue($property, $value) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$nodes = explode('.', $property); |
86
|
|
|
$name = array_shift($nodes); |
87
|
|
|
if (property_exists(__CLASS__, $name)) { |
88
|
|
|
$val = function (&$val) use ($nodes) { |
89
|
|
|
$temp = &$val; |
90
|
|
|
foreach ($nodes as $key) { |
91
|
|
|
if ($key === 'last') { |
92
|
|
|
return array_pop($temp); |
93
|
|
|
} |
94
|
|
|
$temp = &$temp[$key]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $temp; |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
if (($v = $val(static::$$name)) !== null) { |
101
|
|
|
return $v === $value; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw new Exception('Missing value ('.implode($nodes).').'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new Exception('Undefined property ('.$name.').'); |
108
|
|
|
} catch (Exception $e) { |
109
|
|
|
echo 'Caught exception: ', $e->getMessage(), "\n"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|