Passed
Branchmaster (b3b9b8)
by Plamen
01:30
created

tbody.php (1 issue)

Labels
Severity
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
        return $json;
24
    }
25
    
26
    protected static function rowsTbody()
27
    {
28
        $trs = '';
29
        foreach (self::jsonTbody() as $r) {
30
            if (isset($r[0][1], $r[0][1]['class']) &&
31
                    $r[0][1]['class'] === 'no-results'
32
            ) {
33
                $trs .= '<tr><td' . self::attributes($r[0][1]) . '>'
34
                        . $r[0][0] . '</td></tr>';
35
            } else {
36
                $trs .= '<tr><td>' . implode('</td><td>', $r)
37
                        . '</td></tr>';
38
            }
39
        }
40
        return $trs;
41
    }
42
43
    protected static function export()
44
    {
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($v) {
89
                    return str_replace("\t", "&#9;", $v);
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(self::config('EXPORT_FILE_NAME'), $fnReplace);
0 ignored issues
show
It seems like self::config('EXPORT_FILE_NAME') can also be of type null; however, parameter $str of strtr() 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 ignore-type  annotation

115
        return strtr(/** @scrutinizer ignore-type */ self::config('EXPORT_FILE_NAME'), $fnReplace);
Loading history...
116
    }
117
}
118