-
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 all 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. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace admintests\admin\ngrest\plugins; | ||
|
||
use admintests\AdminTestCase; | ||
use luya\admin\ngrest\plugins\Sortable; | ||
|
||
class SortableTest extends AdminTestCase | ||
{ | ||
public function testSortableRenderList() | ||
{ | ||
$renderList = new Sortable([ | ||
'alias' => 'slug', | ||
'name' => 'slug', | ||
'i18n' => true, | ||
]); | ||
|
||
$html = $renderList->renderList(1,'foobar'); | ||
$this->assertNotEmpty($html); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace admintests\admin\traits; | ||
|
||
use admintests\AdminModelTestCase; | ||
use admintests\AdminTestCase; | ||
use admintests\data\fixtures\UserFixture; | ||
use luya\admin\models\Group; | ||
use luya\admin\models\User; | ||
use luya\admin\models\UserOnline; | ||
use luya\admin\traits\SortableTrait; | ||
use luya\testsuite\fixtures\NgRestModelFixture; | ||
|
||
class SortableTraitTest extends AdminModelTestCase | ||
{ | ||
public function testSorting() | ||
{ | ||
new NgRestModelFixture([ | ||
'modelClass' => UserStub::class, | ||
'fixtureData' => [ | ||
'user1' => [ | ||
'id' => 1, | ||
'title' => 1, | ||
'firstname' => 'John', | ||
'lastname' => 'Doe', | ||
'email' => 'john@luya.io', | ||
'password' => 'nohash', | ||
'is_deleted' => 0, | ||
'is_api_user' => 0, | ||
], | ||
'user2' => [ | ||
'id' => 2, | ||
'title' => 3, | ||
'firstname' => 'Jane', | ||
'lastname' => 'Doe', | ||
'email' => 'jane@luya.io', | ||
'password' => 'nohash', | ||
'is_deleted' => 0, | ||
'is_api_user' => 0, | ||
], | ||
'user3' => [ | ||
'id' => 3, | ||
'title' => 2, | ||
'firstname' => 'James', | ||
'lastname' => 'Doe', | ||
'email' => 'deleted@luya.io', | ||
'password' => 'nohash', | ||
'is_deleted' => 0, | ||
'is_api_user' => 0, | ||
] | ||
] | ||
]); | ||
|
||
$this->createAdminNgRestLogFixture(); | ||
|
||
|
||
// ensures the sort index title = 3 is the last item, which is array index 2 | ||
$q = UserStub::find()->asArray()->all(); | ||
$this->assertSame('Jane', $q[2]['firstname']); | ||
|
||
// get last model (jane) and move to new position | ||
$modelLast = UserStub::findOne(['id' => 2]); | ||
$modelLast->title = 1; | ||
$modelLast->save(true, ['title']); | ||
|
||
// ensures the sort index title = 3 is the last item, which is now since jane has swap to first position James | ||
$q = UserStub::find()->asArray()->all(); | ||
$this->assertSame('James', $q[2]['firstname']); | ||
|
||
// delete the model | ||
$modelLast->delete(); | ||
|
||
// add new item | ||
$newModel = new UserStub(); | ||
$newModel->title = 20; | ||
$newModel->firstname = 'Han'; | ||
$newModel->lastname = 'Solo'; | ||
$newModel->email = 'hansolo@luya.io'; | ||
$newModel->password = 'doesnotexis434!@Aasdfts'; | ||
$newModel->is_deleted = false; | ||
$this->assertTrue($newModel->save()); | ||
|
||
$newModel->refresh(); | ||
|
||
// the index has changed by the sortable plugin, even we have entered 20 as value | ||
// since we have deleted an item, the index has now 3 entries and not 4 | ||
$this->assertSame('3', $newModel->title); | ||
} | ||
} | ||
|
||
class UserStub extends User { | ||
|
||
use SortableTrait; | ||
|
||
public static function sortableField() | ||
{ | ||
return 'title'; | ||
} | ||
} |
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.