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 ProgressTrait; |
23
|
|
|
use SkippableTrait; |
24
|
|
|
|
25
|
|
|
public bool $progress = false; |
26
|
|
|
|
27
|
|
|
public function setField(string $field): void |
28
|
|
|
{ |
29
|
|
|
$this->options['field'] = $field; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getField(): string |
33
|
|
|
{ |
34
|
|
|
return $this->options['field']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setRawSql(bool $rawSql): void |
38
|
|
|
{ |
39
|
|
|
$this->options['rawSql'] = $rawSql; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getRawSql(): bool |
43
|
|
|
{ |
44
|
|
|
return $this->options['rawSql']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function hasProperty(ModelInterface $model, string $field): bool |
48
|
|
|
{ |
49
|
|
|
return property_exists($model, $field); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function __construct(array $options = []) |
53
|
|
|
{ |
54
|
|
|
parent::__construct($options); |
55
|
|
|
$this->setField($options['field'] ?? 'position'); |
56
|
|
|
$this->setRawSql($options['rawSql'] ?? true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the default position field value before validation |
61
|
|
|
* Shift position+1 and position-1 to other records after save |
62
|
|
|
*/ |
63
|
|
|
public function notify(string $type, ModelInterface $model) |
64
|
|
|
{ |
65
|
|
|
if (!$this->isEnabled()) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$field = $this->getField(); |
70
|
|
|
$rawSql = $this->getRawSql(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// skip if the current model doesn't have the position property defined |
73
|
|
|
if (!$this->hasProperty($model, $field)) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
switch ($type) { |
78
|
|
|
case 'beforeValidation': |
79
|
|
|
$this->beforeValidation($model, $field); |
80
|
|
|
break; |
81
|
|
|
|
82
|
|
|
case 'afterSave': |
83
|
|
|
// $this->afterSave($model, $field, $rawSql); |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Force the current position to max(position)+1 if it's empty |
92
|
|
|
* will only happen if the position field is present on the current model |
93
|
|
|
*/ |
94
|
|
|
public function beforeValidation(ModelInterface $model, string $field): void |
95
|
|
|
{ |
96
|
|
|
if (property_exists($model, $field)) { |
97
|
|
|
$positionValue = $model->readAttribute($field); |
|
|
|
|
98
|
|
|
if (is_null($positionValue)) { |
99
|
|
|
|
100
|
|
|
// if position field is empty, force current max(position)+1 |
101
|
|
|
$lastPosition = $model::findFirst(['order' => $field . ' DESC']); |
102
|
|
|
if ($lastPosition && assert($lastPosition instanceof $model)) { |
103
|
|
|
$position = (int)$lastPosition->readAttribute($field); |
104
|
|
|
$model->writeAttribute($field, $position + 1); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// @todo fix combined primary keys |
111
|
|
|
public function afterSave(ModelInterface $model, string $field, bool $rawSql): void |
112
|
|
|
{ |
113
|
|
|
if (!$this->getProgress() && $model->hasSnapshotData() && $model->hasUpdated($field)) { |
|
|
|
|
114
|
|
|
self::staticStart(); |
115
|
|
|
|
116
|
|
|
$snapshot = $model->getOldSnapshotData() ?: $model->getSnapshotData(); |
|
|
|
|
117
|
|
|
$modelPosition = $model->readAttribute($field); |
118
|
|
|
$modelPrimaryKeys = $model->getPrimaryKeysValues(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
if (!($modelPosition instanceof RawValue)) { |
121
|
|
|
$uField = Text::uncamelize($field); // @todo use columnMap |
122
|
|
|
$updatePositionQuery = null; |
123
|
|
|
|
124
|
|
|
if ($snapshot[$field] > $modelPosition) { |
125
|
|
|
$updatePositionQuery = $rawSql |
126
|
|
|
? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`+1 WHERE `' . $uField . '` >= :position and `' . $uField . '` < :oldPosition and `' . $idField . '` <> :id' |
|
|
|
|
127
|
|
|
: 'UPDATE [' . get_class($model) . '] SET [' . $field . '] = [' . $field . ']+1 WHERE [' . $field . '] >= ?1 and [' . $field . '] < ?2 and [' . $idField . '] <> ?0'; |
128
|
|
|
} |
129
|
|
|
elseif ($snapshot[$field] < $modelPosition) { |
130
|
|
|
$updatePositionQuery = $rawSql |
131
|
|
|
? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`-1 WHERE `' . $uField . '` > :oldPosition and `' . $uField . '` <= :position and `' . $idField . '` <> :id' |
132
|
|
|
: 'UPDATE [' . get_class($model) . '] SET [' . $field . '] = [' . $field . ']-1 WHERE [' . $field . '] > ?2 and [' . $field . '] <= ?1 and [' . $idField . '] <> ?0'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!empty($updatePositionQuery)) { |
136
|
|
|
if ($rawSql) { |
137
|
|
|
$model->getWriteConnection()->query($updatePositionQuery, [ |
138
|
|
|
'primaryKeys' => $modelPrimaryKeys, |
139
|
|
|
'position' => $modelPosition, |
140
|
|
|
'oldPosition' => $snapshot[$field], |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
else { |
144
|
|
|
$model->getModelsManager()->executeQuery($updatePositionQuery, [$modelId, $modelPosition, $snapshot[$field]]); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
self::staticStop(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|