Passed
Branchmaster (d3315d)
by Plamen
01:35
created

table_setter::err()   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
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
class table_setter
4
{
5
    /** @param array Columns (config) */
6
    public static $cols;
7
    /** @param bool Export flag (for current request) */
8
    public static $export;
9
    /** @param array Collector (values for current table call) */
10
    protected static $t = ['page' => 1, 'tables' => []];
11
    /** @param array Re-settable values for current table call */
12
    protected static $config;
13
    /** @param array Errors collector */
14
    protected static $errors = [];
15
16
    static function assets($path = "/public/table")
17
    {
18
        return "<script src=\"{$path}/table.js\"></script>\n\t" .
19
                "<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n";
20
    }
21
22
    /** Set/Get config value
23
     * @param mixed $c (string) Get if exists, (array) Set if valid
24
     * @return mixed */
25
    public static function config($c)
26
    {
27
        self::$config = self::$config ?: [
28
            'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI
29
            'EMPTY_TABLE_MSG' => 'No results found.',
30
            'EXPORT_FILE_NAME' => ':app - :items export (:datetime)',
31
            'FILTER_CASE_SENSITIVE' => false,
32
            'SAVES' => ['CSV', 'Excel'],
33
            'UTF8_ASC_SYMBOL' => '&#9650;',
34
            'UTF8_DESC_SYMBOL' => '&#9660;',
35
            'UTF8_LEFT_SYMBOL' => '&lsaquo;',
36
            'UTF8_RIGHT_SYMBOL' => '&rsaquo;'
37
        ];
38
39
        $getValid = function($k, $v = null) {
40
            if (!array_key_exists($k, self::$config)) {
41
                throw new Exception('Request to undefined value: ' . $k);
42
            }
43
44
            if ($v !== null) {
45
                if (empty($v) || gettype($v) !== gettype(self::$config[$k])) {
46
                    throw new Exception("Setting invalid value: $v (:$k)");
47
                }
48
            }
49
50
            return $v === null ? self::$config[$k] : $v;
51
        };
52
53
        try {
54
            switch (gettype($c)) {
55
                case 'array';
56
                    foreach ($c as $k => $v) {
57
                        self::$config[$k] = $getValid((string) $k, $v);
58
                    }
59
                    break;
60
                case 'string':
61
                    return $getValid($c);
62
                default:
63
                    throw new Exception("Invalid value type.");
64
            }
65
        } catch (Exception $e) {
66
            self::err('ERROR: ' . $e->getMessage());
67
        }
68
    }
69
70
    protected static function err($message = null)
71
    {
72
        if (isset($message)) {
73
            self::$errors[] = $message;
74
        } else {
75
            return empty(self::$errors) ? null :
76
            '<p class="tbl-err">' . implode('</br>', self::$errors) . '</p>';
77
        }
78
    }
79
80
    /** Converts array to space separated key value list
81
     * @param array $attributes
82
     * @return string */
83
    protected static function attributes(array $attributes)
84
    {
85
        $list = [];
86
        foreach ($attributes as $key => $value) {
87
            if (is_bool($value)) {
88
                if ((bool) $value) {
89
                    $list[] = $key;
90
                }
91
            } else if (empty($value) && !is_int($value)) {
92
                $list[] = $key;
93
            } else {
94
                $list[] = $key . '="' . $value . '"';
95
            }
96
        }
97
        return (count($list) > 0 ? ' ' : '') . join(' ', $list);
98
    }
99
100
    /** Parses view to string
101
     * @param string $template
102
     * @param array $vars
103
     * @return string */
104
    protected static function view($template, $vars = [])
105
    {
106
        extract($vars);
107
        ob_start();
108
        require $template;
109
        return (string) ob_get_clean();
110
    }
111
112
    protected static function filterValues(&$f, &$opts = [])
113
    {
114
        $f = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING) ?: null;
115
116
        $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
117
        foreach (self::$cols as $k => $v) {
118
            if (isset($v[2]['sort']) && $v[2]['sort'] === false) {
119
                continue;
120
            }
121
            if (empty($v)) {
122
                $v = [null];
123
            } // fix for column requested as []
124
            $selected = $by === $k ? ' selected' : null;
125
            $opts[] = "<option value=\"{$k}\"{$selected}>{$v[0]}</option>";
126
        }
127
    }
128
}
129