Passed
Branchmaster (2c7806)
by Plamen
02:54
created

tfoot::select()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 11
rs 9.6111
1
<?php
2
3
class tfoot
4
{
5
6
    use trait_tfoot_setter,
7
        trait_tfoot_paging;
8
    /** @param string class name, where $helper::prepare() is expected */
9
    public static $helper_class;
10
    /** @param closure to MySql select function (example: db::select()) */
11
    public static $select;
12
13
    protected static function jsonTfoot()
14
    {
15
        if (count(self::$data) > 0) {
16
            $v = [];
17
            self::showingMessageVars($v);
18
            if (self::$exportActive === true) {
19
                $url = strtok(self::$t['slug'], "?&") . ".json?table=" .
20
                        self::$t['items'] . "&export=";
21
                $v["export"] = ["url" => $url, "types" => self::config('SAVES')];
22
            }
23
            $v['errors'] = self::err();
24
            self::paging($v);
25
            $html = self::view('views/table.footer.html', $v);
26
            $jsonArrFooter[] = [[$html, ["colspan" => count(self::$cols)]]];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$jsonArrFooter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jsonArrFooter = array(); before regardless.
Loading history...
27
        }
28
        return $jsonArrFooter ?: [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $jsonArrFooter does not seem to be defined for all execution paths leading up to this point.
Loading history...
29
    }
30
31
    private static function showingMessageVars(&$v)
32
    {
33
        $pageNo = self::$t['page'];
34
        if ($pageNo === 1 && count(self::$data) < self::$t['paging']) {
35
            //Skips total count query
36
            self::$t['rows'] = count(self::$data);
37
        } else {
38
            $q = 'SELECT COUNT(*) FROM (' . self::$t['qAll'] . ') AS dt';
39
            self::$t['rows'] = self::select($q);
40
        }
41
        self::$t['pages'] = ceil(self::$t['rows'] / self::$t['paging']);
42
43
        $v['from'] = ($pageNo - 1) * self::$t['paging'] + 1;
44
        $v['upto'] = ($pageNo * self::$t['paging'] < self::$t['rows']) ?
45
                $pageNo * self::$t['paging'] :
46
                self::$t['rows'];
47
48
        $v["total"] = self::$t['rows'];
49
        $v["items"] = self::$t['items'];
50
    }
51
52
    protected static function rowsTfoot()
53
    {
54
        $trs = '';
55
        if (self::$t['rows'] > 0) {
56
            $ftr = self::jsonTfoot()[0][0];
57
            $trs .= '<tr><td' . self::attributes($ftr[1]) . '>'
58
                    . $ftr[0] . '</td></tr>';
59
        } else if (count(self::$errors) > 0) {
60
            $trs .= '<tr><td colspan="' . count(self::$cols) . '">' .
61
                    self::err() . '</td></tr>';
62
        }
63
        return $trs;
64
    }
65
66
    public static function select(string $expression, array $bindings = [])
67
    {
68
        if (is_object(static::$select) && (static::$select instanceof Closure)) {
69
            $select = static::$select;
70
            $res = $select($expression, $bindings);
71
            //if result is single cell value ($res[0]->value), return the value
72
            return (count($res) === 1 && count((array) $res[0]) === 1) ?
73
                    reset($res[0]) :
74
                    $res;
75
        } else {
76
            throw new Exception('ERROR: table::$select is not a closure. ');
77
        }
78
    }
79
}
80