API CHANGE Removing GridFieldDeleteAction and moved functionality into GridFieldRemoveButton #6921

This commit is contained in:
Stig Lindqvist 2012-03-09 13:55:49 +13:00
parent 8b82dae06c
commit 6d0b0d6788
4 changed files with 58 additions and 128 deletions

View File

@ -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());
}

View File

@ -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($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)
)
$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('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);
}
}
}

View File

@ -1,106 +0,0 @@
<?php
/**
* Allows to detach an item from an existing has_many or many_many relationship.
* Similar to {@link GridFieldDeleteAction}, but allows to distinguish between
* a "delete" and "detach" action in the UI - and to use both in parallel, if required.
* Requires the GridField to be populated with a {@link RelationList} rather than a plain {@link DataList}.
* Often used alongside {@link GridFieldAddExistingAutocompleter} to add existing records to the relationship.
* For easier setup, have a look at a sample configuration in {@link GridFieldConfig_RelationEditor}.
*/
class GridFieldRemoveButton implements GridField_ColumnProvider, GridField_ActionProvider {
/**
* Add a column 'UnlinkRelation'
*
* @param type $gridField
* @param array $columns
*/
public function augmentColumns($gridField, &$columns) {
if(!in_array('Actions', $columns))
$columns[] = 'Actions';
}
/**
* Return any special attributes that will be used for FormField::createTag()
*
* @param GridField $gridField
* @param DataObject $record
* @param string $columnName
* @return array
*/
public function getColumnAttributes($gridField, $record, $columnName) {
return array();
}
/**
* Don't add an title
*
* @param GridField $gridField
* @param string $columnName
* @return array
*/
public function getColumnMetadata($gridField, $columnName) {
if($columnName == 'Actions') {
return array('title' => '');
}
}
/**
* 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);
}
}
}

View File

@ -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 {