Passed
Branchmaster (ea0496)
by Plamen
01:28
created

trait_request::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
trait trait_request
4
{
5
6
    protected static function request(&$export)
7
    {
8
        $export = self::set_export();
9
10
        $t = [
11
            'order' => [
12
                'dir' => self::order_dir(),
13
                'col' => self::order_col()
14
            ],
15
            'filter' => self::filter(),
16
            'page' => self::page()
17
        ];
18
19
        return array_merge(self::$t, $t);
20
    }
21
22
    private static function filter()
23
    {
24
        $filter = filter_input(INPUT_GET, 'filter') ?: false;
25
        if ($filter) {
26
            $by = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
27
            if ($by === false || is_null($by)) {
28
                $by = self::filter_by_all();
29
            } else {
30
                $by = self::$cols[$by][1];
31
            }
32
            $by = 'CONCAT(" ",' . $by . ', " ")';
33
            if (self::config('FILTER_CASE_SENSITIVE') !== true) {
34
                $by .= ' COLLATE ' . self::config('DB_COLLATION_CI');
35
            }
36
            $filter = $by . ' LIKE ' . '"%' . $filter . '%"';
37
        }
38
        return $filter;
39
    }
40
41
    private static function filter_by_all()
42
    {
43
        $by = [];
44
        foreach (self::$cols as $v) {
45
            if (isset($v[2]['sort']) && $v[2]['sort'] === false) {
46
                continue;
47
            }
48
            $by[] = 'IFNULL(' . $v[1] . ', "")';
49
        }
50
        return 'CONCAT(' . implode(',', $by) . ')';
51
    }
52
53
    private static function order_col()
54
    {
55
        if (($col = filter_input(INPUT_GET, 'col', FILTER_VALIDATE_INT))) {
56
            return isset(self::$cols[$col][2]['sort']) ?
57
                    self::$cols[$col][2]['sort'] :
58
                    self::$cols[$col][1];
59
        }
60
        return self::$t['order']['col'];
61
    }
62
63
    private static function order_dir()
64
    {
65
        $reset = filter_has_var(INPUT_GET, 'col') ? 'asc' : null;
66
        return in_array(filter_input(INPUT_GET, 'ord'), ['asc', 'desc']) ?
67
                filter_input(INPUT_GET, 'ord') :
68
                ($reset ?: self::$t['order']['dir']);
69
    }
70
71
    private static function set_export()
72
    {
73
        $exp = filter_input(INPUT_GET, 'export', FILTER_SANITIZE_STRING);
74
        return in_array($exp, self::config('SAVES')) ? $exp : false;
75
    }
76
77
    private static function page()
78
    {
79
        return filter_has_var(INPUT_GET, 'pg') && self::$export == false ?
80
                (int)filter_input(INPUT_GET, 'pg', FILTER_SANITIZE_NUMBER_INT) :
81
                self::$t['page'];
82
    }
83
}
84