silverstripe-framework/Forms/GridField/GridFieldDeleteAction.php

173 lines
4.4 KiB
PHP
Raw Normal View History

2012-01-09 11:37:13 +01:00
<?php
namespace SilverStripe\Forms\GridField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
2012-01-09 11:37:13 +01:00
/**
2014-08-15 08:53:05 +02:00
* This class is a {@link GridField} component that adds a delete action for
* objects.
*
2014-08-15 08:53:05 +02:00
* This component also supports unlinking a relation instead of deleting the
* object.
*
* Use the {@link $removeRelation} property set in the constructor.
*
* <code>
* $action = new GridFieldDeleteAction(); // delete objects permanently
*
* // removes the relation to object instead of deleting
2014-08-15 08:53:05 +02:00
* $action = new GridFieldDeleteAction(true);
* </code>
2012-01-09 11:37:13 +01:00
*/
class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider {
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* If this is set to true, this {@link GridField_ActionProvider} will
* remove the object from the list, instead of deleting.
*
2014-08-15 08:53:05 +02:00
* 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;
2014-08-15 08:53:05 +02:00
/**
*
* @param boolean $removeRelation - true if removing the item from the list, but not deleting it
*/
public function __construct($removeRelation = false) {
$this->removeRelation = $removeRelation;
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
* Add a column 'Delete'
2014-08-15 08:53:05 +02:00
*
* @param GridField $gridField
2014-08-15 08:53:05 +02:00
* @param array $columns
2012-01-09 11:37:13 +01:00
*/
public function augmentColumns($gridField, &$columns) {
if(!in_array('Actions', $columns)) {
$columns[] = 'Actions';
}
2012-01-09 11:37:13 +01:00
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
* Return any special attributes that will be used for FormField::create_tag()
2012-01-09 11:37:13 +01:00
*
* @param GridField $gridField
* @param DataObject $record
* @param string $columnName
* @return array
*/
public function getColumnAttributes($gridField, $record, $columnName) {
2016-07-01 03:37:29 +02:00
return array('class' => 'grid-field__col-compact');
2012-01-09 11:37:13 +01:00
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
2014-08-15 08:53:05 +02:00
* Add the title
*
2012-01-09 11:37:13 +01:00
* @param GridField $gridField
* @param string $columnName
* @return array
*/
public function getColumnMetadata($gridField, $columnName) {
if($columnName == 'Actions') {
return array('title' => '');
2012-01-09 11:37:13 +01:00
}
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
* Which columns are handled by this component
2014-08-15 08:53:05 +02:00
*
* @param GridField $gridField
* @return array
2012-01-09 11:37:13 +01:00
*/
public function getColumnsHandled($gridField) {
return array('Actions');
2012-01-09 11:37:13 +01:00
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
* Which GridField actions are this component handling
*
* @param GridField $gridField
2014-08-15 08:53:05 +02:00
* @return array
2012-01-09 11:37:13 +01:00
*/
public function getActions($gridField) {
return array('deleterecord', 'unlinkrelation');
2012-01-09 11:37:13 +01:00
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
*
* @param GridField $gridField
* @param DataObject $record
* @param string $columnName
* @return string the HTML for the column
2012-01-09 11:37:13 +01:00
*/
public function getColumnContent($gridField, $record, $columnName) {
if($this->removeRelation) {
if(!$record->canEdit()) {
return null;
}
$field = GridField_FormAction::create($gridField, 'UnlinkRelation'.$record->ID, false,
2016-07-01 03:37:29 +02:00
"unlinkrelation", array('RecordID' => $record->ID))
->addExtraClass('btn btn--no-text btn--icon-md font-icon-link-broken grid-field__icon-action gridfield-button-unlink')
->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"));
} else {
if(!$record->canDelete()) {
return null;
}
2014-08-15 08:53:05 +02:00
$field = GridField_FormAction::create($gridField, 'DeleteRecord'.$record->ID, false, "deleterecord",
array('RecordID' => $record->ID))
2016-07-01 03:37:29 +02:00
->addExtraClass('gridfield-button-delete btn--icon-md font-icon-trash-bin btn--no-text grid-field__icon-action')
->setAttribute('title', _t('GridAction.Delete', "Delete"))
2012-05-28 02:05:41 +02:00
->setDescription(_t('GridAction.DELETE_DESCRIPTION','Delete'));
}
return $field->Field();
2012-01-09 11:37:13 +01:00
}
2014-08-15 08:53:05 +02:00
2012-01-09 11:37:13 +01:00
/**
* Handle the actions and apply any changes to the GridField
*
* @param GridField $gridField
* @param string $actionName
* @param mixed $arguments
* @param array $data - form data
* @throws ValidationException
2012-01-09 11:37:13 +01:00
*/
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == 'deleterecord' || $actionName == 'unlinkrelation') {
/** @var DataObject $item */
$item = $gridField->getList()->byID($arguments['RecordID']);
if(!$item) {
return;
}
2014-08-15 08:53:05 +02:00
if($actionName == 'deleterecord') {
if(!$item->canDelete()) {
throw new ValidationException(
_t('GridFieldAction_Delete.DeletePermissionsFailure',"No delete permissions"),
0
);
}
$item->delete();
} else {
if(!$item->canEdit()) {
throw new ValidationException(
_t('GridFieldAction_Delete.EditPermissionsFailure',"No permission to unlink record"),
0
);
}
$gridField->getList()->remove($item);
}
2014-08-15 08:53:05 +02:00
}
2012-01-09 11:37:13 +01:00
}
}