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

Tfoot::showingMessageVars()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.8333
1
<?php
2
3
class Tfoot
4
{
5
6
    use trait_config,
7
        trait_helper,
8
        trait_paging,
9
        trait_query;
10
    /** @param string class name, where $helper::prepare() is expected */
11
    public static $helperClass;
12
    /** @param closure to MySql select function (example: db::select()) */
13
    public static $select;
14
15
    protected static function jsonTfoot()
16
    {
17
        if (count(self::$data) > 0) {
18
            $vars = [];
19
            self::showingMessageVars($vars);
20
            if (self::$exportActive === true) {
21
                $url = strtok(self::$t['slug'], '?&') . '.json?table=' .
22
                    self::$t['items'] . '&export=';
23
                $vars['export']['url'] = $url;
24
                $vars['export']['types'] = self::config('SAVES');
25
            }
26
            $vars['errors'] = self::error();
27
            self::paging($vars);
28
            $html = self::view('views/table.footer.html', $vars);
29
            return [[[$html, ["colspan" => count(self::$cols)]]]];
30
        }
31
        return [];
32
    }
33
34
    private static function showingMessageVars(&$vars)
35
    {
36
        $pageNo = self::$t['page'];
37
        if ($pageNo === 1 && count(self::$data) < self::$t['paging']) {
38
            //Skips total count query
39
            self::$t['rows'] = count(self::$data);
40
        } else {
41
            $query = 'SELECT COUNT(*) FROM (' . self::$t['qAll'] . ') AS dt';
42
            self::$t['rows'] = self::select($query);
43
        }
44
        self::$t['pages'] = ceil(self::$t['rows'] / self::$t['paging']);
45
46
        $vars['from'] = ($pageNo - 1) * self::$t['paging'] + 1;
47
        $vars['upto'] = ($pageNo * self::$t['paging'] < self::$t['rows']) ?
48
            $pageNo * self::$t['paging'] :
49
            self::$t['rows'];
50
51
        $vars["total"] = self::$t['rows'];
52
        $vars["items"] = self::$t['items'];
53
    }
54
55
    protected static function rowsTfoot()
56
    {
57
        $trs = '';
58
        if (self::$t['rows'] > 0) {
59
            $ftr = self::jsonTfoot()[0][0];
60
            $trs .= '<tr><td' . self::attributes($ftr[1]) . '>'
61
                . $ftr[0] . '</td></tr>';
62
        } elseif (count(self::$errors) > 0) {
63
            $trs .= '<tr><td colspan="' . count(self::$cols) . '">' .
64
                self::error() . '</td></tr>';
65
        }
66
        return $trs;
67
    }
68
69
    public static function select(string $expression, array $bindings = [])
70
    {
71
        if (is_object(static::$select) && (static::$select instanceof Closure)) {
72
            $select = static::$select;
73
            $res = $select($expression, $bindings);
74
            //if result is single cell value ($res[0]->value), return the value
75
            return (count($res) === 1 && count((array) $res[0]) === 1) ?
76
                reset($res[0]) :
77
                $res;
78
        } else {
79
            throw new Exception('ERROR: table::$select is not a closure. ');
80
        }
81
    }
82
}
83