| 1 | <?php |
||
| 2 | |||
| 3 | class tbody extends tfoot |
||
| 4 | { |
||
| 5 | protected static function jsonGetBodyData() |
||
| 6 | { |
||
| 7 | $json = $outColumns = []; |
||
| 8 | foreach (self::$cols as $c) { |
||
| 9 | $outColumns[] = isset($c[1]) ? $c[1] : null; |
||
| 10 | } |
||
| 11 | if ((self::$t['rows'] = count(self::$data)) > 0) { |
||
| 12 | foreach (self::$data as $row) { |
||
| 13 | $rowData = []; |
||
| 14 | foreach ($outColumns as $dbName) { |
||
| 15 | $rowData[] = array_key_exists($dbName, $row) ? |
||
| 16 | $row[$dbName] : ''; |
||
| 17 | } |
||
| 18 | $json[] = $rowData; |
||
| 19 | } |
||
| 20 | } else { |
||
| 21 | $attr = ['colspan' => count(self::$cols), 'class' => 'no-results']; |
||
| 22 | $json[] = [[self::config('EMPTY_TABLE_MSG'), $attr]]; |
||
| 23 | } |
||
| 24 | return $json; |
||
| 25 | } |
||
| 26 | |||
| 27 | protected static function rowsThead() |
||
| 28 | { |
||
| 29 | $trs = ''; |
||
| 30 | foreach (self::jsonGetBodyData() as $r) { |
||
| 31 | if (isset($r[0][1], $r[0][1]['class']) && |
||
| 32 | $r[0][1]['class'] === 'no-results' |
||
| 33 | ) { |
||
| 34 | $trs .= '<tr><td' . self::attributes($r[0][1]) . '>' |
||
| 35 | . $r[0][0] . '</td></tr>'; |
||
| 36 | } else { |
||
| 37 | $trs .= '<tr><td>' . implode('</td><td>', $r) |
||
| 38 | . '</td></tr>'; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | return $trs; |
||
| 42 | } |
||
| 43 | |||
| 44 | protected static function export() |
||
| 45 | { |
||
| 46 | $data = self::$exportDataAsDisplayed ? |
||
| 47 | self::$data : |
||
| 48 | self::select(self::$t['q']); |
||
| 49 | $fnReplace = []; |
||
| 50 | $fnReplace[':app'] = self::config('APP'); |
||
| 51 | $fnReplace[':items'] = self::$t['items']; |
||
| 52 | $format = str_replace(':', '.', '%d.%m.%Y %H:%i:%s'); |
||
| 53 | $timeQuery = 'SELECT DATE_FORMAT(Now(), "' . $format . '") AS `now`;'; |
||
| 54 | $fnReplace[':datetime'] = self::select($timeQuery); |
||
| 55 | $outFilename = strtr(self::config('EXPORT_FILE_NAME'), $fnReplace); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 56 | $outColumns = $outHeader = []; |
||
| 57 | foreach (self::$cols as $c) { |
||
| 58 | if (isset($c[2]['sort']) && $c[2]['sort'] === false) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | $outColumns[] = $c[1]; |
||
| 62 | $outHeader[] = $c[0]; |
||
| 63 | } |
||
| 64 | $eData = [$outHeader]; |
||
| 65 | if (count($data) > 0) { |
||
| 66 | foreach ($data as $row) { |
||
| 67 | $rowData = []; |
||
| 68 | foreach ($row as $dbName => $value) { |
||
| 69 | if (in_array($dbName, $outColumns)) { |
||
| 70 | $rowData[] = is_array($value) ? $value[0] : $value; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | $eData[] = $rowData; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | self::export2file($eData, $outFilename); |
||
| 77 | } |
||
| 78 | |||
| 79 | private static function export2file($eData, $outFilename) |
||
| 80 | { |
||
| 81 | switch (self::$export) { |
||
| 82 | case 'Excel': |
||
| 83 | case 'CSV': |
||
| 84 | $escape = function($v) { |
||
| 85 | return str_replace("\t", "	", $v); |
||
| 86 | }; |
||
| 87 | header('Content-Type:application/csv'); |
||
| 88 | header('Content-Disposition:attachment; filename=' . |
||
| 89 | $outFilename . '.csv'); |
||
| 90 | |||
| 91 | if (($output = fopen('php://output', 'w'))) { |
||
| 92 | foreach ($eData as $v) { |
||
| 93 | self::$export === 'CSV' ? |
||
| 94 | fputcsv($output, $v) : |
||
| 95 | fputs($output, implode("\t", array_map($escape, $v)) . "\r\n"); |
||
| 96 | } |
||
| 97 | fclose($output); |
||
| 98 | } |
||
| 99 | break; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 |