@@ -587,9 +587,10 @@ protected function executeReorder(GridField $grid, $sortedIDs)
587587 */
588588 protected function reorderItems ($ list , array $ values , array $ sortedIDs )
589589 {
590+ $ this ->extend ('onBeforeReorderItems ' , $ list , $ values , $ sortedIDs );
591+
590592 // setup
591593 $ sortField = $ this ->getSortField ();
592- $ class = $ list ->dataClass ();
593594 // The problem is that $sortedIDs is a list of the _related_ item IDs, which causes trouble
594595 // with ManyManyThrough, where we need the ID of the _join_ item in order to set the value.
595596 $ itemToSortReference = ($ list instanceof ManyManyThroughList) ? 'getJoin ' : 'Me ' ;
@@ -598,53 +599,21 @@ protected function reorderItems($list, array $values, array $sortedIDs)
598599 // sanity check.
599600 $ this ->validateSortField ($ list );
600601
601- $ isVersioned = false ;
602- // check if sort column is present on the model provided by dataClass() and if it's versioned
603- // cases:
604- // Model has sort column and is versioned - handle as versioned
605- // Model has sort column and is NOT versioned - handle as NOT versioned
606- // Model doesn't have sort column because sort column is on ManyManyList - handle as NOT versioned
607- // Model doesn't have sort column because sort column is on ManyManyThroughList - inspect through object
608- if ($ list instanceof ManyManyThroughList) {
609- // We'll be updating the join class, not the relation class.
610- $ class = $ this ->getManyManyInspector ($ list )->getJoinClass ();
611- $ isVersioned = $ class ::create ()->hasExtension (Versioned::class);
612- } elseif (!$ this ->isManyMany ($ list )) {
613- $ isVersioned = $ class ::create ()->hasExtension (Versioned::class);
614- }
615-
616- // Loop through each item, and update the sort values which do not
617- // match to order the objects.
618- if (!$ isVersioned ) {
602+ // ManyManyList extra fields aren't easily updated via the ORM, and so they need to be updated through an SQL
603+ // Query
604+ if ($ list instanceof ManyManyList) {
619605 $ sortTable = $ this ->getSortTable ($ list );
620- $ now = DBDatetime::now ()->Rfc2822 ();
621- $ additionalSQL = '' ;
622- $ baseTable = DataObject::getSchema ()->baseDataTable ($ class );
623-
624- $ isBaseTable = ($ baseTable == $ sortTable );
625- if (!$ list instanceof ManyManyList && $ isBaseTable ) {
626- $ additionalSQL = ", \"LastEdited \" = ' $ now' " ;
627- }
628606
607+ // Loop through each item, and update the sort values which do not match to order the objects.
629608 foreach ($ sortedIDs as $ newSortValue => $ targetRecordID ) {
630609 if ($ currentSortList [$ targetRecordID ]->$ sortField != $ newSortValue ) {
631610 DB ::query (sprintf (
632- 'UPDATE "%s" SET "%s" = %d%s WHERE %s ' ,
611+ 'UPDATE "%s" SET "%s" = %d WHERE %s ' ,
633612 $ sortTable ,
634613 $ sortField ,
635614 $ newSortValue ,
636- $ additionalSQL ,
637615 $ this ->getSortTableClauseForIds ($ list , $ targetRecordID )
638616 ));
639-
640- if (!$ isBaseTable && !$ list instanceof ManyManyList) {
641- DB ::query (sprintf (
642- 'UPDATE "%s" SET "LastEdited" = \'%s \' WHERE %s ' ,
643- $ baseTable ,
644- $ now ,
645- $ this ->getSortTableClauseForIds ($ list , $ targetRecordID )
646- ));
647- }
648617 }
649618 }
650619 } else {
@@ -656,6 +625,7 @@ protected function reorderItems($list, array $values, array $sortedIDs)
656625 // either the list data class (has_many, (belongs_)many_many)
657626 // or the intermediary join class (many_many through)
658627 $ record = $ currentSortList [$ targetRecordID ];
628+
659629 if ($ record ->$ sortField != $ newSortValue ) {
660630 $ record ->$ sortField = $ newSortValue ;
661631 $ record ->write ();
0 commit comments