Tfoot::select()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 11
rs 9.6111
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
29
            return [[[$html, ['colspans' => count(self::$cols)]]]];
30
        }
31
32
        return [];
33
    }
34
35
    private static function showingMessageVars(&$vars)
36
    {
37
        $pageNo = self::$t['page'];
38
        if ($pageNo === 1 && count(self::$data) < self::$t['paging']) {
39
            //Skips total count query
40
            self::$t['rows'] = count(self::$data);
41
        } else {
42
            $query = 'SELECT COUNT(*) FROM ('.self::$t['qAll'].') AS dt';
43
            self::$t['rows'] = self::select($query);
44
        }
45
        self::$t['pages'] = ceil(self::$t['rows'] / self::$t['paging']);
46
47
        $vars['from'] = ($pageNo - 1) * self::$t['paging'] + 1;
48
        $vars['upto'] = ($pageNo * self::$t['paging'] < self::$t['rows']) ?
49
            $pageNo * self::$t['paging'] :
50
            self::$t['rows'];
51
52
        $vars['total'] = self::$t['rows'];
53
        $vars['items'] = self::$t['items'];
54
    }
55
56
    protected static function rowsTfoot()
57
    {
58
        $trs = '';
59
        if (self::$t['rows'] > 0) {
60
            $ftr = self::jsonTfoot()[0][0];
61
            $trs .= '<tr><td'.self::attributes($ftr[1]).'>'
62
                .$ftr[0].'</td></tr>';
63
        } elseif (count(self::$errors) > 0) {
64
            $trs .= '<tr><td colspan="'.count(self::$cols).'">'.
65
                self::error().'</td></tr>';
66
        }
67
68
        return $trs;
69
    }
70
71
    public static function select(string $expression, array $bindings = [])
72
    {
73
        if (is_object(static::$select) && (static::$select instanceof Closure)) {
74
            $select = static::$select;
75
            $res = $select($expression, $bindings);
76
            //if result is single cell value ($res[0]->value), return the value
77
            return (count($res) === 1 && count((array) $res[0]) === 1) ?
78
                reset($res[0]) :
79
                $res;
80
        } else {
81
            throw new Exception('ERROR: table::$select is not a closure. ');
82
        }
83
    }
84
}
85