TraitRequest::page()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
trait TraitRequest
4
{
5
    protected static function request(&$export)
6
    {
7
        $export = self::setExport();
8
9
        $add = [
10
            'order' => [
11
                'dir' => self::orderDir(),
12
                'col' => self::orderCol(),
13
            ],
14
            'filter' => self::filter(),
15
            'page'   => self::page(),
16
        ];
17
18
        return array_merge(self::$t, $add);
19
    }
20
21
    private static function filter()
22
    {
23
        $filter = filter_input(INPUT_GET, 'filter') ?: false;
24
        if ($filter) {
25
            $filterBy = filter_input(INPUT_GET, 'filter-by', FILTER_VALIDATE_INT);
26
            if ($filterBy === false || is_null($filterBy)) {
27
                $filterBy = self::filterByAll();
28
            } else {
29
                $filterBy = self::$cols[$filterBy][1];
30
            }
31
            $filterBy = 'CONCAT(" ",'.$filterBy.', " ")';
32
            if (self::config('FILTER_CASE_SENSITIVE') !== true) {
33
                $filterBy .= ' COLLATE '.self::config('DB_COLLATION_CI');
34
            }
35
            $filter = $filterBy.' LIKE '.'"%'.$filter.'%"';
36
        }
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
51
        return 'CONCAT('.implode(',', $all).')';
52
    }
53
54
    private static function orderCol()
55
    {
56
        if (($col = filter_input(INPUT_GET, 'col', FILTER_VALIDATE_INT))) {
57
            return isset(self::$cols[$col][2]['sort']) ?
58
                self::$cols[$col][2]['sort'] :
59
                self::$cols[$col][1];
60
        }
61
62
        return self::$t['order']['col'];
63
    }
64
65
    private static function orderDir()
66
    {
67
        $reset = filter_has_var(INPUT_GET, 'col') ? 'asc' : null;
68
69
        return in_array(filter_input(INPUT_GET, 'ord'), ['asc', 'desc']) ?
70
            filter_input(INPUT_GET, 'ord') :
71
            ($reset ?: self::$t['order']['dir']);
72
    }
73
74
    private static function setExport()
75
    {
76
        $exp = filter_input(INPUT_GET, 'export', FILTER_SANITIZE_STRING) ?:
77
               filter_var(@$_GET['export'], FILTER_SANITIZE_STRING); //4 testing
78
79
        return in_array($exp, self::config('SAVES')) ? $exp : false;
80
    }
81
82
    private static function page()
83
    {
84
        return filter_has_var(INPUT_GET, 'pg') && self::$export == false ?
85
            (int) filter_input(INPUT_GET, 'pg', FILTER_SANITIZE_NUMBER_INT) :
86
            self::$t['page'];
87
    }
88
}
89