Fixed issue where versioned objects in would not be flagged as modified preventing the sort order from publishing to the live state when the owner is published

This commit is contained in:
UndefinedOffset 2018-07-04 14:00:42 -03:00
parent ae12e74aa8
commit bc633e151d
2 changed files with 53 additions and 19 deletions

View File

@ -1,5 +1,8 @@
# Change Log # Change Log
## [2.0.6](https://github.com/UndefinedOffset/SortableGridField/tree/2.0.6) (2018-07-00)
[Full Changelog](https://github.com/UndefinedOffset/SortableGridField/compare/2.0.5...2.0.6)
## [2.0.5](https://github.com/UndefinedOffset/SortableGridField/tree/2.0.5) (2018-06-07) ## [2.0.5](https://github.com/UndefinedOffset/SortableGridField/tree/2.0.5) (2018-06-07)
[Full Changelog](https://github.com/UndefinedOffset/SortableGridField/compare/2.0.4...2.0.5) [Full Changelog](https://github.com/UndefinedOffset/SortableGridField/compare/2.0.4...2.0.5)

View File

@ -308,6 +308,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$topIncremented = array(); $topIncremented = array();
} }
$modelClass = $gridField->getModelClass();
$hasVersioned = $this->hasVersionedExtension($modelClass);
foreach ($list as $obj) { foreach ($list as $obj) {
if ($many_many) { if ($many_many) {
if ($this->append_to_top) { if ($this->append_to_top) {
@ -324,10 +326,21 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID); . ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
} }
} else if ($this->append_to_top) { } else if ($this->append_to_top) {
if ($hasVersioned) {
// For versioned objects, modify them with the ORM so that the *_versions table is updated
$itemsToUpdate = $modelClass::get()->where(($list instanceof RelationList ? '"' . $list->foreignKey . '" = ' . $owner->ID : $idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\'' . implode('\',\'', $topIncremented) . '\')' : ''));
if ($itemsToUpdate->exists()) {
foreach ($itemsToUpdate as $item) {
$item->$sortColumn = $item->$sortColumn + 1;
$item->write();
}
}
}else {
//Upgrade all the records (including the last inserted from 0 to 1) //Upgrade all the records (including the last inserted from 0 to 1)
DB::query('UPDATE "' . $table DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1' . '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1'
. ' WHERE ' . ($list instanceof RelationList ? '"' . $list->foreignKey . '" = ' . $owner->ID : $idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\'' . implode('\',\'', $topIncremented) . '\')' : '')); . ' WHERE ' . ($list instanceof RelationList ? '"' . $list->foreignKey . '" = ' . $owner->ID : $idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\'' . implode('\',\'', $topIncremented) . '\')' : ''));
}
if ($this->update_versioned_stage && $this->hasVersionedExtension($gridField->getModelClass())) { if ($this->update_versioned_stage && $this->hasVersionedExtension($gridField->getModelClass())) {
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
@ -336,15 +349,22 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
} }
$topIncremented[] = $obj->ID; $topIncremented[] = $obj->ID;
} else {
if ($hasVersioned) {
// For versioned objects, modify them with the ORM so that the *_versions table is updated
$obj->$sortColumn = ($max + $i);
$obj->write();
} else { } else {
//Append the last record to the bottom //Append the last record to the bottom
DB::query('UPDATE "' . $table DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . ($max + $i) . '" SET "' . $sortColumn . '" = ' . ($max + $i)
. ' WHERE "ID" = ' . $obj->ID); . ' WHERE "ID" = ' . $obj->ID);
//LastEdited //LastEdited
DB::query('UPDATE "' . $baseDataTable DB::query('UPDATE "' . $baseDataTable
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\'' . '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
. ' WHERE "ID" = ' . $obj->ID); . ' WHERE "ID" = ' . $obj->ID);
}
if ($this->update_versioned_stage && $this->hasVersionedExtension($gridField->getModelClass())) { if ($this->update_versioned_stage && $this->hasVersionedExtension($gridField->getModelClass())) {
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
@ -505,12 +525,22 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
//Perform sorting //Perform sorting
$ids = explode(',', $data['ItemIDs']); $ids = explode(',', $data['ItemIDs']);
$modelClass = $gridField->getModelClass();
$hasVersioned = $this->hasVersionedExtension($modelClass);
for ($sort = 0; $sort < count($ids); $sort++) { for ($sort = 0; $sort < count($ids); $sort++) {
$id = intval($ids[$sort]); $id = intval($ids[$sort]);
if ($many_many) { if ($many_many) {
DB::query('UPDATE "' . $table DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset) . '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
. ' WHERE "' . $componentField . '" = ' . $id . ' AND "' . $parentField . '" = ' . $owner->ID); . ' WHERE "' . $componentField . '" = ' . $id . ' AND "' . $parentField . '" = ' . $owner->ID);
} else {
if($hasVersioned) {
// For versioned objects, modify them with the ORM so that the *_versions table is updated
$obj = $modelClass::get()->byID(intval($id));
if (!empty($obj) && $obj !== false && $obj->exists()) {
$obj->$sortColumn = (($sort + 1) + $pageOffset);
$obj->write();
}
}else { }else {
DB::query('UPDATE "' . $table DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset) . '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
@ -519,8 +549,9 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
DB::query('UPDATE "' . $baseDataTable DB::query('UPDATE "' . $baseDataTable
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\'' . '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
. ' WHERE "ID" = ' . $id); . ' WHERE "ID" = ' . $id);
}
if ($this->update_versioned_stage && $this->hasVersionedExtension($gridField->getModelClass())) { if ($this->update_versioned_stage && $hasVersioned) {
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset) . '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
. ' WHERE "ID" = ' . $id); . ' WHERE "ID" = ' . $id);