Passed
Push — master ( af185e...b32849 )
by Plamen
01:30
created

trait_helper::error()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
trait trait_helper
4
{
5
    /** @param array Columns (config) */
6
    public static $cols;
7
    /** @param array Query result, allows altering before table::load() */
8
    public static $data = [];
9
    /** @param bool Export flag (for current request) */
10
    public static $export;
11
    /** @param bool Export is allowed (shows buttons in the tfoot) */
12
    public static $exportActive = true;
13
    /** @param bool Export data altered (use false for pure result) */
14
    public static $exportDataAsDisplayed = true;
15
    /** @param array Errors collector */
16
    protected static $errors = [];
17
    /** @param array Collector (values for current table call) */
18
    protected static $t = ['page' => 1, 'tables' => []];
19
20
    public static function assets($path = "/public/table")
21
    {
22
        return "<script src=\"{$path}/table_helper.js\" defer></script>\n\t" .
23
            "<script src=\"{$path}/table.js\" defer></script>\n\t" .
24
            "<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n";
25
    }
26
27
    /** Array to space separated key value list
28
     * @param array $attributes
29
     * @return string */
30
    protected static function attributes(array $attributes)
31
    {
32
        $list = [' '];
33
        foreach ($attributes as $key => $value) {
34
            if ($value === true || (empty($value) && $value != 0)) {
35
                $list[] = $key;
36
            } else {
37
                $list[] = $key . '="' . $value . '"';
38
            }
39
        }
40
        return rtrim(implode(' ', $list));
41
    }
42
43
    /** Parses view to string
44
     * @param string    $template
45
     * @param array     $vars
46
     * @return string */
47
    protected static function view($template, $vars = [])
48
    {
49
        extract($vars);
50
        ob_start();
51
        require $template;
52
        return (string) ob_get_clean();
53
    }
54
55
    /** Needed for more than one table on page
56
     * @param string $items */
57
    protected static function reset($items)
58
    {
59
        if (!in_array($items, self::$t["tables"])) {
60
            self::$t["tables"][] = $items;
61
            self::$cols = [];
62
            self::$t['rows'] = null;
63
        } else {
64
            echo 'Existing table-id used in table::create(): ' . $items;
65
        }
66
    }
67
}
68