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
|
8 |
|
public function __construct(array $fields = null, bool $remember = false) |
28
|
|
|
{ |
29
|
8 |
|
$this->_rememberKey = 'sort'; |
30
|
8 |
|
$this->_rememberState = $remember; |
31
|
|
|
|
32
|
8 |
|
if ($fields !== null) |
33
|
|
|
{ |
34
|
5 |
|
foreach ($fields as $fieldName => $direction) |
35
|
|
|
{ |
36
|
5 |
|
if ($fieldName === 0) |
37
|
|
|
{ |
38
|
2 |
|
$fieldName = $direction; |
39
|
2 |
|
$direction = 'asc'; |
40
|
|
|
} |
41
|
|
|
|
42
|
5 |
|
$this->_sortFields[$fieldName] = \mb_strtolower($direction); |
43
|
|
|
} |
44
|
|
|
} |
45
|
8 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $fields |
49
|
|
|
*/ |
50
|
2 |
|
private function _initFields(string $fields) |
51
|
|
|
{ |
52
|
2 |
|
$this->_sortFields = []; |
53
|
2 |
|
foreach (\explode(self::COLUMN_SEPARATOR, $fields) as $field) |
54
|
|
|
{ |
55
|
2 |
|
$sortParts = \explode(self::SORTING_SEPARATOR, $field); |
56
|
2 |
|
if (\count($sortParts) === 1) |
57
|
|
|
{ |
58
|
1 |
|
$sortParts[1] = 'asc'; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
if ($sortParts[1] === 'none') |
62
|
|
|
{ |
63
|
1 |
|
SessionHelper::removeState($this->_request, $this->_rememberKey . '.' . $sortParts[0]); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$this->_sortFields[$sortParts[0]] = $sortParts[1]; |
67
|
|
|
} |
68
|
2 |
|
} |
69
|
|
|
|
70
|
1 |
|
protected function _readFromSession() |
71
|
|
|
{ |
72
|
1 |
|
$this->_sortFields = (array) SessionHelper::getState($this->_request, $this->_rememberKey, []); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
protected function _storeInSession() |
76
|
|
|
{ |
77
|
1 |
|
SessionHelper::saveState($this->_request, $this->_rememberKey, $this->_sortFields); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
8 |
|
protected function _afterInit() |
81
|
|
|
{ |
82
|
|
|
/** @var string $sortFields */ |
83
|
8 |
|
$sortFields = $this->_request->get('sort'); |
84
|
8 |
|
if (empty($sortFields)) |
85
|
|
|
{ |
86
|
6 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$this->_initFields($sortFields); |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Builder |
94
|
|
|
*/ |
95
|
7 |
|
public function _shapeData(): Builder |
96
|
|
|
{ |
97
|
7 |
|
if (\count($this->_sortFields) > 0) |
98
|
|
|
{ |
99
|
5 |
|
foreach ($this->_sortFields as $fieldName => $direction) |
100
|
|
|
{ |
101
|
5 |
|
if ($direction === 'none') |
102
|
|
|
{ |
103
|
1 |
|
$this->removeField($fieldName); |
104
|
1 |
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
$this->_queryBuilder->orderBy($fieldName, $direction); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
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
|
1 |
|
public function removeField(string $field) |
137
|
|
|
{ |
138
|
1 |
|
if (isset($this->_sortFields[$field])) |
139
|
|
|
{ |
140
|
1 |
|
unset($this->_sortFields[$field]); |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function render(): string |
150
|
|
|
{ |
151
|
|
|
return implode(', ', $this->_sortFields); |
152
|
|
|
} |
153
|
|
|
} |