Issues (3)

Tbody.php (3 issues)

1
<?php
2
3
class Tbody extends Tfoot
4
{
5
    protected static function jsonTbody()
6
    {
7
        $outColumns = array_column(self::$cols, 1);
8
9
        $json = [];
10
11
        if ((self::$t['rows'] = count(self::$data)) > 0) {
12
            foreach (self::$data as $r) {
13
                $rOut = [];
14
                foreach ($outColumns as $dbCol) {
15
                    $rOut[] = array_key_exists($dbCol, $r) ? $r[$dbCol] : '';
16
                }
17
                $json[] = $rOut;
18
            }
19
        } else {
20
            $attr = ['colspan' => count(self::$cols), 'class' => 'no-results'];
21
            $json[] = [[self::config('EMPTY_TABLE_MSG'), $attr]];
22
        }
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
42
        return $trs;
43
    }
44
45
    protected static function export()
46
    {
47
        list($columns, $header) = self::exportColumnsAndHeader();
48
49
        $rows = [$header];
50
51
        $data = self::$exportDataAsDisplayed ?
52
            self::$data :
53
            self::select(self::$t['q']);
54
55
        if (count($data) > 0) {
56
            foreach ($data as $row) {
57
                $cells = [];
58
                foreach ($columns as $column) {
59
                    $cells[] = is_array($row[$column]) ?
60
                        $row[$column][0] :
61
                        $row[$column];
62
                }
63
                $rows[] = $cells;
64
            }
65
        }
66
67
        return self::exportFile($rows);
68
    }
69
70
    private static function exportColumnsAndHeader()
71
    {
72
        $columns = $header = [];
73
        foreach (self::$cols as $c) {
74
            if (isset($c[2]['sort']) && $c[2]['sort'] === false) {
75
                continue;
76
            }
77
            $columns[] = $c[1];
78
            $header[] = $c[0];
79
        }
80
81
        return [$columns, $header];
82
    }
83
84
    private static function exportFile($eData)
85
    {
86
        $filename = self::exportFilename();
87
        switch (self::$export) {
88
            case 'Excel':
89
            case 'CSV':
90
                $escape = function ($value) {
91
                    return str_replace("\t", '&#9;', $value);
92
                };
93
94
                $output = fopen('php://output', 'w');
95
                if (substr($filename, -2) !== '()') { //4 testing
96
                    header('Content-Type:application/csv');
97
                    header('Content-Disposition:attachment; filename='.
98
                            $filename.'.csv');
99
                }
100
                foreach ($eData as $v) {
101
                    self::$export === 'CSV' ?
102
                            fputcsv($output, $v) :
0 ignored issues
show
It seems like $output can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, 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 ignore-type  annotation

102
                            fputcsv(/** @scrutinizer ignore-type */ $output, $v) :
Loading history...
103
                            fwrite($output,
0 ignored issues
show
It seems like $output can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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 ignore-type  annotation

103
                            fwrite(/** @scrutinizer ignore-type */ $output,
Loading history...
104
                                implode("\t", array_map($escape, $v))."\r\n");
105
                }
106
107
                return fclose($output);
0 ignored issues
show
It seems like $output can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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 ignore-type  annotation

107
                return fclose(/** @scrutinizer ignore-type */ $output);
Loading history...
108
        }
109
    }
110
111
    private static function exportFilename()
112
    {
113
        $fnReplace = [];
114
        $fnReplace[':app'] = self::config('APP');
115
        $fnReplace[':items'] = self::$t['items'];
116
        $format = str_replace(':', '.', '%d.%m.%Y  %H:%i:%s');
117
        $timeQuery = 'SELECT DATE_FORMAT(Now(), "'.$format.'") AS `now`;';
118
        $fnReplace[':datetime'] = self::select($timeQuery);
119
120
        return strtr((string) self::config('EXPORT_FILE_NAME'), $fnReplace);
121
    }
122
}
123