Passed
Push — master ( 59f31d...a30cf1 )
by Plamen
01:35
created

TraitRequest::orderCol()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
trait TraitRequest
4
{
5
6
    protected static function request(&$export)
7
    {
8
        $export = self::setExport();
9
10
        $add = [
11
            'order' => [
12
                'dir' => self::orderDir(),
13
                'col' => self::orderCol()
14
            ],
15
            'filter' => self::filter(),
16
            'page' => self::page()
17
        ];
18
19
        return array_merge(self::$t, $add);
20
    }
21
22
    private static function filter()
23
    {
24
        $filter = filter_input(INPUT_GET, 'filter') ?: false;
25
        if ($filter) {
26
            $filterBy = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
27
            if ($filterBy === false || is_null($filterBy)) {
28
                $filterBy = self::filterByAll();
29
            } else {
30
                $filterBy = self::$cols[$filterBy][1];
31
            }
32
            $filterBy = 'CONCAT(" ",' . $filterBy . ', " ")';
33
            if (self::config('FILTER_CASE_SENSITIVE') !== true) {
34
                $filterBy .= ' COLLATE ' . self::config('DB_COLLATION_CI');
35
            }
36
            $filter = $filterBy . ' LIKE ' . '"%' . $filter . '%"';
37
        }
38
        return $filter;
39
    }
40
41
    private static function filterByAll()
42
    {
43
        $all = [];
44
        foreach (self::$cols as $v) {
45
            if (isset($v[2]['sort']) && $v[2]['sort'] === false) {
46
                continue;
47
            }
48
            $all[] = 'IFNULL(' . $v[1] . ', "")';
49
        }
50
        return 'CONCAT(' . implode(',', $all) . ')';
51
    }
52
53
    private static function orderCol()
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 orderDir()
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 setExport()
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