diff --git a/forms/gridfield/GridFieldConfig.php b/forms/gridfield/GridFieldConfig.php index 5cb804989..0cc589a0a 100755 --- a/forms/gridfield/GridFieldConfig.php +++ b/forms/gridfield/GridFieldConfig.php @@ -206,7 +206,7 @@ class GridFieldConfig_RelationEditor extends GridFieldConfig { $this->addComponent(new GridFieldFilterHeader()); $this->addComponent(new GridFieldDefaultColumns()); $this->addComponent(new GridFieldEditButton()); - $this->addComponent(new GridFieldRemoveButton()); + $this->addComponent(new GridFieldDeleteAction(true)); $this->addComponent(new GridFieldPaginator($itemsPerPage)); $this->addComponent(new GridFieldDetailForm()); } diff --git a/forms/gridfield/GridFieldDeleteAction.php b/forms/gridfield/GridFieldDeleteAction.php index 7aa505d49..2685fcfe2 100644 --- a/forms/gridfield/GridFieldDeleteAction.php +++ b/forms/gridfield/GridFieldDeleteAction.php @@ -5,6 +5,24 @@ */ class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider { + + /** + * If this is set to true, this actionprovider will remove the object from the list, instead of + * deleting. In the case of a has one, has many or many many list it will uncouple the item from + * the list. + * + * @var boolean + */ + protected $removeRelation = false; + + /** + * + * @param boolean $unlinkRelation - true if removing the item from the list, but not deleting it + */ + public function __construct($unlinkRelation = false) { + $this->removeRelation = $unlinkRelation; + } + /** * Add a column 'Delete' * @@ -58,7 +76,7 @@ class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_Actio * @return array */ public function getActions($gridField) { - return array('deleterecord'); + return array('deleterecord', 'unlinkrelation'); } /** @@ -69,19 +87,20 @@ class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_Actio * @return string - the HTML for the column */ public function getColumnContent($gridField, $record, $columnName) { - if(!$record->canDelete()) { - return; + if($this->removeRelation) { + $field = Object::create('GridField_FormAction', $gridField, 'UnlinkRelation'.$record->ID, false, "unlinkrelation", array('RecordID' => $record->ID)) + ->addExtraClass('gridfield-button-unlink') + ->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink")) + ->setAttribute('data-icon', 'chain--minus'); + } else { + if(!$record->canDelete()) { + return; + } + $field = Object::create('GridField_FormAction', $gridField, 'DeleteRecord'.$record->ID, false, "deleterecord", array('RecordID' => $record->ID)) + ->addExtraClass('gridfield-button-delete') + ->setAttribute('title', _t('GridAction.Delete', "Delete")) + ->setAttribute('data-icon', 'decline'); } - $field = Object::create('GridField_FormAction', - $gridField, - 'DeleteRecord'.$record->ID, - false, - "deleterecord", - array('RecordID' => $record->ID) - ) - ->addExtraClass('gridfield-button-delete') - ->setAttribute('title', _t('GridAction.Delete', "delete")) - ->setAttribute('data-icon', 'decline'); return $field->Field(); } @@ -95,15 +114,15 @@ class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_Actio * @return void */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { - if($actionName == 'deleterecord') { - $id = $arguments['RecordID']; - // Always deletes a record. Use GridFieldRemoveButton to detach it from the current relationship. - $item = $gridField->getList()->byID($id); - if(!$item->canDelete()) { + if($actionName == 'deleterecord' || $actionName == 'unlinkrelation') { + $item = $gridField->getList()->byID($arguments['RecordID']); + if(!$item) { + return; + } + if($actionName == 'deleterecord' && !$item->canDelete()) { throw new ValidationException(_t('GridFieldAction_Delete.DeletePermissionsFailure',"No delete permissions"),0); } - if(!$item) return; - $item->delete(); - } + $gridField->getList()->remove($item); + } } } \ No newline at end of file diff --git a/forms/gridfield/GridFieldRemoveButton.php b/forms/gridfield/GridFieldRemoveButton.php deleted file mode 100644 index 732836689..000000000 --- a/forms/gridfield/GridFieldRemoveButton.php +++ /dev/null @@ -1,106 +0,0 @@ - ''); - } - } - - /** - * Which columns are handled by this component - * - * @param type $gridField - * @return type - */ - public function getColumnsHandled($gridField) { - return array('Actions'); - } - - /** - * Which GridField actions are this component handling - * - * @param GridField $gridField - * @return array - */ - public function getActions($gridField) { - return array('unlinkrelation'); - } - - /** - * - * @param GridField $gridField - * @param DataObject $record - * @param string $columnName - * @return string - the HTML for the column - */ - public function getColumnContent($gridField, $record, $columnName) { - $field = Object::create('GridField_FormAction', - $gridField, - 'UnlinkRelation'.$record->ID, - false, - "unlinkrelation", - array('RecordID' => $record->ID) - ) - ->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink")) - ->setAttribute('data-icon', 'chain--minus') - ->addExtraClass('gridfield-button-unlink'); - return $field->Field(); - } - - /** - * Handle the actions and apply any changes to the GridField - * - * @param GridField $gridField - * @param string $actionName - * @param mixed $arguments - * @param array $data - form data - * @return void - */ - public function handleAction(GridField $gridField, $actionName, $arguments, $data) { - $id = $arguments['RecordID']; - $item = $gridField->getList()->byID($id); - if(!$item) return; - if($actionName == 'unlinkrelation') { - $gridField->getList()->remove($item); - } - } -} diff --git a/tests/forms/gridfield/GridFieldDeleteActionTest.php b/tests/forms/gridfield/GridFieldDeleteActionTest.php index 750123207..3c09d7b6a 100644 --- a/tests/forms/gridfield/GridFieldDeleteActionTest.php +++ b/tests/forms/gridfield/GridFieldDeleteActionTest.php @@ -60,6 +60,23 @@ class GridFieldDeleteActionTest extends SapphireTest { $this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request); $this->assertEquals(2, $this->list->count(), 'User should be able to delete records with ADMIN permission.'); } + + public function testDeleteActionRemoveRelation() { + $this->logInWithPermission('ADMIN'); + + $config = GridFieldConfig::create()->addComponent(new GridFieldDeleteAction(true)); + + $gridField = new GridField('testfield', 'testfield', $this->list, $config); + $form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList()); + + $stateID = 'testGridStateActionField'; + Session::set($stateID, array('grid'=>'', 'actionName'=>'deleterecord','args'=>array('RecordID'=>1))); + $request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true)); + + $this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request); + $this->assertEquals(2, $this->list->count(), 'User should be able to delete records with ADMIN permission.'); + + } } class GridFieldAction_Delete_Team extends DataObject implements TestOnly {