silverstripe-framework/forms/gridfield/GridFieldDeleteAction.php

109 lines
2.7 KiB
PHP
Raw Normal View History

2012-01-09 11:37:13 +01:00
<?php
/**
2012-03-01 17:06:15 +01:00
* This class is an GridField Component that add Delete action for Objects in the GridField.
* See {@link GridFieldRelationDelete} for detaching an item from the current relationship instead.
2012-01-09 11:37:13 +01:00
*/
class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider {
2012-01-09 11:37:13 +01:00
/**
* Add a column 'Delete'
*
* @param type $gridField
* @param array $columns
*/
public function augmentColumns($gridField, &$columns) {
if(!in_array('Actions', $columns))
$columns[] = 'Actions';
2012-01-09 11:37:13 +01:00
}
/**
* 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();
}
/**
* Add the title
*
* @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
}
}
/**
* Which columns are handled by this component
*
* @param type $gridField
* @return type
*/
public function getColumnsHandled($gridField) {
return array('Actions');
2012-01-09 11:37:13 +01:00
}
/**
* Which GridField actions are this component handling
*
* @param GridField $gridField
* @return array
*/
public function getActions($gridField) {
return array('deleterecord');
}
/**
*
* @param GridField $gridField
* @param DataObject $record
* @param string $columnName
* @return string - the HTML for the column
*/
public function getColumnContent($gridField, $record, $columnName) {
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');
return $field->Field();
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) {
if($actionName == 'deleterecord') {
$id = $arguments['RecordID'];
2012-03-01 17:06:15 +01:00
// Always deletes a record. Use GridFieldRelationDelete to detach it from the current relationship.
$item = $gridField->getList()->byID($id);
if(!$item->canDelete()) {
throw new ValidationException(_t('GridFieldAction_Delete.DeletePermissionsFailure',"No delete permissions"),0);
}
if(!$item) return;
$item->delete();
2012-01-09 11:37:13 +01:00
}
}
}