Skip to content

Commit

Permalink
Merge pull request #58 from hchokshi/fix/auto-live-sort
Browse files Browse the repository at this point in the history
Fix auto live sort
  • Loading branch information
tractorcow authored Sep 10, 2018
2 parents b76d212 + 8f9b9da commit f5fe0aa
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 43 deletions.
4 changes: 4 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ SilverStripe\SiteConfig\SiteConfig:
required_permission:
- SITETREE_VIEW_ALL
- SITETREE_EDIT_ALL

Symbiote\GridFieldExtensions\GridFieldOrderableRows:
extensions:
- LittleGiant\CatalogManager\Extensions\AutoPublishSortExtension
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"require": {
"silverstripe/cms": "^4.0",
"symbiote/silverstripe-gridfieldextensions": "^3.0"
"symbiote/silverstripe-gridfieldextensions": "^3.2"
},
"autoload": {
"psr-4": {
Expand Down
42 changes: 42 additions & 0 deletions src/Extensions/AutoPublishSortExtension.php
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]);
}
}
}
34 changes: 19 additions & 15 deletions src/Extensions/CatalogPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField;
Expand All @@ -21,7 +22,6 @@ class CatalogPageExtension extends DataExtension
const CONFIG_SETTINGS_WITH_DEFAULTS = [
'parent_classes',
'can_duplicate',
'sort_column',
'automatic_live_sort',
];

Expand All @@ -37,14 +37,6 @@ class CatalogPageExtension extends DataExtension
*/
private static $can_duplicate = true;

/**
* Name of the sorting column. SiteTree has a column named "Sort", we use this as default.
*
* @config
* @var string
*/
private static $sort_column = 'Sort';

/**
* @config
* @var bool
Expand Down Expand Up @@ -138,13 +130,25 @@ public function getCatalogParents()
*/
public function getSortFieldName()
{
$sortColumn = $this->owner->config()->get('sort_column');
return static::getClassSortFieldName($this->owner);
}

if ($sortColumn === false) {
return null;
} else {
return $sortColumn ?: 'Sort';
}
/**
* Gets the field name for a class's sort column. As CatalogPageExtension is applied to subclasses of SiteTree,
* 'Sort' is default.
* Can be overwritten using $sort_column config on extended class.
* Set $sort_column config to false to disable sorting in the gridfield
*
* @param string|object $class
* @return null|string
*/
public static function getClassSortFieldName($class)
{
$sortColumn = Config::forClass($class)->get('sort_column');

return $sortColumn === false
? null
: ($sortColumn ?: 'Sort');
}

/**
Expand Down
27 changes: 0 additions & 27 deletions src/ModelAdmin/CatalogPageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
Expand Down Expand Up @@ -151,30 +150,4 @@ protected function getParentClassesForMessage(array $classes)
$last = array_pop($parentNames);
return implode(', ', $parentNames) . " or {$last}";
}

/**
* Hook to update sort column on live versions of items after a sort has occured.
* @param $items
*/
public function onAfterGridFieldRowSort($items)
{
/** @var \LittleGiant\CatalogManager\Extensions\CatalogPageExtension|\SilverStripe\CMS\Model\SiteTree $model */
$model = singleton($this->modelClass);
if (!$model::config()->get('automatic_live_sort')) {
return;
}

// if the sort is the default, then we should update SiteTree. If its a custom sort, update the model.
$sortField = $model->getSortFieldName();
$tableName = DataObjectSchema::singleton()->tableForField($this->modelClass, $sortField);
if ($tableName === null) {
throw new \Exception("Sort field {$sortField} could not be found in table hierarchy for {$this->modelClass}.");
}

foreach ($items as $item) {
if ($item instanceof $this->modelClass) {
DB::query("UPDATE \"{$tableName}_Live\" SET \"{$model->getSortFieldname()}\"=\"{$item->{$sortField}}\" WHERE \"ID\"=\"{$item->ID}\"");
}
}
}
}

0 comments on commit f5fe0aa

Please sign in to comment.