1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Behavior for Yii2 to support sorting in ActiveRecord models |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/inblank/yii2-sortable |
6
|
|
|
* @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace inblank\sortable; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Behavior; |
13
|
|
|
use yii\db\ActiveRecord; |
14
|
|
|
use yii\db\Expression; |
15
|
|
|
use yii\db\QueryBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Behavior for Yii2 to support sorting in ActiveRecord models |
19
|
|
|
* |
20
|
|
|
* @property ActiveRecord $owner |
21
|
|
|
*/ |
22
|
|
|
class SortableBehavior extends Behavior |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Attribute to store the sort order of records |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $sortAttribute = 'sort'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The list of attributes used as sorting conditions of the records. |
32
|
|
|
* null mean no condition and sort all records |
33
|
|
|
* One attribute can be define as string. |
34
|
|
|
* @var array|string |
35
|
|
|
*/ |
36
|
|
|
public $conditionAttributes = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Maximal sort index |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $_max; |
43
|
|
|
/** |
44
|
|
|
* Rendered sorting position |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $_condition; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
8 |
|
public function events() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
8 |
|
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate', |
56
|
8 |
|
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', |
57
|
8 |
|
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** Before validate event */ |
62
|
3 |
|
public function beforeValidate() |
63
|
|
|
{ |
64
|
3 |
|
if ($this->owner->getIsNewRecord()) { |
65
|
|
|
// added new record always on top |
66
|
1 |
|
$this->owner->{$this->sortAttribute} = $this->owner->find()->andWhere($this->getCondition())->count(); |
67
|
|
|
} else { |
68
|
|
|
// prevent direct change of sort field |
69
|
2 |
|
$this->owner->{$this->sortAttribute} = $this->owner->getOldAttribute($this->sortAttribute); |
70
|
|
|
} |
71
|
3 |
|
} |
72
|
|
|
|
73
|
|
|
/** After update event */ |
74
|
2 |
|
public function afterUpdate($event) |
75
|
|
|
{ |
76
|
2 |
|
$oldCondition = []; |
77
|
2 |
|
$currentCondition = []; |
78
|
|
|
/** @var ActiveRecord $owner */ |
79
|
2 |
|
$owner = $this->owner; |
80
|
2 |
|
foreach ((array)$this->conditionAttributes as $attribute) { |
81
|
2 |
|
if (!$owner->hasAttribute($attribute)) { |
82
|
1 |
|
continue; |
83
|
|
|
} |
84
|
2 |
|
$oldCondition[$attribute] = $currentCondition[$attribute] = $owner->getAttribute($attribute); |
85
|
2 |
|
if (array_key_exists($attribute, $event->changedAttributes)) { |
86
|
2 |
|
$oldCondition[$attribute] = $event->changedAttributes[$attribute]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
2 |
|
if (array_diff($currentCondition, $oldCondition) != []) { |
90
|
|
|
// condition was changed |
91
|
|
|
/** @var ActiveRecord $changedModel */ |
92
|
1 |
|
$changedModel = Yii::createObject($owner->className()); |
93
|
1 |
|
$changedModel->setAttributes($oldCondition, false); |
94
|
|
|
// recalculate old model range |
95
|
1 |
|
$changedModel->recalculateSort(); |
96
|
|
|
// move model to top in new model range |
97
|
1 |
|
$owner->updateAttributes([ |
98
|
1 |
|
$this->sortAttribute => $owner->find()->andWhere($this->getCondition())->count() - 1 |
99
|
|
|
]); |
100
|
|
|
} |
101
|
2 |
|
} |
102
|
|
|
|
103
|
|
|
/** After delete event */ |
104
|
1 |
|
public function afterDelete() |
105
|
|
|
{ |
106
|
1 |
|
$owner = $this->owner; |
107
|
1 |
|
$condition = $this->getCondition(); |
108
|
1 |
|
$condition[] = ['>', $this->sortAttribute, $owner->{$this->sortAttribute}]; |
109
|
1 |
|
$owner->updateAllCounters([$this->sortAttribute => -1], $condition); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Move record to the specific sorting position |
114
|
|
|
* @param int $position new sorting position |
115
|
|
|
*/ |
116
|
4 |
|
public function moveToPosition($position) |
117
|
|
|
{ |
118
|
4 |
|
$this->sortChange($position - $this->owner->{$this->sortAttribute}); |
119
|
4 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Move record to the top of sorting order |
123
|
|
|
*/ |
124
|
1 |
|
public function moveToTop() |
125
|
|
|
{ |
126
|
1 |
|
$this->moveToPosition(PHP_INT_MAX); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Move record to the bottom of sorting order |
131
|
|
|
*/ |
132
|
2 |
|
public function moveToBottom() |
133
|
|
|
{ |
134
|
2 |
|
$this->moveToPosition(0); |
135
|
2 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Move records |
139
|
|
|
* @param int $value |
140
|
|
|
*/ |
141
|
5 |
|
public function sortChange($value) |
142
|
|
|
{ |
143
|
5 |
|
if ($value == 0) { |
144
|
1 |
|
return; |
145
|
|
|
} |
146
|
5 |
|
$owner = $this->owner; |
147
|
5 |
|
$condition = $this->getCondition(); |
148
|
5 |
|
$newSort = $owner->{$this->sortAttribute} + $value; |
149
|
5 |
|
if ($value > 0) { |
150
|
|
|
// move up |
151
|
4 |
|
$max = $this->getMaxSort(); |
152
|
4 |
|
if ($owner->{$this->sortAttribute} === $max) { |
153
|
1 |
|
return; |
154
|
|
|
} |
155
|
3 |
|
if ($newSort >= $max) { |
156
|
2 |
|
$newSort = $max; |
157
|
|
|
} |
158
|
3 |
|
$condition[] = [">", $this->sortAttribute, $owner->{$this->sortAttribute}]; |
159
|
3 |
|
$condition[] = ["<=", $this->sortAttribute, $newSort]; |
160
|
3 |
|
$counterChanger = -1; |
161
|
|
|
} else { |
162
|
|
|
// move down |
163
|
4 |
|
if ($owner->{$this->sortAttribute} === 0) { |
164
|
1 |
|
return; |
165
|
|
|
} |
166
|
4 |
|
if ($newSort < 0) { |
167
|
1 |
|
$newSort = 0; |
168
|
|
|
} |
169
|
4 |
|
$condition[] = ['<', $this->sortAttribute, $owner->{$this->sortAttribute}]; |
170
|
4 |
|
$condition[] = ['>=', $this->sortAttribute, $newSort]; |
171
|
4 |
|
$counterChanger = 1; |
172
|
|
|
} |
173
|
5 |
|
$owner->updateAllCounters([$this->sortAttribute => $counterChanger], $condition); |
174
|
5 |
|
$owner->updateAttributes([$this->sortAttribute => $newSort]); |
175
|
5 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Recalculate sorting |
179
|
|
|
*/ |
180
|
2 |
|
public function recalculateSort() |
181
|
|
|
{ |
182
|
2 |
|
$owner = $this->owner; |
183
|
2 |
|
$db = $this->owner->getDb(); |
184
|
2 |
|
$builder = new QueryBuilder($db); |
185
|
|
|
|
186
|
2 |
|
$orderFields = [$this->sortAttribute => 'asc']; |
187
|
2 |
|
foreach ($owner->primaryKey() as $field) { |
188
|
2 |
|
if ($field != $this->sortAttribute) { |
189
|
2 |
|
$orderFields[$field] = 'asc'; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
// recalculate sort |
193
|
2 |
|
$query = $builder->update( |
194
|
2 |
|
$owner->tableName(), |
195
|
2 |
|
[$this->sortAttribute => new Expression('(@sortingCount:=(@sortingCount+1))')], |
196
|
2 |
|
$this->getCondition(), |
197
|
|
|
$params |
198
|
2 |
|
) . ' ' . $builder->buildOrderBy($orderFields); |
199
|
2 |
|
$db->createCommand('set @sortingCount=-1;' . $query, $params)->execute(); |
200
|
|
|
// update in current record |
201
|
2 |
|
if (!$owner->getIsNewRecord()) { |
202
|
1 |
|
$owner->{$this->sortAttribute} = $owner->findOne($owner->getPrimaryKey())->{$this->sortAttribute}; |
203
|
|
|
} |
204
|
2 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get maximal sort index |
208
|
|
|
*/ |
209
|
4 |
|
public function getMaxSort() |
210
|
|
|
{ |
211
|
4 |
|
if ($this->_max === null) { |
212
|
4 |
|
$this->_max = $this->owner->find()->andWhere($this->getCondition())->count() - 1; |
|
|
|
|
213
|
|
|
} |
214
|
4 |
|
return $this->_max; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get WHERE condition for sort change query |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
8 |
|
protected function getCondition() |
222
|
|
|
{ |
223
|
8 |
|
$condition = ['and']; |
224
|
8 |
|
foreach ((array)$this->conditionAttributes as $attribute) { |
225
|
8 |
|
if ($this->owner->hasAttribute($attribute)) { |
226
|
8 |
|
$condition[] = [$attribute => $this->owner->$attribute]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
8 |
|
return $condition; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Move model relative other |
234
|
|
|
* @param ActiveRecord $model model to move |
235
|
|
|
* @param string $position moving position: after or before |
236
|
|
|
*/ |
237
|
|
|
public function relativeMove($model, $position) |
238
|
|
|
{ |
239
|
|
|
$conditionAttributes = (array)$this->conditionAttributes; |
240
|
|
|
$owner = $this->owner; |
241
|
|
|
|
242
|
|
|
if (!empty($conditionAttributes)) { |
243
|
|
|
$sameCondition = true; |
244
|
|
|
foreach ($conditionAttributes as $attr) { |
245
|
|
|
if ($owner->getAttribute($attr) != $model->getAttribute($attr)) { |
246
|
|
|
$sameCondition = false; |
247
|
|
|
break; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
if (!$sameCondition) { |
251
|
|
|
// move in other condition category |
252
|
|
|
$this->moveToTop(); |
253
|
|
|
// update condition attribute |
254
|
|
|
$condition = []; |
255
|
|
|
foreach ($conditionAttributes as $attr) { |
256
|
|
|
$condition[$attr] = $model->getAttribute($attr); |
257
|
|
|
} |
258
|
|
|
$condition[$this->sortAttribute] = $owner->find()->andWhere($this->getCondition())->count() - 1; |
259
|
|
|
$owner->updateAttributes($condition); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
// calculate pos change |
263
|
|
|
$currentPos = $owner->getAttribute($this->sortAttribute); |
264
|
|
|
$destinationPos = $model->getAttribute($this->sortAttribute); |
265
|
|
|
if ($position == 'after') { |
266
|
|
|
$newPos = $destinationPos > $currentPos ? $destinationPos - 1 : $destinationPos; |
267
|
|
|
} else { |
268
|
|
|
$newPos = $destinationPos > $currentPos ? $destinationPos : $destinationPos + 1; |
269
|
|
|
} |
270
|
|
|
$this->moveToPosition($newPos); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Move current model after specified |
275
|
|
|
* @param ActiveRecord $model |
276
|
|
|
*/ |
277
|
|
|
public function moveAfter($model) |
278
|
|
|
{ |
279
|
|
|
$this->relativeMove($model, 'after'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Move current model before specified |
284
|
|
|
* @param ActiveRecord $model |
285
|
|
|
*/ |
286
|
|
|
public function moveBefore($model) |
287
|
|
|
{ |
288
|
|
|
$this->relativeMove($model, 'before'); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.