| 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 thead |
||
| 19 | { |
||
| 20 | use trait_table_request; |
||
| 21 | |||
| 22 | /** Adds needed JavaScript and CSS into the page header, it has to be |
||
| 23 | * included before "headers_sent()" |
||
| 24 | * @param str|bool $setOrCheck - uses helper class to verify own load */ |
||
| 25 | public static function prepare($setOrCheck = false) |
||
| 26 | { |
||
| 27 | //@see http://php.net/manual/es/function.filter-input.php#77307 |
||
| 28 | $uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL) ?: |
||
| 29 | filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL); |
||
| 30 | |||
| 31 | $extension = pathinfo(strtok($uri, '?'), PATHINFO_EXTENSION); |
||
| 32 | |||
| 33 | self::$t['slug'] = pathinfo($uri, PATHINFO_BASENAME); |
||
| 34 | self::$pageExt = strtolower($extension); |
||
| 35 | |||
| 36 | if (self::$helper_class::prepare(__METHOD__, $setOrCheck) !== true) { |
||
| 37 | if (self::$pageExt === 'json' && !isset(self::$t['prepared'])) { |
||
| 38 | ob_start(); |
||
| 39 | self::$t['prepared'] = true; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** #1. Create (setup) |
||
| 45 | * @param string $items - The items name |
||
| 46 | * @param string $orderBy - a db column name |
||
| 47 | * @param string $orderDir - (Default: self::DEFAULT_SORT_ORDER) |
||
| 48 | * @param int $paging - Rows per page */ |
||
| 49 | public static function create( |
||
| 50 | $items, |
||
| 51 | $orderBy, |
||
| 52 | $orderDir = 'asc', |
||
| 53 | $paging = 10 |
||
| 54 | ) |
||
| 55 | { |
||
| 56 | self::reset((self::$t['items'] = $items)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | self::prepare(true); |
||
| 58 | self::$t['order']['col'] = $orderBy; |
||
| 59 | $dir = strtolower($orderDir); |
||
| 60 | self::$t['order']['dir'] = in_array($dir, ['asc', 'desc']) ? |
||
| 61 | $dir : self::err('Invalid orderDir (Asc/Desc): ' . $orderDir); |
||
| 62 | self::$t['paging'] = ($p = abs($paging)) > 10 ? |
||
| 63 | $p : self::err('Invalid paging (>10): ' . $paging); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** #2. Execute (queries) |
||
| 67 | * @param string $q - query for the table data; |
||
| 68 | * @param (null)|string $qTotal - simple version of $sql if applicable |
||
| 69 | * (used only for Total count in the table footer).<br> |
||
| 70 | * Example: <pre>$sql = 'SELECT `id`, ... FROM `table` WHERE ...'</pre> |
||
| 71 | * For query to many thousands results, will be faster to have: |
||
| 72 | * <pre>$sqlTotals = 'SELECT `id` FROM `table` WHERE ...'</pre> */ |
||
| 73 | public static function execute($q, $qTotal = null) |
||
| 74 | { |
||
| 75 | self::$t['order']['col'] = self::requestOrderCol(); |
||
| 76 | self::$t['order']['dir'] = self::requestOrderDir(); |
||
| 77 | self::$t['page'] = self::requestPage(); |
||
| 78 | self::$export = self::requestExport(); |
||
| 79 | |||
| 80 | $filter = self::requestFilter(); |
||
| 81 | $order = [self::$t['order']['col'] => self::$t['order']['dir']]; |
||
| 82 | $offset = (self::$t['page'] - 1) * self::$t['paging']; |
||
| 83 | $limit = [$offset, self::$t['paging']]; |
||
| 84 | self::$t['q'] = self::q($q, $filter, $order, $limit, true); |
||
| 85 | |||
| 86 | $qAll = isset($qTotal) && !$filter ? $qTotal : $q; |
||
| 87 | $orderAll = !self::$export ? [] : $order; |
||
| 88 | self::$t['qAll'] = self::q($qAll, $filter, $orderAll, 0, true); |
||
| 89 | |||
| 90 | $query = !self::$export ? self::$t['q'] : self::$t['qAll']; |
||
| 91 | |||
| 92 | try { |
||
| 93 | self::$data = self::select($query); |
||
| 94 | } catch (Exception $e) { |
||
| 95 | self::err('ERROR: ' . $q . '<br />' . $e->getMessage()); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** #3. Loads output (html, json or csv file) */ |
||
| 100 | public static function load() |
||
| 101 | { |
||
| 102 | if (self::$pageExt !== 'json') { |
||
| 103 | echo parent::load(); |
||
| 104 | } else { |
||
| 105 | ob_get_clean(); |
||
| 106 | $tableId = filter_input(INPUT_GET, 'table-id') ?: null; |
||
| 107 | if ($tableId === self::$t['items'] . '-table') { |
||
| 108 | if (!self::$export) { |
||
| 109 | header('Content-Type: application/json'); |
||
| 110 | $jsonArr = ['body' => self::jsonTbody(), |
||
| 111 | 'footer' => self::jsonTfoot()]; |
||
| 112 | die(json_encode($jsonArr)); |
||
|
0 ignored issues
–
show
|
|||
| 113 | } else { |
||
| 114 | self::export(); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 |