-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from hchokshi/fix/auto-live-sort
Fix auto live sort
- Loading branch information
Showing
5 changed files
with
66 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace LittleGiant\CatalogManager\Extensions; | ||
|
||
use SilverStripe\CMS\Model\SiteTree; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\ORM\DB; | ||
use SilverStripe\ORM\SS_List; | ||
|
||
/** | ||
* Class AutoPublishSortExtension | ||
* @package LittleGiant\CatalogManager\Extensions | ||
*/ | ||
class AutoPublishSortExtension extends Extension | ||
{ | ||
/** | ||
* @see \Symbiote\GridFieldExtensions\GridFieldOrderableRows::reorderItems() | ||
* @param \SilverStripe\ORM\ArrayList|\SilverStripe\ORM\DataList $list | ||
* @param array $values [listItemID => currentSortValue]; | ||
* @param array $sortedIDs [newSortValue => listItemID] | ||
*/ | ||
public function onAfterReorderItems(SS_List &$list, array $values, array $sortedIDs) | ||
{ | ||
$modelClass = $list->dataClass(); | ||
/** @var CatalogPageExtension|SiteTree $model */ | ||
$model = singleton($modelClass); | ||
if (!$model::config()->get('automatic_live_sort')) { | ||
return; | ||
} | ||
|
||
$sortField = CatalogPageExtension::getClassSortFieldName($modelClass); | ||
$tableName = DataObject::getSchema()->tableForField($modelClass, $sortField); | ||
if ($tableName === null) { | ||
throw new \Exception("Sort field {$sortField} could not be found in table hierarchy for {$modelClass}."); | ||
} | ||
|
||
foreach ($sortedIDs as $sortValue => $itemID) { | ||
DB::prepared_query('UPDATE "' . $tableName . '_Live" SET "' . $sortField . '"=? WHERE "ID"=?', [$sortValue, $itemID]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters