1 | <?php |
||
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 | 4 | public function __construct(array $fields = null, bool $remember = false) |
|
46 | |||
47 | /** |
||
48 | * @param string $fields |
||
49 | */ |
||
50 | private function _initFields(string $fields) |
||
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 | 4 | protected function _afterInit() |
|
91 | |||
92 | /** |
||
93 | * @return Builder |
||
94 | */ |
||
95 | 3 | public function _shapeData(): Builder |
|
96 | { |
||
97 | 3 | 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 | 3 | 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') |
|
128 | |||
129 | /** |
||
130 | * Stop sorting by this column |
||
131 | * |
||
132 | * @param string $field |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function removeField(string $field) |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | public function render(): string |
||
153 | } |