2012-01-25 05:31:27 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-03-01 17:06:15 +01:00
|
|
|
* Allows to detach an item from an existing has_many or many_many relationship.
|
|
|
|
* Similar to {@link GridFieldAction_Delete}, but allows to distinguish between
|
|
|
|
* a "delete" and "detach" action in the UI - and to use both in parallel, if required.
|
2012-03-05 12:27:25 +01:00
|
|
|
* Requires the GridField to be populated with a {@link RelationList} rather than a plain {@link DataList}.
|
|
|
|
* Often used alongside {@link GridFieldRelationAdd} to add existing records to the relationship.
|
|
|
|
* For easier setup, have a look at a sample configuration in {@link GridFieldConfig_RelationEditor}.
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
class GridFieldRelationDelete implements GridField_ColumnProvider, GridField_ActionProvider {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a column 'UnlinkRelation'
|
|
|
|
*
|
|
|
|
* @param type $gridField
|
|
|
|
* @param array $columns
|
|
|
|
*/
|
|
|
|
public function augmentColumns($gridField, &$columns) {
|
2012-03-05 00:45:05 +01:00
|
|
|
if(!in_array('Actions', $columns))
|
|
|
|
$columns[] = 'Actions';
|
2012-01-25 05:31:27 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't add an title
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $columnName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumnMetadata($gridField, $columnName) {
|
2012-03-05 00:45:05 +01:00
|
|
|
if($columnName == 'Actions') {
|
2012-01-25 05:31:27 +01:00
|
|
|
return array('title' => '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Which columns are handled by this component
|
|
|
|
*
|
|
|
|
* @param type $gridField
|
|
|
|
* @return type
|
|
|
|
*/
|
|
|
|
public function getColumnsHandled($gridField) {
|
2012-03-05 00:45:05 +01:00
|
|
|
return array('Actions');
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2012-03-06 16:58:13 +01:00
|
|
|
$field = Object::create('GridField_Action',
|
2012-01-25 05:31:27 +01:00
|
|
|
$gridField,
|
|
|
|
'UnlinkRelation'.$record->ID,
|
2012-03-06 16:58:13 +01:00
|
|
|
false,
|
2012-01-25 05:31:27 +01:00
|
|
|
"unlinkrelation",
|
|
|
|
array('RecordID' => $record->ID)
|
2012-03-06 16:58:13 +01:00
|
|
|
)
|
2012-03-06 17:24:46 +01:00
|
|
|
->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"))
|
|
|
|
->setAttribute('data-icon', 'chain--minus');
|
2012-03-06 16:58:13 +01:00
|
|
|
return $field->Field();
|
2012-01-25 05:31:27 +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) {
|
|
|
|
$id = $arguments['RecordID'];
|
|
|
|
$item = $gridField->getList()->byID($id);
|
|
|
|
if(!$item) return;
|
|
|
|
if($actionName == 'unlinkrelation') {
|
|
|
|
$gridField->getList()->remove($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|