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

tfoot   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 14

3 Methods

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