Use DBQuery to update the stage & live sort field

Use DBQuery to update the stage & live sort field.
Also check if the list is a many_many list when reordering items
This commit is contained in:
Michael 2018-03-28 10:07:55 +13:00 committed by GitHub
parent 9009a83ae1
commit d7774d6673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
namespace Symbiote\GridFieldExtensions; namespace Symbiote\GridFieldExtensions;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler; use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
@ -17,11 +18,11 @@ use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\DataObjectSchema;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\ManyManyList; use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\Map; use SilverStripe\ORM\Map;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData; use SilverStripe\View\ViewableData;
@ -211,19 +212,24 @@ class GridFieldOrderableRows extends RequestHandler implements
public function getSortTable(SS_List $list) public function getSortTable(SS_List $list)
{ {
$field = $this->getSortField(); $field = $this->getSortField();
if ($list instanceof ManyManyList) { if ($list instanceof ManyManyList) {
$extra = $list->getExtraFields(); $extra = $list->getExtraFields();
$table = $list->getJoinTable(); $table = $list->getJoinTable();
if ($extra && array_key_exists($field, $extra)) { if ($extra && array_key_exists($field, $extra)) {
return $table; return $table;
} }
} }
$classes = ClassInfo::dataClassesFor($list->dataClass()); $classes = ClassInfo::dataClassesFor($list->dataClass());
foreach ($classes as $class) { foreach ($classes as $class) {
if (singleton($class)->hasDataBaseField($field)) { if (singleton($class)->hasDataBaseField($field)) {
return DataObject::getSchema()->tableName($class); return DataObject::getSchema()->tableName($class);
} }
} }
throw new \Exception("Couldn't find the sort field '$field'"); throw new \Exception("Couldn't find the sort field '$field'");
} }
@ -531,24 +537,21 @@ class GridFieldOrderableRows extends RequestHandler implements
// If not a ManyManyList and using versioning, detect it. // If not a ManyManyList and using versioning, detect it.
$this->validateSortField($list); $this->validateSortField($list);
$isVersioned = false;
$class = $list->dataClass(); $class = $list->dataClass();
if (DataObject::getSchema()->tableName($class) == $this->getSortTable($list)) {
$isVersioned = $class::has_extension(Versioned::class); $isVersioned = $class::has_extension(Versioned::class);
}
// Loop through each item, and update the sort values which do not // Loop through each item, and update the sort values which do not
// match to order the objects. // match to order the objects.
if (!$isVersioned) { if (!$isVersioned || $list instanceof ManyManyList) {
$sortTable = $this->getSortTable($list); $sortTable = $this->getSortTable($list);
$now = DBDatetime::now()->Rfc2822();
$additionalSQL = ''; $additionalSQL = '';
$baseTable = DataObject::getSchema()->baseDataTable($list->dataClass()); $baseTable = $sortTable;
if (class_exists($sortTable)) {
$baseTable = singleton($sortTable)->baseTable();
}
$isBaseTable = ($baseTable == $sortTable); $isBaseTable = ($baseTable == $sortTable);
if (!$list instanceof ManyManyList && $isBaseTable) { if (!$list instanceof ManyManyList && $isBaseTable) {
$additionalSQL = ", \"LastEdited\" = '$now'"; $additionalSQL = ', "LastEdited" = NOW()';
} }
foreach ($sortedIDs as $sortValue => $id) { foreach ($sortedIDs as $sortValue => $id) {
@ -562,11 +565,10 @@ class GridFieldOrderableRows extends RequestHandler implements
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
if (!$isBaseTable && !$list instanceof ManyManyList) { if (!$isBaseTable) {
DB::query(sprintf( DB::query(sprintf(
'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s', 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s',
$baseTable, $baseTable,
$now,
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
} }
@ -580,8 +582,14 @@ class GridFieldOrderableRows extends RequestHandler implements
foreach ($sortedIDs as $sortValue => $id) { foreach ($sortedIDs as $sortValue => $id) {
if ($map[$id] != $sortValue) { if ($map[$id] != $sortValue) {
$record = $class::get()->byID($id); $record = $class::get()->byID($id);
$record->$sortField = $sortValue; if ($record instanceof SiteTree) {
$record->write(); $tableClass = 'SiteTree';
} else {
$schema = new DataObjectSchema();
$tableClass = $schema->tableName($class);
}
DB::query("UPDATE " . $tableClass . " SET " . $sortField . "=" . $sortValue . " WHERE ID=" . $record->ID);
DB::query("UPDATE " . $tableClass . "_Live SET " . $sortField . "=" . $sortValue. " WHERE ID=" . $record->ID);
} }
} }
} }
@ -595,13 +603,15 @@ class GridFieldOrderableRows extends RequestHandler implements
$field = $this->getSortField(); $field = $this->getSortField();
$table = $this->getSortTable($list); $table = $this->getSortTable($list);
$clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField()); $clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField());
$now = DBDatetime::now()->Rfc2822();
$additionalSQL = '';
$baseTable = DataObject::getSchema()->baseDataTable($list->dataClass());
$additionalSQL = '';
$baseTable = $table;
if (class_exists($table)) {
$baseTable = singleton($table)->baseTable();
}
$isBaseTable = ($baseTable == $table); $isBaseTable = ($baseTable == $table);
if (!$list instanceof ManyManyList && $isBaseTable) { if (!$list instanceof ManyManyList && $isBaseTable) {
$additionalSQL = ", \"LastEdited\" = '$now'"; $additionalSQL = ', "LastEdited" = NOW()';
} }
foreach ($list->where($clause)->column('ID') as $id) { foreach ($list->where($clause)->column('ID') as $id) {
@ -617,11 +627,10 @@ class GridFieldOrderableRows extends RequestHandler implements
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
if (!$isBaseTable && !$list instanceof ManyManyList) { if (!$isBaseTable) {
DB::query(sprintf( DB::query(sprintf(
'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s', 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s',
$baseTable, $baseTable,
$now,
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
} }