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) {
//Upgrade all the records (including the last inserted from 0 to 1) if ($hasVersioned) {
DB::query('UPDATE "' . $table // For versioned objects, modify them with the ORM so that the *_versions table is updated
. '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1' $itemsToUpdate = $modelClass::get()->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 ($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)
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1'
. ' 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
@ -337,14 +350,21 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$topIncremented[] = $obj->ID; $topIncremented[] = $obj->ID;
} else { } else {
//Append the last record to the bottom if ($hasVersioned) {
DB::query('UPDATE "' . $table // For versioned objects, modify them with the ORM so that the *_versions table is updated
. '" SET "' . $sortColumn . '" = ' . ($max + $i) $obj->$sortColumn = ($max + $i);
. ' WHERE "ID" = ' . $obj->ID); $obj->write();
//LastEdited } else {
DB::query('UPDATE "' . $baseDataTable //Append the last record to the bottom
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\'' DB::query('UPDATE "' . $table
. ' WHERE "ID" = ' . $obj->ID); . '" SET "' . $sortColumn . '" = ' . ($max + $i)
. ' WHERE "ID" = ' . $obj->ID);
//LastEdited
DB::query('UPDATE "' . $baseDataTable
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
. ' 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,6 +525,8 @@ 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) {
@ -512,15 +534,24 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
. '" 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 { } else {
DB::query('UPDATE "' . $table if($hasVersioned) {
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset) // For versioned objects, modify them with the ORM so that the *_versions table is updated
. ' WHERE "ID" = ' . $id); $obj = $modelClass::get()->byID(intval($id));
if (!empty($obj) && $obj !== false && $obj->exists()) {
$obj->$sortColumn = (($sort + 1) + $pageOffset);
$obj->write();
}
}else {
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
. ' WHERE "ID" = ' . $id);
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);