-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow updates, sorting for new row #714
Changes from 6 commits
334944f
48aeacc
8963341
762f5c6
99df206
ff2247c
3d341b7
56f9dff
457f78f
1296192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,13 @@ | |
|
||
namespace luya\admin\traits; | ||
|
||
use luya\admin\ngrest\base\NgRestModel; | ||
use Yii; | ||
use yii\base\Event; | ||
use yii\db\AfterSaveEvent; | ||
|
||
/** | ||
* Sortable Trait provides orderBy clause. | ||
* Sortable Trait provides orderBy clause and re-index when update, delete or create rows. | ||
* | ||
* By default the field `sortindex` is taken, change this by override the `sortableField` method. | ||
* | ||
|
@@ -23,6 +28,134 @@ | |
*/ | ||
trait SortableTrait | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* @since 4.4.0 | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
$this->on(NgRestModel::EVENT_AFTER_INSERT, [$this, 'newItemIndex']); | ||
$this->on(NgRestModel::EVENT_AFTER_UPDATE, [$this, 'updateItemIndex']); | ||
$this->on(NgRestModel::EVENT_AFTER_DELETE, [$this, 'deleteItemIndex']); | ||
} | ||
|
||
/** | ||
* Update the index when deleting an item | ||
* | ||
* @param Event $event | ||
* @since 4.4.0 | ||
*/ | ||
protected function deleteItemIndex(Event $event) | ||
{ | ||
$transaction = Yii::$app->db->beginTransaction(); | ||
try { | ||
|
||
$pkName = current($event->sender->primaryKey()); | ||
$this->reIndex($event, self::sortableField(), $pkName); | ||
$transaction->commit(); | ||
} catch (\Exception $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} catch (\Throwable $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* Update the index for a new item | ||
* | ||
* @param AfterSaveEvent $event | ||
* @since 4.4.0 | ||
*/ | ||
protected function newItemIndex(AfterSaveEvent $event) | ||
{ | ||
$this->updateItemIndex($event, true); | ||
} | ||
|
||
/** | ||
* Update the index for a given event. | ||
* | ||
* Either | ||
* - set the highest index available (if a row is created but no value has been given) | ||
* - swap index for high to low position | ||
* - swap index for low to hight position | ||
* | ||
* @param AfterSaveEvent $event | ||
* @param boolean $isNewRecord | ||
* @since 4.4.0 | ||
*/ | ||
protected function updateItemIndex(AfterSaveEvent $event, $isNewRecord = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
{ | ||
$attributeName = self::sortableField(); | ||
$oldPosition = array_key_exists($attributeName, $event->changedAttributes) ? $event->changedAttributes[$attributeName] : false; | ||
$newPosition = $event->sender[$attributeName]; | ||
|
||
// nothing has changed, skip further updates | ||
if ($oldPosition == $newPosition && !$isNewRecord) { | ||
return; | ||
} | ||
|
||
$transaction = Yii::$app->db->beginTransaction(); | ||
try { | ||
$pkName = current($event->sender->primaryKey()); | ||
// no index has been set, set max value (last position) | ||
if ($isNewRecord && empty($newPosition)) { | ||
$event->sender->updateAttributes([$attributeName => $event->sender::find()->max($attributeName) + 1]); | ||
} else if ($oldPosition && $newPosition && $oldPosition != $newPosition) { | ||
$i = 1; | ||
if ($newPosition > $oldPosition) { | ||
// when the new position is highter then the old one: (old position - 1) + *1 | ||
foreach ($event->sender->find()->andWhere(['and', ['!=', $pkName, $event->sender->primaryKey], ['>', $attributeName, $oldPosition], ['<=', $attributeName, $newPosition]])->all() as $item) { | ||
$item->updateAttributes([$attributeName => ($oldPosition - 1) + $i]); | ||
$i++; | ||
} | ||
} else { | ||
// when the new position is higher then the old one: (new position + *1) | ||
foreach ($event->sender->find()->andWhere(['and', ['!=', $pkName, $event->sender->primaryKey], ['>=', $attributeName, $newPosition], ['<', $attributeName, $oldPosition]])->all() as $item) { | ||
$item->updateAttributes([$attributeName => $newPosition + $i]); | ||
$i++; | ||
} | ||
} | ||
} else if (!empty($newPosition) && empty($oldPosition)) { | ||
// its a new record where the user entered a position, lets move all the other higher indexes | ||
$i = 1; | ||
foreach ($event->sender->find()->andWhere(['and', ['!=', $pkName, $event->sender->primaryKey], ['>=', $attributeName, $newPosition]])->all() as $item) { | ||
$item->updateAttributes([$attributeName => $newPosition + $i]); | ||
$i++; | ||
} | ||
} | ||
|
||
$this->reIndex($event, $attributeName, $pkName); | ||
$transaction->commit(); | ||
} catch (\Exception $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} catch (\Throwable $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* ReIndex the all items to ensure consistent numbers | ||
* | ||
* @param Event $event | ||
* @param string $attributeName | ||
* @param string $pkName | ||
* @since 4.4.0 | ||
*/ | ||
private function reIndex(Event $event, $attributeName, $pkName) | ||
{ | ||
$q = $event->sender->find()->asArray()->all(); | ||
$i = 1; | ||
foreach ($q as $item) { | ||
$event->sender->updateAll([$attributeName => $i], [$pkName => $item[$pkName]]); | ||
$i++; | ||
} | ||
} | ||
|
||
/** | ||
* The field which should by used to sort. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.