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