Passed
Branchmaster (1bd863)
by Plamen
01:24
created

tbody::jsonGetBodyData()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 20
rs 8.8333
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
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

55
        $outFilename = strtr(/** @scrutinizer ignore-type */ self::config('EXPORT_FILE_NAME'), $fnReplace);
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", "&#9;", $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