From 41b9140ec2adafee8b7e1cb4447e96ea50605e54 Mon Sep 17 00:00:00 2001 From: markhuot Date: Sun, 8 Oct 2023 21:04:52 -0400 Subject: [PATCH] fixed sorting --- src/controllers/ComponentsController.php | 20 +++++++++++++++++--- src/models/Component.php | 6 ++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/controllers/ComponentsController.php b/src/controllers/ComponentsController.php index 38b9f82..f849706 100644 --- a/src/controllers/ComponentsController.php +++ b/src/controllers/ComponentsController.php @@ -112,13 +112,27 @@ public function actionMove() $target = Component::findOne(['id' => $targetId]); $position = $this->request->getRequiredBodyParam('position'); + // remove ourselves from the list + Component::updateAll([ + 'sortOrder' => new Expression('sortOrder - 1') + ], ['and', + ['=', 'elementId', $source->elementId], + ['=', 'fieldId', $source->fieldId], + ['path' => $source->path], + ['>', 'sortOrder', $source->sortOrder] + ]); + + // Refresh our target to get the updated/correct sortOrder + $target->refresh(); + + // make room for the insertion if ($position === 'above') { Component::updateAll([ 'sortOrder' => new Expression('sortOrder + 1') ], ['and', ['=', 'elementId', $target->elementId], ['=', 'fieldId', $target->fieldId], - ['=', 'path', $target->path], + ['path' => $target->path], ['>=', 'sortOrder', $target->sortOrder] ]); } @@ -129,8 +143,8 @@ public function actionMove() ], ['and', ['=', 'elementId', $target->elementId], ['=', 'fieldId', $target->fieldId], - ['=', 'path', $target->path], - ['>=', 'sortOrder', $target->sortOrder] + ['path' => $target->path], + ['>', 'sortOrder', $target->sortOrder] ]); } diff --git a/src/models/Component.php b/src/models/Component.php index ddbab42..b0f4a3e 100644 --- a/src/models/Component.php +++ b/src/models/Component.php @@ -77,9 +77,11 @@ public function rules(): array ]; } - public function setPath(string $path): void + public function setPath(?string $path): void { - $path = trim($path, '/'); + if (is_string($path)) { + $path = trim($path, '/'); + } if (empty($path)) { $path = null;