1 | <?php |
||||
2 | |||||
3 | /** Note: table::prepare() - call must come before `headers_sent()`; |
||||
4 | * 1. Create (initial setup): |
||||
5 | * <code>table::create('tableId', 'sortByDbCol', 'sortDirection');</code> |
||||
6 | * 1.1. Set columns to be displayed: <code> |
||||
7 | * table::$cols[] = ['innerHtml', 'dbColumn|Name', |
||||
8 | * ['width'=>'?px','sort'=>false]];</code> |
||||
9 | * 2. Execute (data query) |
||||
10 | * <code>table::execute(sqlQuery);</code> |
||||
11 | * 2.1. Alter loaded data, before table load <code> |
||||
12 | * foreach(table::$data as $r => &$cells){ |
||||
13 | * $cells['id'] = $cells['id']==3 ? |
||||
14 | * [$cells['id'] ,['class'=>'red']] : $cells['id']; |
||||
15 | * }</code> |
||||
16 | * 3. Loads the markup, or json data (on sort/page/export etc. actions). |
||||
17 | * <code>table::load();</code> */ |
||||
18 | class table extends table_getter |
||||
19 | { |
||||
20 | /** @param string class name, where $helper::prepare() is expected */ |
||||
21 | public static $helper_class; |
||||
22 | /** @param closure to MySql select function (example: db::select()) */ |
||||
23 | public static $select; |
||||
24 | |||||
25 | /** #1. Create (setup) |
||||
26 | * @param string $items - The items name |
||||
27 | * @param string $orderBy - a db column name |
||||
28 | * @param string $orderDir - (Default: self::DEFAULT_SORT_ORDER) |
||||
29 | * @param int $paging - Rows per page */ |
||||
30 | public static function create($items, $orderBy, $orderDir = 'asc', $paging = 10) |
||||
31 | { |
||||
32 | self::reset((self::$t['items'] = $items)); |
||||
33 | self::prepare(true); |
||||
34 | self::$t['order']['col'] = $orderBy; |
||||
35 | $dir = strtolower($orderDir); |
||||
36 | self::$t['order']['dir'] = in_array($dir, ['asc', 'desc']) ? |
||||
37 | $dir : |
||||
38 | die('Invalid orderDir(Asc/Desc): ' . $orderDir); |
||||
0 ignored issues
–
show
|
|||||
39 | self::$t['paging'] = ($p = abs($paging)) > 10 ? |
||||
40 | $p : |
||||
41 | die('Invalid paging(>10): ' . $paging); |
||||
0 ignored issues
–
show
|
|||||
42 | } |
||||
43 | |||||
44 | /** #2. Execute (queries) |
||||
45 | * @param string $q - query for the table data; |
||||
46 | * @param (null)|string $qTotal - simple version of $sql if applicable |
||||
47 | * (used only for Total count in the table footer).<br> |
||||
48 | * Example: <pre>$sql = 'SELECT `id`, ... FROM `table` WHERE ...'</pre> |
||||
49 | * For query to many thousands results, will be faster to have: |
||||
50 | * <pre>$sqlTotals = 'SELECT `id` FROM `table` WHERE ...'</pre> */ |
||||
51 | public static function execute($q, $qTotal = null) |
||||
52 | { |
||||
53 | self::$t['order']['col'] = self::requestOrderCol(); |
||||
54 | self::$t['order']['dir'] = self::requestOrderDir(); |
||||
55 | self::$t['page'] = self::requestPage(); |
||||
56 | self::$export = self::requestExport(); |
||||
57 | |||||
58 | $filter = self::requestFilter(); |
||||
59 | $order = [self::$t['order']['col'] => self::$t['order']['dir']]; |
||||
60 | $offset = (self::$t['page'] - 1) * self::$t['paging']; |
||||
61 | $limit = [$offset, self::$t['paging']]; |
||||
62 | self::$t['q'] = self::q($q, $filter, $order, $limit, true); |
||||
0 ignored issues
–
show
It seems like
$filter can also be of type false ; however, parameter $cond of table_getter::q() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
63 | |||||
64 | $qAll = isset($qTotal) && !$filter ? $qTotal : $q; |
||||
65 | $orderAll = !self::$export ? [] : $order; |
||||
66 | self::$t['qAll'] = self::q($qAll, $filter, $orderAll, 0, true); |
||||
67 | |||||
68 | $query = !self::$export ? self::$t['q'] : self::$t['qAll']; |
||||
69 | |||||
70 | try { |
||||
71 | self::$data = self::select($query); |
||||
72 | } catch (Exception $e) { |
||||
73 | die('ERROR (query in the table): ' . "\n$q\n" . $e->getMessage()); |
||||
0 ignored issues
–
show
|
|||||
74 | } |
||||
75 | } |
||||
76 | |||||
77 | /** #3. Loads output (html, json or csv file) */ |
||||
78 | public static function load() |
||||
79 | { |
||||
80 | if (self::$pageExt !== 'json') { |
||||
81 | echo parent::load(); |
||||
82 | } else { |
||||
83 | ob_get_clean(); |
||||
84 | $tableId = filter_input(INPUT_GET, 'table-id') ?: null; |
||||
85 | if ($tableId === self::$t['items'] . '-table') { |
||||
86 | if (!self::$export) { |
||||
87 | header('Content-Type: application/json'); |
||||
88 | $jsonArr = ['body' => self::jsonGetBodyData(), |
||||
89 | 'footer' => self::jsonGetFooterData()]; |
||||
90 | die(json_encode($jsonArr)); |
||||
0 ignored issues
–
show
|
|||||
91 | } else { |
||||
92 | return self::exportData(); |
||||
0 ignored issues
–
show
Are you sure the usage of
self::exportData() targeting table_getter::exportData() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
93 | } |
||||
94 | } |
||||
95 | } |
||||
96 | } |
||||
97 | |||||
98 | /** Adds needed JavaScript and CSS into the page header, it has to be |
||||
99 | * included before "headers_sent()" |
||||
100 | * @param str|bool $setOrCheck - uses helper class to verify own load */ |
||||
101 | public static function prepare($setOrCheck = false) |
||||
102 | { |
||||
103 | //@see http://php.net/manual/es/function.filter-input.php#77307 |
||||
104 | $uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL) ?: |
||||
105 | filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL); |
||||
106 | |||||
107 | $extension = pathinfo(strtok($uri, '?'), PATHINFO_EXTENSION); |
||||
108 | |||||
109 | self::$t['slug'] = pathinfo($uri, PATHINFO_BASENAME); |
||||
110 | self::$pageExt = strtolower($extension); |
||||
111 | |||||
112 | if (self::$helper_class::prepare(__METHOD__, $setOrCheck) !== true) { |
||||
113 | if (self::$pageExt === 'json' && !isset(self::$t['prepared'])) { |
||||
114 | ob_start(); |
||||
115 | self::$t['prepared'] = true; |
||||
116 | } |
||||
117 | } |
||||
118 | } |
||||
119 | } |
||||
120 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.