Passed
Push — master ( 74b95b...af185e )
by Plamen
01:33
created

Tbody::jsonTbody()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.5555
1
<?php
2
3
class Tbody extends Tfoot
4
{
5
6
    protected static function jsonTbody()
7
    {
8
        $outColumns = array_column(self::$cols, 1);
9
10
        $json = [];
11
12
        if ((self::$t['rows'] = count(self::$data)) > 0) {
13
            foreach (self::$data as $r) {
14
                $rOut = [];
15
                foreach ($outColumns as $dbCol) {
16
                    $rOut[] = array_key_exists($dbCol, $r) ? $r[$dbCol] : '';
17
                }
18
                $json[] = $rOut;
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 rowsTbody()
28
    {
29
        $trs = '';
30
        foreach (self::jsonTbody() 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
        list($columns, $header) = self::exportColumnsAndHeader();
47
48
        $rows = [$header];
49
50
        $data = self::$exportDataAsDisplayed ?
51
            self::$data :
52
            self::select(self::$t['q']);
53
54
        if (count($data) > 0) {
55
            foreach ($data as $row) {
56
                $cells = [];
57
                foreach ($columns as $column) {
58
                    $cells[] = is_array($row[$column]) ?
59
                        $row[$column][0] :
60
                        $row[$column];
61
                }
62
                $rows[] = $cells;
63
            }
64
        }
65
66
        self::exportFile($rows);
67
    }
68
69
    private static function exportColumnsAndHeader()
70
    {
71
        $columns = $header = [];
72
        foreach (self::$cols as $c) {
73
            if (isset($c[2]['sort']) && $c[2]['sort'] === false) {
74
                continue;
75
            }
76
            $columns[] = $c[1];
77
            $header[] = $c[0];
78
        }
79
        return [$columns, $header];
80
    }
81
82
    private static function exportFile($eData)
83
    {
84
        $filename = self::exportFilename();
85
        switch (self::$export) {
86
            case 'Excel':
87
            case 'CSV':
88
                $escape = function ($value) {
89
                    return str_replace("\t", "&#9;", $value);
90
                };
91
                header('Content-Type:application/csv');
92
                header('Content-Disposition:attachment; filename=' .
93
                    $filename . '.csv');
94
95
                if (($output = fopen('php://output', 'w'))) {
96
                    foreach ($eData as $v) {
97
                        self::$export === 'CSV' ?
98
                                fputcsv($output, $v) :
99
                                fputs($output, implode("\t", array_map($escape, $v)) . "\r\n");
100
                    }
101
                    fclose($output);
102
                }
103
                break;
104
        }
105
    }
106
107
    private static function exportFilename()
108
    {
109
        $fnReplace = [];
110
        $fnReplace[':app'] = self::config('APP');
111
        $fnReplace[':items'] = self::$t['items'];
112
        $format = str_replace(':', '.', '%d.%m.%Y  %H:%i:%s');
113
        $timeQuery = 'SELECT DATE_FORMAT(Now(), "' . $format . '") AS `now`;';
114
        $fnReplace[':datetime'] = self::select($timeQuery);
115
        return strtr((string) self::config('EXPORT_FILE_NAME'), $fnReplace);
116
    }
117
}
118