1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model\Behavior; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\RawValue; |
15
|
|
|
use Phalcon\Mvc\Model\Behavior; |
16
|
|
|
use Phalcon\Mvc\ModelInterface; |
17
|
|
|
use Phalcon\Text; |
18
|
|
|
use Zemit\Mvc\Model; |
19
|
|
|
|
20
|
|
|
class Position extends Behavior |
21
|
|
|
{ |
22
|
|
|
use SkippableTrait; |
23
|
|
|
|
24
|
|
|
public bool $progress = false; |
25
|
|
|
|
26
|
|
|
public function setField(string $field): void |
27
|
|
|
{ |
28
|
|
|
$this->options['field'] = $field; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getField(): string |
32
|
|
|
{ |
33
|
|
|
return $this->options['field']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setRawSql(bool $rawSql): void |
37
|
|
|
{ |
38
|
|
|
$this->options['rawSql'] = $rawSql; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getRawSql(): bool |
42
|
|
|
{ |
43
|
|
|
return $this->options['rawSql']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setProgress(bool $progress): void |
47
|
|
|
{ |
48
|
|
|
$this->progress = $progress; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getProgress(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->progress; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function __construct(array $options = []) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($options); |
59
|
|
|
$this->setField($options['field'] ?? 'position'); |
60
|
|
|
$this->setRawSql($options['rawSql'] ?? true); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function notify(string $type, ModelInterface $model) |
67
|
|
|
{ |
68
|
|
|
if (!$this->isEnabled()) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
assert($model instanceof Model); |
73
|
|
|
|
74
|
|
|
$positionField = $this->getField(); |
75
|
|
|
$rawSql = $this->getRawSql(); |
76
|
|
|
|
77
|
|
|
switch ($type) { |
78
|
|
|
case 'beforeValidation': |
79
|
|
|
// if position field is empty, force current max(position)+1 |
80
|
|
|
$lastPosition = $model::findFirst(['order' => $positionField . ' DESC']); |
81
|
|
|
if ($lastPosition && assert($lastPosition instanceof $model)) { |
82
|
|
|
$position = (int)$lastPosition->getAttribute($positionField); |
|
|
|
|
83
|
|
|
$model->setAttribute($positionField, $position + 1); |
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case 'afterSave': |
88
|
|
|
if (!$this->getProgress() && $model->hasSnapshotData() && $model->hasUpdated($positionField)) { |
89
|
|
|
$this->setProgress(true); |
90
|
|
|
|
91
|
|
|
$snapshot = $model->getOldSnapshotData() ?: $model->getSnapshotData(); |
92
|
|
|
$modelPosition = $model->getAttribute($positionField); |
93
|
|
|
$modelPrimaryKeys = $model->getPrimaryKeysValues(); |
94
|
|
|
|
95
|
|
|
if (!($modelPosition instanceof RawValue)) { |
96
|
|
|
$uField = Text::uncamelize($positionField); // @todo use columnMap |
97
|
|
|
$updatePositionQuery = null; |
98
|
|
|
|
99
|
|
|
if ($snapshot[$positionField] > $modelPosition) { |
100
|
|
|
$updatePositionQuery = $rawSql |
101
|
|
|
? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`+1 WHERE `' . $uField . '` >= :position and `' . $uField . '` < :oldPosition and `' . $idField . '` <> :id' |
|
|
|
|
102
|
|
|
: 'UPDATE [' . get_class($model) . '] SET [' . $positionField . '] = [' . $positionField . ']+1 WHERE [' . $positionField . '] >= ?1 and [' . $positionField . '] < ?2 and [' . $idField . '] <> ?0'; |
103
|
|
|
} |
104
|
|
|
elseif ($snapshot[$positionField] < $modelPosition) { |
105
|
|
|
$updatePositionQuery = $rawSql |
106
|
|
|
? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`-1 WHERE `' . $uField . '` > :oldPosition and `' . $uField . '` <= :position and `' . $idField . '` <> :id' |
107
|
|
|
: 'UPDATE [' . get_class($model) . '] SET [' . $positionField . '] = [' . $positionField . ']-1 WHERE [' . $positionField . '] > ?2 and [' . $positionField . '] <= ?1 and [' . $idField . '] <> ?0'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!empty($updatePositionQuery)) { |
111
|
|
|
if ($rawSql) { |
112
|
|
|
$model->getWriteConnection()->query($updatePositionQuery, [ |
113
|
|
|
'primaryKeys' => $modelPrimaryKeys, |
114
|
|
|
'position' => $modelPosition, |
115
|
|
|
'oldPosition' => $snapshot[$positionField], |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
else { |
119
|
|
|
$model->getModelsManager()->executeQuery($updatePositionQuery, [$modelId, $modelPosition, $snapshot[$positionField]]); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->setProgress(false); |
125
|
|
|
} |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|