1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Table package base class. |
4
|
|
|
* |
5
|
|
|
* @category Helper |
6
|
|
|
* |
7
|
|
|
* @author Plamen Marinov <[email protected]> |
8
|
|
|
* @license https://bitbucket.org/webdivane/table/src/master/LICENSE.md MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Note: Table::prepare() - call must come before `headers_sent()`; |
13
|
|
|
* 1. Create (initial setup): |
14
|
|
|
* <code>Table::create('tableId', 'sortByDbCol', 'sortDirection');</code> |
15
|
|
|
* 1.1. Set columns to be displayed: <code> |
16
|
|
|
* Table::$cols[] = ['innerHtml', 'dbColumn|Name', |
17
|
|
|
* ['width'=>'?px','sort'=>false]];</code> |
18
|
|
|
* 2. Execute (data query) |
19
|
|
|
* <code>Table::execute(sqlQuery);</code> |
20
|
|
|
* 2.1. Alter loaded data, before table load <code> |
21
|
|
|
* foreach(Table::$data as $r => &$cells){ |
22
|
|
|
* $cells['id'] = $cells['id']==3 ? |
23
|
|
|
* [$cells['id'] ,['class'=>'red']] : $cells['id']; |
24
|
|
|
* }</code> |
25
|
|
|
* 3. Loads the markup, or json data (on sort/page/export etc. actions). |
26
|
|
|
* <code>Table::load();</code>. |
27
|
|
|
*/ |
28
|
|
|
class Table extends Thead |
29
|
|
|
{ |
30
|
|
|
use TraitRequest; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Adds needed JavaScript and CSS into the page header, it has to be |
34
|
|
|
* included before "headers_sent()". |
35
|
|
|
* |
36
|
|
|
* @param string|bool $setOrCheck - uses helper class to verify own load |
37
|
|
|
*/ |
38
|
|
|
public static function prepare($setOrCheck = false) |
39
|
|
|
{ |
40
|
|
|
//@see http://php.net/manual/es/function.filter-input.php#77307 |
41
|
|
|
$uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL) ?: |
42
|
|
|
filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL); |
43
|
|
|
|
44
|
|
|
$extension = pathinfo(strtok($uri, '?'), PATHINFO_EXTENSION); |
45
|
|
|
|
46
|
|
|
self::$t['slug'] = pathinfo($uri, PATHINFO_BASENAME); |
47
|
|
|
self::$pageExt = strtolower($extension); |
48
|
|
|
|
49
|
|
|
if (self::$helperClass::prepare(__METHOD__, $setOrCheck) !== true) { |
50
|
|
|
if (self::$pageExt === 'json' && !isset(self::$t['prepared'])) { |
51
|
|
|
//ob_start(); |
52
|
|
|
self::$t['prepared'] = true; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* #1. Create (setup). |
59
|
|
|
* |
60
|
|
|
* @param string $items - The items name |
61
|
|
|
* @param string $orderBy - a db column name |
62
|
|
|
* @param string $orderDir - (Default: self::DEFAULT_SORT_ORDER) |
63
|
|
|
* @param int $paging - Rows per page |
64
|
|
|
*/ |
65
|
|
|
public static function create( |
66
|
|
|
$items, |
67
|
|
|
$orderBy, |
68
|
|
|
$orderDir = 'asc', |
69
|
|
|
$paging = 10 |
70
|
|
|
) { |
71
|
|
|
self::reset((self::$t['items'] = (string) $items)); |
72
|
|
|
self::prepare(true); |
73
|
|
|
self::$t['order']['col'] = $orderBy; |
74
|
|
|
$dir = strtolower($orderDir); |
75
|
|
|
self::$t['order']['dir'] = in_array($dir, ['asc', 'desc']) ? |
76
|
|
|
$dir : self::error('Invalid orderDir (Asc/Desc): '.$orderDir); |
77
|
|
|
self::$t['paging'] = ($num = abs($paging)) >= 10 ? |
78
|
|
|
$num : self::error('Invalid paging (<10): '.$paging); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* #2. Execute (queries). |
83
|
|
|
* |
84
|
|
|
* @param string $query - query for the table data; |
85
|
|
|
* @param (null)|string $qTotal - simple version of $sql if applicable |
86
|
|
|
* (used only for Total count in the table footer). |
87
|
|
|
* |
88
|
|
|
* Example: <pre>$sql = 'SELECT `id`, ... FROM `table` WHERE ...'</pre> |
89
|
|
|
* For query to many thousands results, will be faster to have: |
90
|
|
|
* <pre>$sqlTotals = 'SELECT `id` FROM `table` WHERE ...'</pre> |
91
|
|
|
*/ |
92
|
|
|
public static function execute($query, $qTotal = null) |
93
|
|
|
{ |
94
|
|
|
self::$t = self::request(self::$export); |
95
|
|
|
|
96
|
|
|
$order = [self::$t['order']['col'] => self::$t['order']['dir']]; |
97
|
|
|
$offset = (self::$t['page'] - 1) * self::$t['paging']; |
98
|
|
|
$limit = [$offset, self::$t['paging']]; |
99
|
|
|
self::$t['q'] = self::query($query, self::$t['filter'], $order, $limit, true); |
100
|
|
|
|
101
|
|
|
$qAll = isset($qTotal) && !self::$t['filter'] ? $qTotal : $query; |
102
|
|
|
$ordAll = !self::$export ? [] : $order; |
103
|
|
|
self::$t['qAll'] = self::query($qAll, self::$t['filter'], $ordAll, 0, true); |
104
|
|
|
|
105
|
|
|
$q = !self::$export ? self::$t['q'] : self::$t['qAll']; |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
self::$data = self::select($q); |
109
|
|
|
} catch (Exception $e) { |
110
|
|
|
self::error('ERROR: '.$q.'<br />'.$e->getMessage()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* #3. Loads output (HTML, JSON or CSV file). |
116
|
|
|
*/ |
117
|
|
|
public static function load() |
118
|
|
|
{ |
119
|
|
|
if (self::$pageExt !== 'json') { |
120
|
|
|
echo parent::load(); |
121
|
|
|
} else { |
122
|
|
|
$tableId = filter_input(INPUT_GET, 'table-id') ?: //4 testing |
123
|
|
|
filter_var($_GET['table-id'], FILTER_SANITIZE_STRING); |
124
|
|
|
if ($tableId === self::$t['items'].'-table') { |
125
|
|
|
//ob_get_clean(); |
126
|
|
|
//ob_start('ob_gzhandler'); |
127
|
|
|
if (!self::$export) { |
128
|
|
|
//header('Content-Type: application/json'); |
129
|
|
|
$jsonArr = ['body' => self::jsonTbody(), |
130
|
|
|
'footer' => self::jsonTfoot(), ]; |
131
|
|
|
echo json_encode($jsonArr); |
132
|
|
|
} else { |
133
|
|
|
return self::export(); |
134
|
|
|
} |
135
|
|
|
//ob_end_flush(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|