|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models\DataComponents; |
|
4
|
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Facades\SessionHelper; |
|
6
|
|
|
use hamburgscleanest\DataTables\Models\DataComponent; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use function implode; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Sorter |
|
12
|
|
|
* @package hamburgscleanest\DataTables\Models\DataComponents |
|
13
|
|
|
*/ |
|
14
|
|
|
class Sorter extends DataComponent { |
|
15
|
|
|
|
|
16
|
|
|
const SORTING_SEPARATOR = '~'; |
|
17
|
|
|
const COLUMN_SEPARATOR = '.'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private $_sortFields = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Sorter constructor. |
|
24
|
|
|
* @param null|array $fields |
|
25
|
|
|
* @param bool $remember |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct(array $fields = null, bool $remember = false) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->_rememberKey = 'sort'; |
|
30
|
2 |
|
$this->_rememberState = $remember; |
|
31
|
|
|
|
|
32
|
2 |
|
if ($fields !== null) |
|
33
|
|
|
{ |
|
34
|
1 |
|
foreach ($fields as $fieldName => $direction) |
|
35
|
|
|
{ |
|
36
|
1 |
|
if ($fieldName === 0) |
|
37
|
|
|
{ |
|
38
|
|
|
$fieldName = $direction; |
|
39
|
|
|
$direction = 'asc'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$this->_sortFields[$fieldName] = \mb_strtolower($direction); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $fields |
|
49
|
|
|
*/ |
|
50
|
|
|
private function _initFields(string $fields) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->_sortFields = []; |
|
53
|
|
|
foreach (\explode(self::COLUMN_SEPARATOR, $fields) as $field) |
|
54
|
|
|
{ |
|
55
|
|
|
$sortParts = \explode(self::SORTING_SEPARATOR, $field); |
|
56
|
|
|
if (\count($sortParts) === 1) |
|
57
|
|
|
{ |
|
58
|
|
|
$sortParts[1] = 'asc'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($sortParts[1] === 'none') |
|
62
|
|
|
{ |
|
63
|
|
|
SessionHelper::removeState($this->_request, $this->_rememberKey . '.' . $sortParts[0]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->_sortFields[$sortParts[0]] = $sortParts[1]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function _readFromSession() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->_sortFields = (array) SessionHelper::getState($this->_request, $this->_rememberKey, []); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function _storeInSession() |
|
76
|
|
|
{ |
|
77
|
|
|
SessionHelper::saveState($this->_request, $this->_rememberKey, $this->_sortFields); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
protected function _afterInit() |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var string $sortFields */ |
|
83
|
2 |
|
$sortFields = $this->_request->get('sort'); |
|
84
|
2 |
|
if (empty($sortFields)) |
|
85
|
|
|
{ |
|
86
|
2 |
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->_initFields($sortFields); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return Builder |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public function _shapeData(): Builder |
|
96
|
|
|
{ |
|
97
|
2 |
|
if (\count($this->_sortFields) > 0) |
|
98
|
|
|
{ |
|
99
|
2 |
|
foreach ($this->_sortFields as $fieldName => $direction) |
|
100
|
|
|
{ |
|
101
|
2 |
|
if ($direction === 'none') |
|
102
|
|
|
{ |
|
103
|
|
|
$this->removeField($fieldName); |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
$this->_queryBuilder->orderBy($fieldName, $direction); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
return $this->_queryBuilder; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Sort by this column. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $field |
|
118
|
|
|
* @param string $direction |
|
119
|
|
|
* |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function addField(string $field, string $direction = 'asc') |
|
123
|
|
|
{ |
|
124
|
1 |
|
$this->_sortFields[$field] = \mb_strtolower($direction); |
|
125
|
|
|
|
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Stop sorting by this column |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $field |
|
133
|
|
|
* |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
|
|
public function removeField(string $field) |
|
137
|
|
|
{ |
|
138
|
|
|
if (isset($this->_sortFields[$field])) |
|
139
|
|
|
{ |
|
140
|
|
|
unset($this->_sortFields[$field]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function render(): string |
|
150
|
|
|
{ |
|
151
|
|
|
return implode(', ', $this->_sortFields); |
|
152
|
|
|
} |
|
153
|
|
|
} |