2012-01-09 11:37:13 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
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
|
2013-05-20 12:18:07 +02:00
|
|
|
* objects.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* This component also supports unlinking a relation instead of deleting the
|
2013-05-20 12:18:07 +02:00
|
|
|
* object.
|
2012-03-22 22:50:17 +01:00
|
|
|
*
|
2012-03-22 22:59:17 +01:00
|
|
|
* Use the {@link $removeRelation} property set in the constructor.
|
2012-03-22 22:50:17 +01:00
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $action = new GridFieldDeleteAction(); // delete objects permanently
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
|
|
|
* // removes the relation to object instead of deleting
|
2014-08-15 08:53:05 +02:00
|
|
|
* $action = new GridFieldDeleteAction(true);
|
2012-03-22 22:50:17 +01:00
|
|
|
* </code>
|
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-01-09 11:37:13 +01:00
|
|
|
*/
|
2012-03-06 12:57:16 +01:00
|
|
|
class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01: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.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* In the case of a has one, has many or many many list it will uncouple
|
2013-05-20 12:18:07 +02:00
|
|
|
* the item from the list.
|
2012-03-09 01:55:49 +01:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $removeRelation = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01:00
|
|
|
/**
|
|
|
|
*
|
2012-03-22 22:51:23 +01:00
|
|
|
* @param boolean $removeRelation - true if removing the item from the list, but not deleting it
|
2012-03-09 01:55:49 +01:00
|
|
|
*/
|
2012-03-22 22:51:23 +01:00
|
|
|
public function __construct($removeRelation = false) {
|
|
|
|
$this->removeRelation = $removeRelation;
|
2012-03-09 01:55:49 +01:00
|
|
|
}
|
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
|
|
|
*
|
2013-06-21 00:32:08 +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) {
|
2012-03-22 22:51:23 +01:00
|
|
|
if(!in_array('Actions', $columns)) {
|
2012-02-07 23:21:34 +01:00
|
|
|
$columns[] = 'Actions';
|
2012-03-22 22:51:23 +01:00
|
|
|
}
|
2012-01-09 11:37:13 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 11:37:13 +01:00
|
|
|
/**
|
2012-11-15 02:19:57 +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) {
|
2012-03-12 10:23:59 +01:00
|
|
|
return array('class' => 'col-buttons');
|
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) {
|
2012-02-07 23:21:34 +01:00
|
|
|
if($columnName == 'Actions') {
|
2012-01-09 17:16:07 +01:00
|
|
|
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
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
2012-01-09 11:37:13 +01:00
|
|
|
*/
|
|
|
|
public function getColumnsHandled($gridField) {
|
2012-02-07 23:21:34 +01:00
|
|
|
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) {
|
2012-03-09 01:55:49 +01:00
|
|
|
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
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string - the HTML for the column
|
2012-01-09 11:37:13 +01:00
|
|
|
*/
|
|
|
|
public function getColumnContent($gridField, $record, $columnName) {
|
2012-03-09 01:55:49 +01:00
|
|
|
if($this->removeRelation) {
|
2012-12-17 00:46:51 +01:00
|
|
|
if(!$record->canEdit()) return;
|
|
|
|
|
2015-06-19 07:43:05 +02:00
|
|
|
$field = GridField_FormAction::create($gridField, 'UnlinkRelation'.$record->ID, false,
|
2012-09-26 23:34:00 +02:00
|
|
|
"unlinkrelation", array('RecordID' => $record->ID))
|
2012-03-09 01:55:49 +01:00
|
|
|
->addExtraClass('gridfield-button-unlink')
|
|
|
|
->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"))
|
|
|
|
->setAttribute('data-icon', 'chain--minus');
|
|
|
|
} else {
|
2012-12-17 00:46:51 +01:00
|
|
|
if(!$record->canDelete()) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-19 07:43:05 +02:00
|
|
|
$field = GridField_FormAction::create($gridField, 'DeleteRecord'.$record->ID, false, "deleterecord",
|
2012-09-26 23:34:00 +02:00
|
|
|
array('RecordID' => $record->ID))
|
2012-03-09 01:55:49 +01:00
|
|
|
->addExtraClass('gridfield-button-delete')
|
|
|
|
->setAttribute('title', _t('GridAction.Delete', "Delete"))
|
2012-05-02 04:56:10 +02:00
|
|
|
->setAttribute('data-icon', 'cross-circle')
|
2012-05-28 02:05:41 +02:00
|
|
|
->setDescription(_t('GridAction.DELETE_DESCRIPTION','Delete'));
|
2012-03-08 01:58:53 +01:00
|
|
|
}
|
2012-03-06 16:58:13 +01:00
|
|
|
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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
2012-03-09 01:55:49 +01:00
|
|
|
if($actionName == 'deleterecord' || $actionName == 'unlinkrelation') {
|
|
|
|
$item = $gridField->getList()->byID($arguments['RecordID']);
|
|
|
|
if(!$item) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-22 00:31:34 +02:00
|
|
|
if($actionName == 'deleterecord') {
|
2012-12-17 00:46:51 +01:00
|
|
|
if(!$item->canDelete()) {
|
|
|
|
throw new ValidationException(
|
|
|
|
_t('GridFieldAction_Delete.DeletePermissionsFailure',"No delete permissions"),0);
|
|
|
|
}
|
|
|
|
|
2012-08-22 00:31:34 +02:00
|
|
|
$item->delete();
|
|
|
|
} else {
|
2012-12-17 00:46:51 +01:00
|
|
|
if(!$item->canEdit()) {
|
|
|
|
throw new ValidationException(
|
|
|
|
_t('GridFieldAction_Delete.EditPermissionsFailure',"No permission to unlink record"),0);
|
|
|
|
}
|
|
|
|
|
2012-08-22 00:31:34 +02:00
|
|
|
$gridField->getList()->remove($item);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-01-09 11:37:13 +01:00
|
|
|
}
|
2012-03-22 22:50:17 +01:00
|
|
|
}
|