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