|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Services; |
|
4
|
|
|
|
|
5
|
|
|
class Sorter |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var array */ |
|
8
|
|
|
protected $sortable; |
|
9
|
|
|
|
|
10
|
|
|
/** @var null|string */ |
|
11
|
|
|
protected $element; |
|
12
|
|
|
|
|
13
|
|
|
/** @var null|string */ |
|
14
|
|
|
protected $direction; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Sorter constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $sortable |
|
20
|
|
|
* @param string $sortDir |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(array $sortable = [], string $sortDir = 'desc') |
|
23
|
|
|
{ |
|
24
|
|
|
$this->sortable = $sortable; |
|
25
|
|
|
|
|
26
|
|
|
$this->element = $this->input('sort_by', $this->first()); |
|
27
|
|
|
|
|
28
|
|
|
$this->direction = $this->input('sort_dir', $sortDir); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Build sortable url. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $element |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public function makeUrl(string $element) |
|
39
|
|
|
{ |
|
40
|
|
|
return \admin\helpers\qsRoute(null, [ |
|
41
|
|
|
'sort_by' => $element, |
|
42
|
|
|
'sort_dir' => $this->proposeDirection($element), |
|
43
|
|
|
]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get current sorting direction. |
|
48
|
|
|
* |
|
49
|
|
|
* @return null|string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function direction(): ?string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->direction; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get current sorting element. |
|
58
|
|
|
* |
|
59
|
|
|
* @return null|string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function element(): ?string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->element ?: $this->first(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check if a column is sortable. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $column |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function canSortBy(string $column): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return array_key_exists($column, $this->sortable) || \in_array($column, $this->sortable, true); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Retrieve first sortable element. |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function first() |
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($this->sortable as $key => $value) { |
|
86
|
|
|
if (is_numeric($key)) { |
|
87
|
|
|
return $value; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $key; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Propose new sort direction for element. |
|
98
|
|
|
* |
|
99
|
|
|
* @param $forElement |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function proposeDirection($forElement) |
|
104
|
|
|
{ |
|
105
|
|
|
$sortDir = $this->direction(); |
|
106
|
|
|
|
|
107
|
|
|
return $forElement === $this->element() ? $this->reverseDirection($sortDir) : $sortDir; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Reverse sorting direction. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $direction |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function reverseDirection(string $direction) |
|
118
|
|
|
{ |
|
119
|
|
|
return 'asc' === strtolower($direction) ? 'desc' : 'asc'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param $key |
|
124
|
|
|
* @param null $default |
|
|
|
|
|
|
125
|
|
|
* |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function input($key, $default = null) |
|
129
|
|
|
{ |
|
130
|
|
|
return app('request')->input($key, $default); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|