mirror of
https://github.com/UndefinedOffset/SortableGridField.git
synced 2024-10-01 04:08:59 +02:00
SS4 Compliance & Updated Tests
This commit is contained in:
parent
37b3d86d60
commit
8b80547144
@ -1,23 +1,66 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace UndefinedOffset\SortableGridField\Forms;
|
||||||
|
|
||||||
|
use SilverStripe\Admin\ModelAdmin;
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Core\ClassInfo;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\Core\Extensible;
|
||||||
|
use SilverStripe\Forms\GridField\GridField;
|
||||||
|
use SilverStripe\Forms\GridField\GridField_ActionProvider;
|
||||||
|
use SilverStripe\Forms\GridField\GridField_DataManipulator;
|
||||||
|
use SilverStripe\Forms\GridField\GridField_FormAction;
|
||||||
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldPaginator;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
|
||||||
|
use SilverStripe\ORM\DataList;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\ORM\DataQuery;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
use SilverStripe\ORM\ManyManyList;
|
||||||
|
use SilverStripe\ORM\RelationList;
|
||||||
|
use SilverStripe\ORM\SS_List;
|
||||||
|
use SilverStripe\ORM\UnsavedRelationList;
|
||||||
|
use SilverStripe\ORM\ValidationException;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
use SilverStripe\View\arrayData;
|
||||||
|
use SilverStripe\View\Requirements;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
|
* This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
|
||||||
*
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
*/
|
*/
|
||||||
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
|
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
protected $sortColumn;
|
protected $sortColumn;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
protected $disable_selection = true;
|
protected $disable_selection = true;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
protected $append_to_top = false;
|
protected $append_to_top = false;
|
||||||
|
|
||||||
|
/** @var null|string */
|
||||||
protected $update_versioned_stage = null;
|
protected $update_versioned_stage = null;
|
||||||
|
|
||||||
|
/** @var null|string */
|
||||||
protected $custom_relation_name = null;
|
protected $custom_relation_name = null;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $tableMap = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sortColumn Column that should be used to update the sort information
|
* @param string $sortColumn Column that should be used to update the sort information
|
||||||
* @param bool $disableSelection Disable selection on the GridField when dragging
|
* @param bool $disableSelection Disable selection on the GridField when dragging
|
||||||
* @param string $updateVersionStage Name of the versioned stage to update this disabled by default unless this is set
|
* @param string $updateVersionStage Name of the versioned stage to update this disabled by default unless this is set
|
||||||
* @param string $customRelationName Name of the relationship to use, if left null the name is determined from the GridField's name
|
* @param string $customRelationName Name of the relationship to use, if left null the name is determined from the GridField's name
|
||||||
*/
|
*/
|
||||||
public function __construct($sortColumn, $disableSelection = true, $updateVersionStage = null, $customRelationName = null) {
|
public function __construct($sortColumn, $disableSelection = true, $updateVersionStage = null, $customRelationName = null)
|
||||||
|
{
|
||||||
$this->sortColumn = $sortColumn;
|
$this->sortColumn = $sortColumn;
|
||||||
$this->disable_selection = $disableSelection;
|
$this->disable_selection = $disableSelection;
|
||||||
$this->update_versioned_stage = $updateVersionStage;
|
$this->update_versioned_stage = $updateVersionStage;
|
||||||
@ -27,9 +70,10 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
/**
|
/**
|
||||||
* Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
* Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @return Array Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
* @return array Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
||||||
*/
|
*/
|
||||||
public function getHTMLFragments($gridField) {
|
public function getHTMLFragments($gridField)
|
||||||
|
{
|
||||||
$dataList = $gridField->getList();
|
$dataList = $gridField->getList();
|
||||||
|
|
||||||
if (class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
if (class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
||||||
@ -82,24 +126,21 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
'Checked' => ($state->sortableToggle == true ? ' checked = "checked"' : ''),
|
'Checked' => ($state->sortableToggle == true ? ' checked = "checked"' : ''),
|
||||||
'List' => $dataList);
|
'List' => $dataList);
|
||||||
|
|
||||||
$forTemplate = new ArrayData($data);
|
$forTemplate = new arrayData($data);
|
||||||
|
|
||||||
|
|
||||||
//Inject Requirements
|
//Inject Requirements
|
||||||
$custom = Config::inst()->get('GridFieldSortableRows', 'Base');
|
$custom = Config::inst()->get(GridFieldSortableRows::class, 'Base');
|
||||||
$base = $custom ?: SORTABLE_GRIDFIELD_BASE;
|
$base = $custom ?: SORTABLE_GRIDFIELD_BASE;
|
||||||
|
|
||||||
Requirements::css($base . '/css/GridFieldSortableRows.css');
|
Requirements::css($base . '/css/GridFieldSortableRows.css');
|
||||||
Requirements::javascript($base . '/javascript/GridFieldSortableRows.js');
|
Requirements::javascript($base . '/javascript/GridFieldSortableRows.js');
|
||||||
|
|
||||||
|
|
||||||
$args = array('Colspan' => count($gridField->getColumns()), 'ID' => $gridField->ID(), 'DisableSelection' => $this->disable_selection);
|
$args = array('Colspan' => count($gridField->getColumns()), 'ID' => $gridField->ID(), 'DisableSelection' => $this->disable_selection);
|
||||||
|
|
||||||
|
$fragments = array('header' => $forTemplate->renderWith('SortableGridField\Forms\Includes\GridFieldSortableRows', $args));
|
||||||
|
|
||||||
$fragments=array('header' => $forTemplate->renderWith('GridFieldSortableRows', $args));
|
if ($gridField->getConfig()->getComponentByType(GridFieldPaginator::class)) {
|
||||||
|
$fragments['after'] = $forTemplate->renderWith('SortableGridField\Forms\Includes\GridFieldSortableRows_paginator');
|
||||||
if($gridField->getConfig()->getComponentByType('GridFieldPaginator')) {
|
|
||||||
$fragments['after']=$forTemplate->renderWith('GridFieldSortableRows_paginator');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $fragments;
|
return $fragments;
|
||||||
@ -108,10 +149,11 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
/**
|
/**
|
||||||
* Manipulate the datalist as needed by this grid modifier.
|
* Manipulate the datalist as needed by this grid modifier.
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param SS_List $dataList Data List to adjust
|
* @param SS_List|DataList $dataList Data List to adjust
|
||||||
* @return DataList Modified Data List
|
* @return DataList Modified Data List
|
||||||
*/
|
*/
|
||||||
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
public function getManipulatedData(GridField $gridField, SS_List $dataList)
|
||||||
|
{
|
||||||
//Detect and correct items with a sort column value of 0 (push to bottom)
|
//Detect and correct items with a sort column value of 0 (push to bottom)
|
||||||
$this->fixSortColumn($gridField, $dataList);
|
$this->fixSortColumn($gridField, $dataList);
|
||||||
|
|
||||||
@ -123,8 +165,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($state->sortableToggle === true) {
|
if ($state->sortableToggle === true) {
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
|
$gridField->getConfig()->removeComponentsByType(GridFieldFilterHeader::class);
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
|
$gridField->getConfig()->removeComponentsByType(GridFieldSortableHeader::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dataList->sort($this->sortColumn);
|
return $dataList->sort($this->sortColumn);
|
||||||
@ -135,7 +177,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
* @param bool $value Boolean true to append to the top false to append to the bottom
|
* @param bool $value Boolean true to append to the top false to append to the bottom
|
||||||
* @return GridFieldSortableRows Returns the current instance
|
* @return GridFieldSortableRows Returns the current instance
|
||||||
*/
|
*/
|
||||||
public function setAppendToTop($value) {
|
public function setAppendToTop($value)
|
||||||
|
{
|
||||||
$this->append_to_top = $value;
|
$this->append_to_top = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -144,7 +187,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
* @param bool $value Boolean true to disable selection of table contents false to enable selection
|
* @param bool $value Boolean true to disable selection of table contents false to enable selection
|
||||||
* @return GridFieldSortableRows Returns the current instance
|
* @return GridFieldSortableRows Returns the current instance
|
||||||
*/
|
*/
|
||||||
public function setDisableSelection($value){
|
public function setDisableSelection($value)
|
||||||
|
{
|
||||||
$this->disable_selection = $value;
|
$this->disable_selection = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -154,7 +198,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
* @param string $value Versioned Stage to update this is disabled by default unless this is set
|
* @param string $value Versioned Stage to update this is disabled by default unless this is set
|
||||||
* @return GridFieldSortableRows Returns the current instance
|
* @return GridFieldSortableRows Returns the current instance
|
||||||
*/
|
*/
|
||||||
public function setUpdateVersionedStage($value) {
|
public function setUpdateVersionedStage($value)
|
||||||
|
{
|
||||||
$this->update_versioned_stage = $value;
|
$this->update_versioned_stage = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -164,7 +209,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
* @param string $value Name of the relationship to use, by default the name is determined from the GridField's name
|
* @param string $value Name of the relationship to use, by default the name is determined from the GridField's name
|
||||||
* @return GridFieldSortableRows Returns the current instance
|
* @return GridFieldSortableRows Returns the current instance
|
||||||
*/
|
*/
|
||||||
public function setCustomRelationName($value) {
|
public function setCustomRelationName($value)
|
||||||
|
{
|
||||||
$this->custom_relation_name = $value;
|
$this->custom_relation_name = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -172,15 +218,18 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
/**
|
/**
|
||||||
* Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
|
* Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param SS_List $dataList Data List of items to be checked
|
* @param SS_List|DataList $dataList Data List of items to be checked
|
||||||
*/
|
*/
|
||||||
protected function fixSortColumn($gridField, SS_List $dataList) {
|
protected function fixSortColumn($gridField, SS_List $dataList)
|
||||||
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
{
|
||||||
|
if ($dataList instanceof UnsavedRelationList) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var SS_List|DataList $list */
|
||||||
$list = clone $dataList;
|
$list = clone $dataList;
|
||||||
$list = $list->alterDataQuery(function ($query, SS_List $tmplist) {
|
$list = $list->alterDataQuery(function ($query, SS_List $tmplist) {
|
||||||
|
/** @var DataQuery $query */
|
||||||
$query->limit(array());
|
$query->limit(array());
|
||||||
return $query;
|
return $query;
|
||||||
});
|
});
|
||||||
@ -188,7 +237,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
$many_many = ($list instanceof ManyManyList);
|
$many_many = ($list instanceof ManyManyList);
|
||||||
if (!$many_many) {
|
if (!$many_many) {
|
||||||
$sng = singleton($gridField->getModelClass());
|
$sng = singleton($gridField->getModelClass());
|
||||||
$fieldType=$sng->db($this->sortColumn);
|
$fieldType = $sng->config()->db[$this->sortColumn];
|
||||||
|
|
||||||
if (!$fieldType || !(strtolower($fieldType) == 'int' || is_subclass_of($fieldType, 'Int'))) {
|
if (!$fieldType || !(strtolower($fieldType) == 'int' || is_subclass_of($fieldType, 'Int'))) {
|
||||||
if (is_array($fieldType)) {
|
if (is_array($fieldType)) {
|
||||||
user_error('Sort column ' . $this->sortColumn . ' could not be found in ' . $gridField->getModelClass() . '\'s ancestry', E_USER_ERROR);
|
user_error('Sort column ' . $this->sortColumn . ' could not be found in ' . $gridField->getModelClass() . '\'s ancestry', E_USER_ERROR);
|
||||||
@ -200,16 +250,19 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$max = $list->Max($this->sortColumn);
|
$max = $list->Max($this->sortColumn);
|
||||||
$list = $list->filter($this->sortColumn, 0)->sort("Created,ID");
|
$list = $list->filter($this->sortColumn, 0)->sort("Created,ID");
|
||||||
if ($list->Count() > 0) {
|
if ($list->Count() > 0) {
|
||||||
$owner = $gridField->Form->getRecord();
|
$owner = $gridField->getForm()->getRecord();
|
||||||
$sortColumn = $this->sortColumn;
|
$sortColumn = $this->sortColumn;
|
||||||
$i = 1;
|
$i = 1;
|
||||||
|
|
||||||
if ($many_many) {
|
if ($many_many) {
|
||||||
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
||||||
|
|
||||||
|
/** @var DataObject $table */
|
||||||
|
$table = $this->mapTableNameAndReturn($table);
|
||||||
|
|
||||||
$extraFields = $owner->many_many_extraFields((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
$extraFields = $owner->many_many_extraFields((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
||||||
|
|
||||||
if (!$extraFields || !array_key_exists($this->sortColumn, $extraFields) || !($extraFields[$this->sortColumn] == 'Int' || is_subclass_of('Int', $extraFields[$this->sortColumn]))) {
|
if (!$extraFields || !array_key_exists($this->sortColumn, $extraFields) || !($extraFields[$this->sortColumn] == 'Int' || is_subclass_of('Int', $extraFields[$this->sortColumn]))) {
|
||||||
@ -223,13 +276,13 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
||||||
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
||||||
$table=$class;
|
$table = $this->mapTableNameAndReturn($class);
|
||||||
} else {
|
} else {
|
||||||
$classes = ClassInfo::ancestry($class, true);
|
$classes = ClassInfo::ancestry($class, true);
|
||||||
foreach ($classes as $class) {
|
foreach ($classes as $class) {
|
||||||
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
||||||
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
||||||
$table=$class;
|
$table = $this->mapTableNameAndReturn($class);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -240,13 +293,15 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$baseDataClass=ClassInfo::baseDataClass($gridField->getModelClass());
|
$baseDataClass = DataObject::getSchema()->baseDataClass($gridField->getModelClass());
|
||||||
|
$baseDataClass = $this->mapTableNameAndReturn($baseDataClass);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Start transaction if supported
|
//Start transaction if supported
|
||||||
if(DB::getConn()->supportsTransactions()) {
|
if (DB::get_conn()->supportsTransactions()) {
|
||||||
DB::getConn()->transactionStart();
|
DB::get_conn()->transactionStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
$idCondition = null;
|
$idCondition = null;
|
||||||
@ -279,7 +334,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
. '" 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 && class_exists($table) && Object::has_extension($table, 'Versioned')) {
|
if ($this->update_versioned_stage && class_exists($this->tableMap[$table]) && $this->hasVersionedExtension($this->tableMap[$table])) {
|
||||||
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
||||||
. '" 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) . '\')' : ''));
|
||||||
@ -296,12 +351,12 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
. '" 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 && class_exists($table) && Object::has_extension($table, 'Versioned')) {
|
if ($this->update_versioned_stage && class_exists($this->tableMap[$table]) && $this->hasVersionedExtension($this->tableMap[$table])) {
|
||||||
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
||||||
. '" SET "' . $sortColumn . '" = ' . ($max + $i)
|
. '" SET "' . $sortColumn . '" = ' . ($max + $i)
|
||||||
. ' WHERE "ID" = ' . $obj->ID);
|
. ' WHERE "ID" = ' . $obj->ID);
|
||||||
|
|
||||||
if(Object::has_extension($baseDataClass, 'Versioned')) {
|
if ($this->hasVersionedExtension($this->tableMap[$baseDataClass])) {
|
||||||
DB::query('UPDATE "' . $baseDataClass . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $baseDataClass . '_' . $this->update_versioned_stage
|
||||||
. '" 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);
|
||||||
@ -318,7 +373,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
||||||
. ' WHERE "ID" IN(\'' . implode('\',\'', $topIncremented) . '\')');
|
. ' WHERE "ID" IN(\'' . implode('\',\'', $topIncremented) . '\')');
|
||||||
|
|
||||||
if($this->update_versioned_stage && class_exists($table) && Object::has_extension($table, 'Versioned') && Object::has_extension($baseDataClass, 'Versioned')) {
|
if ($this->update_versioned_stage && class_exists($this->tableMap[$table]) && $this->hasVersionedExtension($this->tableMap[$table]) && $this->hasVersionedExtension($this->tableMap[$baseDataClass])) {
|
||||||
DB::query('UPDATE "' . $baseDataClass . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $baseDataClass . '_' . $this->update_versioned_stage
|
||||||
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
||||||
. ' WHERE "ID" IN(\'' . implode('\',\'', $topIncremented) . '\')');
|
. ' WHERE "ID" IN(\'' . implode('\',\'', $topIncremented) . '\')');
|
||||||
@ -327,8 +382,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
|
|
||||||
//End transaction if supported
|
//End transaction if supported
|
||||||
if(DB::getConn()->supportsTransactions()) {
|
if (DB::get_conn()->supportsTransactions()) {
|
||||||
DB::getConn()->transactionEnd();
|
DB::get_conn()->transactionEnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -336,9 +391,10 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
/**
|
/**
|
||||||
* Return a list of the actions handled by this action provider.
|
* Return a list of the actions handled by this action provider.
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @return Array Array with action identifier strings.
|
* @return array array with action identifier strings.
|
||||||
*/
|
*/
|
||||||
public function getActions($gridField) {
|
public function getActions($gridField)
|
||||||
|
{
|
||||||
return array('saveGridRowSort', 'sortableRowsToggle', 'sortToPage');
|
return array('saveGridRowSort', 'sortableRowsToggle', 'sortToPage');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -346,16 +402,17 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
* Handle an action on the given grid field.
|
* Handle an action on the given grid field.
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param String $actionName Action identifier, see {@link getActions()}.
|
* @param String $actionName Action identifier, see {@link getActions()}.
|
||||||
* @param Array $arguments Arguments relevant for this
|
* @param array $arguments Arguments relevant for this
|
||||||
* @param Array $data All form data
|
* @param array $data All form data
|
||||||
*/
|
*/
|
||||||
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
||||||
|
{
|
||||||
$state = $gridField->State->GridFieldSortableRows;
|
$state = $gridField->State->GridFieldSortableRows;
|
||||||
if (!is_bool($state->sortableToggle)) {
|
if (!is_bool($state->sortableToggle)) {
|
||||||
$state->sortableToggle = false;
|
$state->sortableToggle = false;
|
||||||
} else if ($state->sortableToggle == true) {
|
} else if ($state->sortableToggle == true) {
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
|
$gridField->getConfig()->removeComponentsByType(GridFieldFilterHeader::class);
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
|
$gridField->getConfig()->removeComponentsByType(GridFieldSortableHeader::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -369,12 +426,14 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
/**
|
/**
|
||||||
* Handles saving of the row sort order
|
* Handles saving of the row sort order
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param Array $data Data submitted in the request
|
* @param array $data Data submitted in the request
|
||||||
|
* @throws ValidationException If user has no edit permissions
|
||||||
*/
|
*/
|
||||||
protected function saveGridRowSort(GridField $gridField, $data) {
|
protected function saveGridRowSort(GridField $gridField, $data)
|
||||||
|
{
|
||||||
$dataList = $gridField->getList();
|
$dataList = $gridField->getList();
|
||||||
|
|
||||||
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
if ($dataList instanceof UnsavedRelationList) {
|
||||||
user_error('Cannot sort an UnsavedRelationList', E_USER_ERROR);
|
user_error('Cannot sort an UnsavedRelationList', E_USER_ERROR);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -394,7 +453,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
$sortColumn = $this->sortColumn;
|
$sortColumn = $this->sortColumn;
|
||||||
$pageOffset = 0;
|
$pageOffset = 0;
|
||||||
|
|
||||||
if ($paginator = $gridField->getConfig()->getComponentsByType('GridFieldPaginator')->First()) {
|
if ($paginator = $gridField->getConfig()->getComponentsByType(GridFieldPaginator::class)->First()) {
|
||||||
$pageState = $gridField->State->GridFieldPaginator;
|
$pageState = $gridField->State->GridFieldPaginator;
|
||||||
|
|
||||||
if ($pageState->currentPage && is_int($pageState->currentPage) && $pageState->currentPage > 1) {
|
if ($pageState->currentPage && is_int($pageState->currentPage) && $pageState->currentPage > 1) {
|
||||||
@ -405,19 +464,22 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
if ($many_many) {
|
if ($many_many) {
|
||||||
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
||||||
|
|
||||||
|
/** @var DataObject $table */
|
||||||
|
$table = $this->mapTableNameAndReturn($table);
|
||||||
} else {
|
} else {
|
||||||
//Find table containing the sort column
|
//Find table containing the sort column
|
||||||
$table = false;
|
$table = false;
|
||||||
$class = $gridField->getModelClass();
|
$class = $gridField->getModelClass();
|
||||||
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
||||||
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
||||||
$table=$class;
|
$table = $this->mapTableNameAndReturn($class);
|
||||||
} else {
|
} else {
|
||||||
$classes = ClassInfo::ancestry($class, true);
|
$classes = ClassInfo::ancestry($class, true);
|
||||||
foreach ($classes as $class) {
|
foreach ($classes as $class) {
|
||||||
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
||||||
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
||||||
$table=$class;
|
$table = $this->mapTableNameAndReturn($class);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -428,7 +490,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$baseDataClass=ClassInfo::baseDataClass($gridField->getModelClass());
|
$baseDataClass = DataObject::getSchema()->baseDataClass($gridField->getModelClass());
|
||||||
|
$baseDataClass = $this->mapTableNameAndReturn($baseDataClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -439,13 +502,11 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
Controller::curr()->onBeforeGridFieldRowSort(clone $items);
|
Controller::curr()->onBeforeGridFieldRowSort(clone $items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Start transaction if supported
|
//Start transaction if supported
|
||||||
if(DB::getConn()->supportsTransactions()) {
|
if (DB::get_conn()->supportsTransactions()) {
|
||||||
DB::getConn()->transactionStart();
|
DB::get_conn()->transactionStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Perform sorting
|
//Perform sorting
|
||||||
$ids = explode(',', $data['ItemIDs']);
|
$ids = explode(',', $data['ItemIDs']);
|
||||||
for ($sort = 0; $sort < count($ids); $sort++) {
|
for ($sort = 0; $sort < count($ids); $sort++) {
|
||||||
@ -463,12 +524,12 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
. '" 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 && class_exists($table) && Object::has_extension($table, 'Versioned')) {
|
if ($this->update_versioned_stage && class_exists($this->tableMap[$table]) && $this->hasVersionedExtension($this->tableMap[$table])) {
|
||||||
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);
|
||||||
|
|
||||||
if(Object::has_extension($baseDataClass, 'Versioned')) {
|
if ($this->hasVersionedExtension($this->tableMap[$baseDataClass])) {
|
||||||
DB::query('UPDATE "' . $baseDataClass . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $baseDataClass . '_' . $this->update_versioned_stage
|
||||||
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
||||||
. ' WHERE "ID" = ' . $id);
|
. ' WHERE "ID" = ' . $id);
|
||||||
@ -479,8 +540,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
|
|
||||||
//End transaction if supported
|
//End transaction if supported
|
||||||
if(DB::getConn()->supportsTransactions()) {
|
if (DB::get_conn()->supportsTransactions()) {
|
||||||
DB::getConn()->transactionEnd();
|
DB::get_conn()->transactionEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -495,10 +556,11 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
/**
|
/**
|
||||||
* Handles sorting across pages
|
* Handles sorting across pages
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param Array $data Data submitted in the request
|
* @param array $data Data submitted in the request
|
||||||
*/
|
*/
|
||||||
protected function sortToPage(GridField $gridField, $data) {
|
protected function sortToPage(GridField $gridField, $data)
|
||||||
if (!$paginator = $gridField->getConfig()->getComponentsByType('GridFieldPaginator')->First()) {
|
{
|
||||||
|
if (!$paginator = $gridField->getConfig()->getComponentsByType(GridFieldPaginator::class)->First()) {
|
||||||
user_error('Paginator not detected', E_USER_ERROR);
|
user_error('Paginator not detected', E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -510,10 +572,13 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
user_error('No target page', E_USER_ERROR);
|
user_error('No target page', E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var Extensible $className */
|
||||||
$className = $gridField->getModelClass();
|
$className = $gridField->getModelClass();
|
||||||
$owner = $gridField->Form->getRecord();
|
$owner = $gridField->Form->getRecord();
|
||||||
|
|
||||||
|
/** @var DataList $items */
|
||||||
$items = clone $gridField->getList();
|
$items = clone $gridField->getList();
|
||||||
|
|
||||||
$many_many = ($items instanceof ManyManyList);
|
$many_many = ($items instanceof ManyManyList);
|
||||||
$sortColumn = $this->sortColumn;
|
$sortColumn = $this->sortColumn;
|
||||||
$targetItem = $items->byID(intval($data['ItemID']));
|
$targetItem = $items->byID(intval($data['ItemID']));
|
||||||
@ -524,18 +589,16 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
$currentPage = 1;
|
$currentPage = 1;
|
||||||
|
|
||||||
|
|
||||||
$pageState = $gridField->State->GridFieldPaginator;
|
$pageState = $gridField->State->GridFieldPaginator;
|
||||||
if ($pageState->currentPage && $pageState->currentPage > 1) {
|
if ($pageState->currentPage && $pageState->currentPage > 1) {
|
||||||
$currentPage = $pageState->currentPage;
|
$currentPage = $pageState->currentPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($many_many) {
|
if ($many_many) {
|
||||||
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name : $gridField->getName()));
|
||||||
|
$table = $this->mapTableNameAndReturn($table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($data['Target'] == 'previouspage') {
|
if ($data['Target'] == 'previouspage') {
|
||||||
$items = $items->limit($paginator->getItemsPerPage() + 1, ($paginator->getItemsPerPage() * ($currentPage - 1)) - 1);
|
$items = $items->limit($paginator->getItemsPerPage() + 1, ($paginator->getItemsPerPage() * ($currentPage - 1)) - 1);
|
||||||
} else if ($data['Target'] == 'nextpage') {
|
} else if ($data['Target'] == 'nextpage') {
|
||||||
@ -546,7 +609,6 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
$sortPositions = $items->column($sortColumn);
|
$sortPositions = $items->column($sortColumn);
|
||||||
|
|
||||||
|
|
||||||
//Event to notify the Controller or owner DataObject before list sort
|
//Event to notify the Controller or owner DataObject before list sort
|
||||||
if ($owner && $owner instanceof DataObject && method_exists($owner, 'onBeforeGridFieldPageSort')) {
|
if ($owner && $owner instanceof DataObject && method_exists($owner, 'onBeforeGridFieldPageSort')) {
|
||||||
$owner->onBeforeGridFieldPageSort(clone $items);
|
$owner->onBeforeGridFieldPageSort(clone $items);
|
||||||
@ -554,15 +616,14 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
Controller::curr()->onBeforeGridFieldPageSort(clone $items);
|
Controller::curr()->onBeforeGridFieldPageSort(clone $items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Find the sort column
|
//Find the sort column
|
||||||
if($this->update_versioned_stage && Object::has_extension($className, 'Versioned')) {
|
if ($this->update_versioned_stage && $this->hasVersionedExtension($className)) {
|
||||||
$table = false;
|
$table = false;
|
||||||
$classes = ClassInfo::ancestry($className, true);
|
$classes = ClassInfo::ancestry($className, true);
|
||||||
foreach ($classes as $class) {
|
foreach ($classes as $class) {
|
||||||
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
||||||
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
if (!empty($db) && array_key_exists($sortColumn, $db)) {
|
||||||
$table=$class;
|
$table = $this->mapTableNameAndReturn($class);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -573,10 +634,9 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Start transaction if supported
|
//Start transaction if supported
|
||||||
if(DB::getConn()->supportsTransactions()) {
|
if (DB::get_conn()->supportsTransactions()) {
|
||||||
DB::getConn()->transactionStart();
|
DB::get_conn()->transactionStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($data['Target'] == 'previouspage') {
|
if ($data['Target'] == 'previouspage') {
|
||||||
@ -588,14 +648,13 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
$targetItem->$sortColumn = $sortPositions[0];
|
$targetItem->$sortColumn = $sortPositions[0];
|
||||||
$targetItem->write();
|
$targetItem->write();
|
||||||
|
|
||||||
if($this->update_versioned_stage && Object::has_extension($className, 'Versioned')) {
|
if ($this->update_versioned_stage && $this->hasVersionedExtension($className)) {
|
||||||
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
||||||
. '" SET "' . $sortColumn . '" = ' . $sortPositions[0]
|
. '" SET "' . $sortColumn . '" = ' . $sortPositions[0]
|
||||||
. ' WHERE "ID" = ' . $targetItem->ID);
|
. ' WHERE "ID" = ' . $targetItem->ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$i = 1;
|
$i = 1;
|
||||||
foreach ($items as $obj) {
|
foreach ($items as $obj) {
|
||||||
if ($obj->ID == $targetItem->ID) {
|
if ($obj->ID == $targetItem->ID) {
|
||||||
@ -611,7 +670,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
$obj->$sortColumn = $sortPositions[$i];
|
$obj->$sortColumn = $sortPositions[$i];
|
||||||
$obj->write();
|
$obj->write();
|
||||||
|
|
||||||
if($this->update_versioned_stage && Object::has_extension($className, 'Versioned')) {
|
if ($this->update_versioned_stage && $this->hasVersionedExtension($className)) {
|
||||||
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
||||||
. '" SET "' . $sortColumn . '" = ' . $sortPositions[$i]
|
. '" SET "' . $sortColumn . '" = ' . $sortPositions[$i]
|
||||||
. ' WHERE "ID" = ' . $obj->ID);
|
. ' WHERE "ID" = ' . $obj->ID);
|
||||||
@ -629,14 +688,13 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
$targetItem->$sortColumn = $sortPositions[count($sortPositions) - 1];
|
$targetItem->$sortColumn = $sortPositions[count($sortPositions) - 1];
|
||||||
$targetItem->write();
|
$targetItem->write();
|
||||||
|
|
||||||
if($this->update_versioned_stage && Object::has_extension($className, 'Versioned')) {
|
if ($this->update_versioned_stage && $this->hasVersionedExtension($className)) {
|
||||||
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
||||||
. '" SET "' . $sortColumn . '" = ' . $sortPositions[count($sortPositions) - 1]
|
. '" SET "' . $sortColumn . '" = ' . $sortPositions[count($sortPositions) - 1]
|
||||||
. ' WHERE "ID" = ' . $targetItem->ID);
|
. ' WHERE "ID" = ' . $targetItem->ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($items as $obj) {
|
foreach ($items as $obj) {
|
||||||
if ($obj->ID == $targetItem->ID) {
|
if ($obj->ID == $targetItem->ID) {
|
||||||
@ -652,7 +710,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
$obj->$sortColumn = $sortPositions[$i];
|
$obj->$sortColumn = $sortPositions[$i];
|
||||||
$obj->write();
|
$obj->write();
|
||||||
|
|
||||||
if($this->update_versioned_stage && Object::has_extension($className, 'Versioned')) {
|
if ($this->update_versioned_stage && $this->hasVersionedExtension($className)) {
|
||||||
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
DB::query('UPDATE "' . $table . '_' . $this->update_versioned_stage
|
||||||
. '" SET "' . $sortColumn . '" = ' . $sortPositions[$i]
|
. '" SET "' . $sortColumn . '" = ' . $sortPositions[$i]
|
||||||
. ' WHERE "ID" = ' . $obj->ID);
|
. ' WHERE "ID" = ' . $obj->ID);
|
||||||
@ -663,13 +721,11 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//End transaction if supported
|
//End transaction if supported
|
||||||
if(DB::getConn()->supportsTransactions()) {
|
if (DB::get_conn()->supportsTransactions()) {
|
||||||
DB::getConn()->transactionEnd();
|
DB::get_conn()->transactionEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Event to notify the Controller or owner DataObject after list sort
|
//Event to notify the Controller or owner DataObject after list sort
|
||||||
if ($owner && $owner instanceof DataObject && method_exists($owner, 'onAfterGridFieldPageSort')) {
|
if ($owner && $owner instanceof DataObject && method_exists($owner, 'onAfterGridFieldPageSort')) {
|
||||||
$owner->onAfterGridFieldPageSort(clone $items);
|
$owner->onAfterGridFieldPageSort(clone $items);
|
||||||
@ -677,5 +733,34 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
Controller::curr()->onAfterGridFieldPageSort(clone $items);
|
Controller::curr()->onAfterGridFieldPageSort(clone $items);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check to see if the given class name has the Versioned extension
|
||||||
|
*
|
||||||
|
* @param Extensible|string $className
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasVersionedExtension($className)
|
||||||
|
{
|
||||||
|
return $className::has_extension(Versioned::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if $table_name is declared on the DataObject, if not returns string as given
|
||||||
|
*
|
||||||
|
* @param $tableName
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function mapTableNameAndReturn($tableName)
|
||||||
|
{
|
||||||
|
if (array_key_exists($tableName, $this->tableMap)) {
|
||||||
|
return $this->tableMap[$tableName];
|
||||||
|
}
|
||||||
|
|
||||||
|
$realName = (Config::inst()->get($tableName, 'table_name', CONFIG::UNINHERITED)) ? Config::inst()->get($tableName, 'table_name', CONFIG::UNINHERITED) : $table;
|
||||||
|
|
||||||
|
$this->tableMap[$realName] = $tableName;
|
||||||
|
|
||||||
|
return $realName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
@ -1,28 +1,56 @@
|
|||||||
<?php
|
<?php
|
||||||
class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|
||||||
|
namespace UndefinedOffset\SortableGridField\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Control\HTTPRequest;
|
||||||
|
use SilverStripe\Control\Session;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\Form;
|
||||||
|
use SilverStripe\Forms\GridField\GridField;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
||||||
|
use SilverStripe\ORM\DataList;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
use SilverStripe\Security\Member;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class GridFieldSortableRowsAutoSortTest
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
*/
|
||||||
|
class GridFieldSortableRowsAutoSortTest extends SapphireTest
|
||||||
|
{
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public static $fixture_file = 'GridFieldSortableRowsAutoSortTest.yml';
|
public static $fixture_file = 'sortablegridfield/tests/forms/GridFieldSortableRowsAutoSortTest.yml';
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $extraDataObjects = array(
|
protected static $extra_dataobjects = array(
|
||||||
'GridFieldAction_SortOrder_Player',
|
GridFieldAction_SortOrder_Player::class,
|
||||||
'GridFieldAction_SortOrder_VPlayer',
|
GridFieldAction_SortOrder_VPlayer::class,
|
||||||
'GridFieldAction_SortOrder_TestParent',
|
GridFieldAction_SortOrder_TestParent::class,
|
||||||
'GridFieldAction_SortOrder_BaseObject',
|
GridFieldAction_SortOrder_BaseObject::class,
|
||||||
'GridFieldAction_SortOrder_ChildObject'
|
GridFieldAction_SortOrder_ChildObject::class
|
||||||
);
|
);
|
||||||
|
|
||||||
public function testAutoSort() {
|
public function testAutoSort()
|
||||||
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
{
|
||||||
|
if (Member::currentUser()) {
|
||||||
|
Member::currentUser()->logOut();
|
||||||
|
}
|
||||||
|
|
||||||
$list = GridFieldAction_SortOrder_Player::get();
|
$list = GridFieldAction_SortOrder_Player::get();
|
||||||
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
||||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||||
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
$form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
||||||
|
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()));
|
||||||
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
||||||
|
|
||||||
//Insure sort ran
|
//Insure sort ran
|
||||||
@ -35,21 +63,26 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAppendToTopAutoSort() {
|
public function testAppendToTopAutoSort()
|
||||||
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
{
|
||||||
|
if (Member::currentUser()) {
|
||||||
|
Member::currentUser()->logOut();
|
||||||
|
}
|
||||||
|
|
||||||
$list = GridFieldAction_SortOrder_Player::get();
|
$list = GridFieldAction_SortOrder_Player::get();
|
||||||
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
||||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||||
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
$form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
||||||
|
|
||||||
$gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setAppendToTop(true);
|
/** @var GridFieldSortableRows $sortableRows */
|
||||||
|
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
||||||
|
$sortableRows->setAppendToTop(true);
|
||||||
|
|
||||||
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
||||||
|
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()));
|
||||||
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
||||||
|
|
||||||
//Insure sort ran
|
//Insure sort ran
|
||||||
@ -62,8 +95,11 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAutoSortVersioned() {
|
public function testAutoSortVersioned()
|
||||||
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
{
|
||||||
|
if (Member::currentUser()) {
|
||||||
|
Member::currentUser()->logOut();
|
||||||
|
}
|
||||||
|
|
||||||
//Force versioned to reset
|
//Force versioned to reset
|
||||||
Versioned::reset();
|
Versioned::reset();
|
||||||
@ -78,11 +114,11 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
|
|
||||||
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
||||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||||
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
$form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
||||||
|
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()));
|
||||||
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
||||||
|
|
||||||
|
|
||||||
@ -97,24 +133,27 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
|
|
||||||
|
|
||||||
//Force versioned over to Live stage
|
//Force versioned over to Live stage
|
||||||
Versioned::reading_stage('Live');
|
Versioned::set_reading_mode('Live');
|
||||||
|
|
||||||
//Get live instance
|
//Get live instance
|
||||||
$obj=Versioned::get_one_by_stage('GridFieldAction_SortOrder_VPlayer', 'Live', '"ID"='.$list->last()->ID);
|
$obj = Versioned::get_one_by_stage(GridFieldAction_SortOrder_VPlayer::class, 'Live', '"ID"=' . $list->last()->ID);
|
||||||
|
|
||||||
//Insure sort ran
|
//Insure sort ran
|
||||||
$this->assertEquals(3, $obj->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
$this->assertEquals(3, $obj->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
||||||
|
|
||||||
|
|
||||||
//Check for duplicates (there shouldn't be any)
|
//Check for duplicates (there shouldn't be any)
|
||||||
$list=Versioned::get_by_stage('GridFieldAction_SortOrder_VPlayer', 'Live');
|
$list = Versioned::get_by_stage(GridFieldAction_SortOrder_VPlayer::class, 'Live');
|
||||||
$count = $list->Count();
|
$count = $list->Count();
|
||||||
$indexes = count(array_unique($list->column('SortOrder')));
|
$indexes = count(array_unique($list->column('SortOrder')));
|
||||||
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAppendToTopAutoSortVersioned() {
|
public function testAppendToTopAutoSortVersioned()
|
||||||
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
{
|
||||||
|
if (Member::currentUser()) {
|
||||||
|
Member::currentUser()->logOut();
|
||||||
|
}
|
||||||
|
|
||||||
//Force versioned to reset
|
//Force versioned to reset
|
||||||
Versioned::reset();
|
Versioned::reset();
|
||||||
@ -129,15 +168,17 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
|
|
||||||
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
||||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||||
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
$form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
||||||
|
|
||||||
$gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setAppendToTop(true);
|
/** @var GridFieldSortableRows $sortableRows */
|
||||||
|
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
||||||
|
$sortableRows->setAppendToTop(true);
|
||||||
|
|
||||||
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run on Versioned stage "Stage"');
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run on Versioned stage "Stage"');
|
||||||
|
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()));
|
||||||
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
||||||
|
|
||||||
|
|
||||||
@ -152,7 +193,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
|
|
||||||
|
|
||||||
//Force versioned over to Live stage
|
//Force versioned over to Live stage
|
||||||
Versioned::reading_stage('Live');
|
Versioned::set_reading_mode('Live');
|
||||||
|
|
||||||
//Insure sort ran
|
//Insure sort ran
|
||||||
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
||||||
@ -164,26 +205,35 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAppendToTopAutoSortChild() {
|
public function testAppendToTopAutoSortChild()
|
||||||
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
{
|
||||||
|
if (Member::currentUser()) {
|
||||||
|
Member::currentUser()->logOut();
|
||||||
|
}
|
||||||
|
|
||||||
//Push the edit date into the past, we're checking this later
|
//Push the edit date into the past, we're checking this later
|
||||||
DB::query('UPDATE "GridFieldAction_SortOrder_BaseObject" SET "LastEdited"=\'' . date('Y-m-d 00:00:00', strtotime('yesterday')) . '\'');
|
DB::query('UPDATE "GridFieldAction_SortOrder_BaseObject" SET "LastEdited"=\'' . date('Y-m-d 00:00:00', strtotime('yesterday')) . '\'');
|
||||||
|
|
||||||
|
/** @var GridFieldAction_SortOrder_TestParent $parent */
|
||||||
$parent = GridFieldAction_SortOrder_TestParent::get()->first();
|
$parent = GridFieldAction_SortOrder_TestParent::get()->first();
|
||||||
|
|
||||||
|
/** @var DataList $list */
|
||||||
$list = $parent->TestRelation();
|
$list = $parent->TestRelation();
|
||||||
|
|
||||||
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
||||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||||
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
$form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
||||||
$form->loadDataFrom($parent);
|
$form->loadDataFrom($parent);
|
||||||
|
|
||||||
$gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setAppendToTop(true);
|
/** @var GridFieldSortableRows $sortableRows */
|
||||||
|
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
||||||
|
$sortableRows->setAppendToTop(true);
|
||||||
|
|
||||||
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
||||||
|
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()));
|
||||||
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request);
|
||||||
|
|
||||||
//Insure sort ran
|
//Insure sort ran
|
||||||
@ -201,53 +251,111 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_Player extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_Player
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @property int SortOrder
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_Player extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_Player';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar',
|
'Name' => 'Varchar',
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $default_sort='SortOrder';
|
private static $default_sort = 'SortOrder';
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_VPlayer extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_VPlayer
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @property int SortOrder
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_VPlayer extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_VPlayer';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar',
|
'Name' => 'Varchar',
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $default_sort = 'SortOrder';
|
private static $default_sort = 'SortOrder';
|
||||||
|
|
||||||
static $extensions=array(
|
private static $extensions = array(
|
||||||
"Versioned('Stage', 'Live')"
|
"SilverStripe\\Versioned\\Versioned('Stage', 'Live')"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_TestParent extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_TestParent
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @method GridFieldAction_SortOrder_ChildObject TestRelation
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_TestParent extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_TestParent';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar'
|
'Name' => 'Varchar'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $has_many = array(
|
private static $has_many = array(
|
||||||
'TestRelation' => 'GridFieldAction_SortOrder_ChildObject'
|
'TestRelation' => GridFieldAction_SortOrder_ChildObject::class
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_BaseObject extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_BaseObject
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_BaseObject extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_BaseObject';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar'
|
'Name' => 'Varchar'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_ChildObject extends GridFieldAction_SortOrder_BaseObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_ChildObject
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property int SortOrder
|
||||||
|
* @method GridFieldAction_SortOrder_TestParent Parent
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_ChildObject extends GridFieldAction_SortOrder_BaseObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_ChildObject';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $has_one = array(
|
private static $has_one = array(
|
||||||
'Parent'=>'GridFieldAction_SortOrder_TestParent'
|
'Parent' => GridFieldAction_SortOrder_TestParent::class
|
||||||
);
|
);
|
||||||
|
|
||||||
static $default_sort='SortOrder';
|
private static $default_sort = 'SortOrder';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SortableGridField_DummyController
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
*/
|
||||||
|
class SortableGridField_DummyController extends Controller
|
||||||
|
{
|
||||||
|
private static $url_segment = 'sortable-grid-field';
|
||||||
}
|
}
|
||||||
?>
|
|
@ -1,4 +1,4 @@
|
|||||||
GridFieldAction_SortOrder_Player:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Player:
|
||||||
player1:
|
player1:
|
||||||
Name: Player 1
|
Name: Player 1
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
@ -9,7 +9,7 @@ GridFieldAction_SortOrder_Player:
|
|||||||
Name: Player 3
|
Name: Player 3
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
|
|
||||||
GridFieldAction_SortOrder_VPlayer:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VPlayer:
|
||||||
player1:
|
player1:
|
||||||
Name: Player 1
|
Name: Player 1
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
@ -20,20 +20,20 @@ GridFieldAction_SortOrder_VPlayer:
|
|||||||
Name: Player 3
|
Name: Player 3
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
|
|
||||||
GridFieldAction_SortOrder_TestParent:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent:
|
||||||
testparent:
|
testparent:
|
||||||
Name: Test
|
Name: Test
|
||||||
|
|
||||||
GridFieldAction_SortOrder_ChildObject:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_ChildObject:
|
||||||
testitem1:
|
testitem1:
|
||||||
Name: Test 1
|
Name: Test 1
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
Parent: =>GridFieldAction_SortOrder_TestParent.testparent
|
Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent
|
||||||
testitem2:
|
testitem2:
|
||||||
Name: Test 2
|
Name: Test 2
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
Parent: =>GridFieldAction_SortOrder_TestParent.testparent
|
Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent
|
||||||
testitem3:
|
testitem3:
|
||||||
Name: Test 3
|
Name: Test 3
|
||||||
SortOrder: 0
|
SortOrder: 0
|
||||||
Parent: =>GridFieldAction_SortOrder_TestParent.testparent
|
Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent
|
@ -1,6 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
class GridFieldSortableRowsPageTest extends SapphireTest {
|
|
||||||
|
|
||||||
|
namespace UndefinedOffset\SortableGridField\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Control\HTTPRequest;
|
||||||
|
use SilverStripe\Control\Session;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\Form;
|
||||||
|
use SilverStripe\Forms\GridField\GridField;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
|
||||||
|
use SilverStripe\ORM\ArrayList;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class GridFieldSortableRowsPageTest
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
*/
|
||||||
|
class GridFieldSortableRowsPageTest extends SapphireTest
|
||||||
|
{
|
||||||
/** @var ArrayList */
|
/** @var ArrayList */
|
||||||
protected $list;
|
protected $list;
|
||||||
|
|
||||||
@ -11,66 +33,73 @@ class GridFieldSortableRowsPageTest extends SapphireTest {
|
|||||||
protected $form;
|
protected $form;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public static $fixture_file = 'GridFieldSortableRowsPageTest.yml';
|
public static $fixture_file = 'sortablegridfield/tests/forms/GridFieldSortableRowsPageTest.yml';
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $extraDataObjects = array('GridFieldAction_PageSortOrder_Team', 'GridFieldAction_PageSortOrder_VTeam');
|
protected static $extra_dataobjects = array(
|
||||||
|
GridFieldAction_PageSortOrder_Team::class,
|
||||||
|
GridFieldAction_PageSortOrder_VTeam::class
|
||||||
|
);
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp()
|
||||||
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->list = GridFieldAction_PageSortOrder_Team::get();
|
$this->list = GridFieldAction_PageSortOrder_Team::get();
|
||||||
$config = GridFieldConfig_Base::create(5)->addComponent(new GridFieldSortableRows('SortOrder'));
|
$config = GridFieldConfig_Base::create(5)->addComponent(new GridFieldSortableRows('SortOrder'));
|
||||||
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
||||||
$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
|
$this->form = new Form(new SortableGridField_DummyController(), 'mockform', FieldList::create(array($this->gridField)), FieldList::create());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortToNextPage() {
|
public function testSortToNextPage()
|
||||||
|
{
|
||||||
$this->gridField->State->GridFieldPaginator->currentPage = 1;
|
$this->gridField->State->GridFieldPaginator->currentPage = 1;
|
||||||
|
|
||||||
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team3');
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_PageSortOrder_Team', 'team3');
|
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemID'=>$team3->ID, 'Target'=>'nextpage'), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemID' => $team3->ID, 'Target' => 'nextpage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
|
|
||||||
|
$team6 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team6');
|
||||||
$team6 = $this->objFromFixture('GridFieldAction_PageSortOrder_Team', 'team6');
|
|
||||||
$this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page');
|
$this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page');
|
||||||
|
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_PageSortOrder_Team', 'team3');
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team3');
|
||||||
$this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page');
|
$this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortToPrevPage() {
|
public function testSortToPrevPage()
|
||||||
|
{
|
||||||
$this->gridField->State->GridFieldPaginator->currentPage = 2;
|
$this->gridField->State->GridFieldPaginator->currentPage = 2;
|
||||||
|
|
||||||
|
$team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team7');
|
||||||
$team7 = $this->objFromFixture('GridFieldAction_PageSortOrder_Team', 'team7');
|
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemID'=>$team7->ID, 'Target'=>'previouspage'), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemID' => $team7->ID, 'Target' => 'previouspage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
|
|
||||||
|
|
||||||
$team7 = $this->objFromFixture('GridFieldAction_PageSortOrder_Team', 'team7');
|
$team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team7');
|
||||||
$this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page');
|
$this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page');
|
||||||
|
|
||||||
$team5 = $this->objFromFixture('GridFieldAction_PageSortOrder_Team', 'team5');
|
$team5 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team5');
|
||||||
$this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page');
|
$this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortToNextPageVersioned() {
|
public function testSortToNextPageVersioned()
|
||||||
|
{
|
||||||
//Force versioned to reset
|
//Force versioned to reset
|
||||||
Versioned::reset();
|
Versioned::reset();
|
||||||
|
|
||||||
$list = GridFieldAction_PageSortOrder_VTeam::get();
|
$list = GridFieldAction_PageSortOrder_VTeam::get();
|
||||||
$this->gridField->setList($list);
|
$this->gridField->setList($list);
|
||||||
$this->gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setUpdateVersionedStage('Live');
|
|
||||||
|
/** @var GridFieldSortableRows $sortableGrid */
|
||||||
|
$sortableGrid = $this->gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
||||||
|
$sortableGrid->setUpdateVersionedStage('Live');
|
||||||
$this->gridField->State->GridFieldPaginator->currentPage = 1;
|
$this->gridField->State->GridFieldPaginator->currentPage = 1;
|
||||||
|
|
||||||
//Publish all records
|
//Publish all records
|
||||||
@ -79,23 +108,23 @@ class GridFieldSortableRowsPageTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_PageSortOrder_VTeam', 'team3');
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team3');
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemID'=>$team3->ID, 'Target'=>'nextpage'), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemID' => $team3->ID, 'Target' => 'nextpage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
|
|
||||||
|
|
||||||
$team6 = $this->objFromFixture('GridFieldAction_PageSortOrder_VTeam', 'team6');
|
$team6 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team6');
|
||||||
$this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page on Versioned stage "Stage"');
|
$this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page on Versioned stage "Stage"');
|
||||||
|
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_PageSortOrder_VTeam', 'team3');
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team3');
|
||||||
$this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page on Versioned stage "Stage"');
|
$this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page on Versioned stage "Stage"');
|
||||||
|
|
||||||
|
|
||||||
$list=Versioned::get_by_stage('GridFieldAction_PageSortOrder_VTeam', 'Live');
|
$list = Versioned::get_by_stage(GridFieldAction_PageSortOrder_VTeam::class, 'Live');
|
||||||
|
|
||||||
$team6 = $list->byID($team6->ID);
|
$team6 = $list->byID($team6->ID);
|
||||||
$this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page on Versioned stage "Live"');
|
$this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page on Versioned stage "Live"');
|
||||||
@ -104,13 +133,17 @@ class GridFieldSortableRowsPageTest extends SapphireTest {
|
|||||||
$this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page on Versioned stage "Live"');
|
$this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page on Versioned stage "Live"');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortToPrevPageVersioned() {
|
public function testSortToPrevPageVersioned()
|
||||||
|
{
|
||||||
//Force versioned to reset
|
//Force versioned to reset
|
||||||
Versioned::reset();
|
Versioned::reset();
|
||||||
|
|
||||||
$list = GridFieldAction_PageSortOrder_VTeam::get();
|
$list = GridFieldAction_PageSortOrder_VTeam::get();
|
||||||
$this->gridField->setList($list);
|
$this->gridField->setList($list);
|
||||||
$this->gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setUpdateVersionedStage('Live');
|
|
||||||
|
/** @var GridFieldSortableRows $sortableGrid */
|
||||||
|
$sortableGrid = $this->gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
||||||
|
$sortableGrid->setUpdateVersionedStage('Live');
|
||||||
$this->gridField->State->GridFieldPaginator->currentPage = 2;
|
$this->gridField->State->GridFieldPaginator->currentPage = 2;
|
||||||
|
|
||||||
//Publish all records
|
//Publish all records
|
||||||
@ -119,23 +152,23 @@ class GridFieldSortableRowsPageTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$team7 = $this->objFromFixture('GridFieldAction_PageSortOrder_VTeam', 'team7');
|
$team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team7');
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemID'=>$team7->ID, 'Target'=>'previouspage'), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemID' => $team7->ID, 'Target' => 'previouspage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
|
|
||||||
|
|
||||||
$team7 = $this->objFromFixture('GridFieldAction_PageSortOrder_VTeam', 'team7');
|
$team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team7');
|
||||||
$this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page on Versioned stage "Stage"');
|
$this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page on Versioned stage "Stage"');
|
||||||
|
|
||||||
$team5 = $this->objFromFixture('GridFieldAction_PageSortOrder_VTeam', 'team5');
|
$team5 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team5');
|
||||||
$this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page on Versioned stage "Stage"');
|
$this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page on Versioned stage "Stage"');
|
||||||
|
|
||||||
|
|
||||||
$list=Versioned::get_by_stage('GridFieldAction_PageSortOrder_VTeam', 'Live');
|
$list = Versioned::get_by_stage(GridFieldAction_PageSortOrder_VTeam::class, 'Live');
|
||||||
|
|
||||||
$team7 = $list->byID($team7->ID);
|
$team7 = $list->byID($team7->ID);
|
||||||
$this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page on Versioned stage "Live"');
|
$this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page on Versioned stage "Live"');
|
||||||
@ -145,27 +178,48 @@ class GridFieldSortableRowsPageTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_PageSortOrder_Team extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_PageSortOrder_Team
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @property string City
|
||||||
|
* @property int SortOrder
|
||||||
|
*/
|
||||||
|
class GridFieldAction_PageSortOrder_Team extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_PageSortOrder_Team';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar',
|
'Name' => 'Varchar',
|
||||||
'City' => 'Varchar',
|
'City' => 'Varchar',
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $default_sort='SortOrder';
|
private static $default_sort = 'SortOrder';
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_PageSortOrder_VTeam extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_PageSortOrder_VTeam
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @property string City
|
||||||
|
* @property int SortOrder
|
||||||
|
*/
|
||||||
|
class GridFieldAction_PageSortOrder_VTeam extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_PageSortOrder_VTeam';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar',
|
'Name' => 'Varchar',
|
||||||
'City' => 'Varchar',
|
'City' => 'Varchar',
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $default_sort='SortOrder';
|
private static $default_sort = 'SortOrder';
|
||||||
|
|
||||||
static $extensions=array(
|
private static $extensions = array(
|
||||||
"Versioned('Stage', 'Live')"
|
"SilverStripe\\Versioned\\Versioned('Stage', 'Live')"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
?>
|
|
@ -1,4 +1,4 @@
|
|||||||
GridFieldAction_PageSortOrder_Team:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team:
|
||||||
team1:
|
team1:
|
||||||
Name: Team 1
|
Name: Team 1
|
||||||
City: Cologne
|
City: Cologne
|
||||||
@ -36,7 +36,7 @@ GridFieldAction_PageSortOrder_Team:
|
|||||||
City: Auckland
|
City: Auckland
|
||||||
SortOrder: 9
|
SortOrder: 9
|
||||||
|
|
||||||
GridFieldAction_PageSortOrder_VTeam:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam:
|
||||||
team1:
|
team1:
|
||||||
Name: Team 1
|
Name: Team 1
|
||||||
City: Cologne
|
City: Cologne
|
||||||
|
@ -1,6 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
class GridFieldSortableRowsTest extends SapphireTest {
|
|
||||||
|
|
||||||
|
namespace UndefinedOffset\SortableGridField\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Control\HTTPRequest;
|
||||||
|
use SilverStripe\Control\Session;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\Form;
|
||||||
|
use SilverStripe\Forms\GridField\GridField;
|
||||||
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
||||||
|
use SilverStripe\ORM\ArrayList;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\ORM\ValidationException;
|
||||||
|
use SilverStripe\Security\Member;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
|
||||||
|
|
||||||
|
class GridFieldSortableRowsTest extends SapphireTest
|
||||||
|
{
|
||||||
/** @var ArrayList */
|
/** @var ArrayList */
|
||||||
protected $list;
|
protected $list;
|
||||||
|
|
||||||
@ -11,96 +29,128 @@ class GridFieldSortableRowsTest extends SapphireTest {
|
|||||||
protected $form;
|
protected $form;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public static $fixture_file = 'GridFieldSortableRowsTest.yml';
|
public static $fixture_file = 'sortablegridfield/tests/forms/GridFieldSortableRowsTest.yml';
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $extraDataObjects = array('GridFieldAction_SortOrder_Team', 'GridFieldAction_SortOrder_VTeam');
|
protected static $extra_dataobjects = array(
|
||||||
|
GridFieldAction_SortOrder_Team::class,
|
||||||
|
GridFieldAction_SortOrder_VTeam::class
|
||||||
|
);
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp()
|
||||||
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->list = GridFieldAction_SortOrder_Team::get();
|
$this->list = GridFieldAction_SortOrder_Team::get();
|
||||||
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
||||||
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
||||||
$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
|
$this->form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortActionWithoutCorrectPermission() {
|
public function testSortActionWithoutCorrectPermission()
|
||||||
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
{
|
||||||
$this->setExpectedException('ValidationException');
|
if (Member::currentUser()) {
|
||||||
$team1 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team1');
|
Member::currentUser()->logOut();
|
||||||
$team2 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team2');
|
}
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team3');
|
$this->setExpectedException(ValidationException::class);
|
||||||
|
$team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team1');
|
||||||
|
$team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team2');
|
||||||
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team3');
|
||||||
|
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemIDs'=>"$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
$this->assertEquals($team3->ID, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.');
|
$this->assertEquals($team3->ID, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortActionWithAdminPermission() {
|
public function testSortActionWithAdminPermission()
|
||||||
$team1 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team1');
|
{
|
||||||
$team2 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team2');
|
$team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team1');
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team3');
|
$team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team2');
|
||||||
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team3');
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemIDs'=>"$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
$this->assertEquals($team2->ID, $this->list->last()->ID, 'User should be able to sort records with ADMIN permission.');
|
$this->assertEquals($team2->ID, $this->list->last()->ID, 'User should be able to sort records with ADMIN permission.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSortActionVersioned() {
|
public function testSortActionVersioned()
|
||||||
|
{
|
||||||
//Force versioned to reset
|
//Force versioned to reset
|
||||||
Versioned::reset();
|
Versioned::reset();
|
||||||
|
|
||||||
$list = GridFieldAction_SortOrder_VTeam::get();
|
$list = GridFieldAction_SortOrder_VTeam::get();
|
||||||
$this->gridField->setList($list);
|
$this->gridField->setList($list);
|
||||||
$this->gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setUpdateVersionedStage('Live');
|
|
||||||
|
/** @var GridFieldSortableRows $sortableGrid */
|
||||||
|
$sortableGrid = $this->gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
||||||
|
$sortableGrid->setUpdateVersionedStage('Live');
|
||||||
|
|
||||||
//Publish all records
|
//Publish all records
|
||||||
foreach ($list as $item) {
|
foreach ($list as $item) {
|
||||||
$item->publish('Stage', 'Live');
|
$item->publish('Stage', 'Live');
|
||||||
}
|
}
|
||||||
|
|
||||||
$team1 = $this->objFromFixture('GridFieldAction_SortOrder_VTeam', 'team1');
|
$team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team1');
|
||||||
$team2 = $this->objFromFixture('GridFieldAction_SortOrder_VTeam', 'team2');
|
$team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team2');
|
||||||
$team3 = $this->objFromFixture('GridFieldAction_SortOrder_VTeam', 'team3');
|
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team3');
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$stateID = 'testGridStateActionField';
|
$stateID = 'testGridStateActionField';
|
||||||
Session::set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
Session::set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true))));
|
||||||
$request = new SS_HTTPRequest('POST', 'url', array('ItemIDs'=>"$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $this->form->getSecurityToken()->getName()=>$this->form->getSecurityToken()->getValue()));
|
$request = new HTTPRequest('POST', 'url', array('ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()));
|
||||||
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
$this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request);
|
||||||
|
|
||||||
$this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Stage"');
|
$this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Stage"');
|
||||||
|
|
||||||
$list=Versioned::get_by_stage('GridFieldAction_SortOrder_VTeam', 'Live');
|
$list = Versioned::get_by_stage(GridFieldAction_SortOrder_VTeam::class, 'Live');
|
||||||
$this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Live"');
|
$this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Live"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_Team extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_Team
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @property string City
|
||||||
|
* @property int SortOrder
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_Team extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_Team';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar',
|
'Name' => 'Varchar',
|
||||||
'City' => 'Varchar',
|
'City' => 'Varchar',
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
static $default_sort='SortOrder';
|
private static $default_sort = 'SortOrder';
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridFieldAction_SortOrder_VTeam extends DataObject implements TestOnly {
|
/**
|
||||||
static $db = array(
|
* Class GridFieldAction_SortOrder_VTeam
|
||||||
|
*
|
||||||
|
* @package SortableGridField\Tests
|
||||||
|
* @property string Name
|
||||||
|
* @property string City
|
||||||
|
* @property int SortOrder
|
||||||
|
*/
|
||||||
|
class GridFieldAction_SortOrder_VTeam extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'GridFieldAction_SortOrder_VTeam';
|
||||||
|
|
||||||
|
private static $db = array(
|
||||||
'Name' => 'Varchar',
|
'Name' => 'Varchar',
|
||||||
'City' => 'Varchar',
|
'City' => 'Varchar',
|
||||||
'SortOrder' => 'Int'
|
'SortOrder' => 'Int'
|
||||||
);
|
);
|
||||||
|
private static $default_sort = 'SortOrder';
|
||||||
|
|
||||||
static $default_sort='SortOrder';
|
private static $extensions = array(
|
||||||
|
"SilverStripe\\Versioned\\Versioned('Stage', 'Live')"
|
||||||
static $extensions=array(
|
|
||||||
"Versioned('Stage', 'Live')"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
?>
|
|
@ -1,4 +1,4 @@
|
|||||||
GridFieldAction_SortOrder_Team:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team:
|
||||||
team1:
|
team1:
|
||||||
Name: Team 1
|
Name: Team 1
|
||||||
City: Cologne
|
City: Cologne
|
||||||
@ -12,7 +12,7 @@ GridFieldAction_SortOrder_Team:
|
|||||||
City: Auckland
|
City: Auckland
|
||||||
SortOrder: 3
|
SortOrder: 3
|
||||||
|
|
||||||
GridFieldAction_SortOrder_VTeam:
|
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam:
|
||||||
team1:
|
team1:
|
||||||
Name: Team 1
|
Name: Team 1
|
||||||
City: Cologne
|
City: Cologne
|
||||||
|
Loading…
Reference in New Issue
Block a user