2012-08-01 05:00:43 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This class is a {@link GridField} component that adds a delete a DMS document.
|
|
|
|
*
|
|
|
|
* This component also supports unlinking a relation instead of deleting the object. By default it unlinks, but if
|
|
|
|
* this is the last reference to a specific document, it warns the user that continuing with the operation will
|
|
|
|
* delete the document completely.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $action = new GridFieldDeleteAction(); // delete objects permanently
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @package dms
|
|
|
|
* @subpackage cms
|
|
|
|
*/
|
2017-05-01 05:54:48 +02:00
|
|
|
class DMSGridFieldDeleteAction extends GridFieldDeleteAction implements
|
|
|
|
GridField_ColumnProvider,
|
|
|
|
GridField_ActionProvider
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $record
|
|
|
|
* @param string $columnName
|
|
|
|
* @return string - the HTML for the column
|
|
|
|
*/
|
|
|
|
public function getColumnContent($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
if ($this->removeRelation) {
|
2017-05-01 05:54:48 +02:00
|
|
|
$field = GridField_FormAction::create(
|
|
|
|
$gridField,
|
|
|
|
'UnlinkRelation' . $record->ID,
|
|
|
|
false,
|
|
|
|
"unlinkrelation",
|
|
|
|
array('RecordID' => $record->ID)
|
|
|
|
)
|
2015-12-17 19:48:37 +01:00
|
|
|
->addExtraClass('gridfield-button-unlink')
|
|
|
|
->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"))
|
|
|
|
->setAttribute('data-icon', 'chain--minus');
|
|
|
|
} else {
|
|
|
|
if (!$record->canDelete()) {
|
2017-05-01 05:54:48 +02:00
|
|
|
return '';
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
2017-05-01 05:54:48 +02:00
|
|
|
|
|
|
|
$field = GridField_FormAction::create(
|
|
|
|
$gridField,
|
|
|
|
'DeleteRecord' . $record->ID,
|
|
|
|
false,
|
|
|
|
"deleterecord",
|
|
|
|
array('RecordID' => $record->ID)
|
|
|
|
)
|
2015-12-17 19:48:37 +01:00
|
|
|
->addExtraClass('gridfield-button-delete')
|
|
|
|
->setAttribute('title', _t('GridAction.Delete', "Delete"))
|
|
|
|
->setAttribute('data-icon', 'cross-circle')
|
|
|
|
->setDescription(_t('GridAction.DELETE_DESCRIPTION', 'Delete'));
|
|
|
|
}
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Add a class to the field to if it is the last gridfield in the list
|
2017-05-02 04:49:41 +02:00
|
|
|
$numberOfRelations = $record->getRelatedPages()->count();
|
2017-05-01 05:54:48 +02:00
|
|
|
$field
|
|
|
|
// Add a new class for custom JS to handle the delete action
|
|
|
|
->addExtraClass('dms-delete')
|
|
|
|
// Add the number of pages attached to this field as a data-attribute
|
|
|
|
->setAttribute('data-pages-count', $numberOfRelations)
|
|
|
|
// Remove the base gridfield behaviour
|
|
|
|
->removeExtraClass('gridfield-button-delete');
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Set a class telling JS what kind of warning to display when clicking the delete button
|
2015-12-17 19:48:37 +01:00
|
|
|
if ($numberOfRelations > 1) {
|
|
|
|
$field->addExtraClass('dms-delete-link-only');
|
|
|
|
} else {
|
|
|
|
$field->addExtraClass('dms-delete-last-warning');
|
|
|
|
}
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Set a class to show if the document is hidden
|
2015-12-17 19:48:37 +01:00
|
|
|
if ($record->isHidden()) {
|
|
|
|
$field->addExtraClass('dms-document-hidden');
|
|
|
|
}
|
2012-08-14 03:03:35 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
return $field->Field();
|
|
|
|
}
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2015-12-17 19:48:37 +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
|
2017-05-01 05:54:48 +02:00
|
|
|
* @throws ValidationException If the current user doesn't have permission to delete the record
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
|
|
|
{
|
|
|
|
if ($actionName == 'deleterecord' || $actionName == 'unlinkrelation') {
|
|
|
|
$item = $gridField->getList()->byID($arguments['RecordID']);
|
|
|
|
if (!$item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($actionName == 'deleterecord' && !$item->canDelete()) {
|
2017-05-01 05:54:48 +02:00
|
|
|
throw new ValidationException(
|
|
|
|
_t('GridFieldAction_Delete.DeletePermissionsFailure', "No delete permissions"),
|
|
|
|
0
|
|
|
|
);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
$delete = false;
|
2017-05-05 04:51:52 +02:00
|
|
|
if ($item->getRelatedPages()->count() <= 1) {
|
2015-12-17 19:48:37 +01:00
|
|
|
$delete = true;
|
|
|
|
}
|
2012-08-01 05:00:43 +02:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Remove the relation
|
|
|
|
$gridField->getList()->remove($item);
|
2015-12-17 19:48:37 +01:00
|
|
|
if ($delete) {
|
2017-05-01 05:54:48 +02:00
|
|
|
// Delete the document
|
2015-12-17 19:48:37 +01:00
|
|
|
$item->delete();
|
2017-05-01 05:54:48 +02:00
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 05:00:43 +02:00
|
|
|
}
|