mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
API-CHANGE: deleting a DMSDocument from the GridField working
This commit is contained in:
parent
2800533e40
commit
236857bc80
@ -325,8 +325,22 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
|
||||
//remove tags
|
||||
$this->removeAllTags();
|
||||
|
||||
//delete the file
|
||||
if (is_file($this->getFullPath())) unlink($this->getFullPath());
|
||||
//delete the file (and previous versions of files)
|
||||
$filesToDelete = array();
|
||||
$storageFolder = DMS::get_DMS_path() . DIRECTORY_SEPARATOR . DMS::getStorageFolder($this->ID);
|
||||
if ($handle = opendir($storageFolder)) { //Open directory
|
||||
//List files in the directory
|
||||
while (false !== ($entry = readdir($handle))) {
|
||||
if(strpos($entry,$this->ID.'~') !== false) $filesToDelete[] = $entry;
|
||||
}
|
||||
closedir($handle);
|
||||
|
||||
//delete all this files that have the id of this document
|
||||
foreach($filesToDelete as $file) {
|
||||
$filePath = $storageFolder .DIRECTORY_SEPARATOR . $file;
|
||||
if (is_file($filePath)) unlink($filePath);
|
||||
}
|
||||
}
|
||||
|
||||
$this->removeAllPages();
|
||||
|
||||
|
@ -6,6 +6,9 @@ class DMSSiteTreeExtension extends DataExtension {
|
||||
);
|
||||
|
||||
function updateCMSFields(FieldList $fields){
|
||||
//javascript to customize the grid field for the DMS document (overriding entwine in FRAMEWORK_DIR.'/javascript/GridField.js'
|
||||
Requirements::javascript('dms/javascript/DMSGridField.js');
|
||||
|
||||
// Document listing
|
||||
$gridFieldConfig = GridFieldConfig::create()->addComponents(
|
||||
new GridFieldToolbarHeader(),
|
||||
@ -14,7 +17,7 @@ class DMSSiteTreeExtension extends DataExtension {
|
||||
new GridFieldDataColumns(),
|
||||
new GridFieldPaginator(15),
|
||||
new GridFieldEditButton(),
|
||||
new GridFieldDeleteAction(),
|
||||
new DMSGridFieldDeleteAction(), //special delete dialog to handle custom behaviour of unlinking and deleting
|
||||
new GridFieldDetailForm()
|
||||
//GridFieldLevelup::create($folder->ID)->setLinkSpec('admin/assets/show/%d')
|
||||
);
|
||||
|
81
code/cms/DMSGridFieldDeleteAction.php
Normal file
81
code/cms/DMSGridFieldDeleteAction.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?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
|
||||
*/
|
||||
class DMSGridFieldDeleteAction extends GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider {
|
||||
|
||||
/**
|
||||
*
|
||||
* @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) {
|
||||
$field = GridField_FormAction::create($gridField, 'UnlinkRelation'.$record->ID, false, "unlinkrelation", array('RecordID' => $record->ID))
|
||||
->addExtraClass('gridfield-button-unlink')
|
||||
->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"))
|
||||
->setAttribute('data-icon', 'chain--minus');
|
||||
} else {
|
||||
if(!$record->canDelete()) {
|
||||
return;
|
||||
}
|
||||
$field = GridField_FormAction::create($gridField, 'DeleteRecord'.$record->ID, false, "deleterecord", array('RecordID' => $record->ID))
|
||||
->addExtraClass('gridfield-button-delete')
|
||||
->setAttribute('title', _t('GridAction.Delete', "Delete"))
|
||||
->setAttribute('data-icon', 'cross-circle')
|
||||
->setDescription(_t('GridAction.DELETE_DESCRIPTION','Delete'));
|
||||
}
|
||||
|
||||
//add a class to the field to if it is the last gridfield in the list
|
||||
$numberOfRelations = $record->Pages()->Count();
|
||||
$field->addExtraClass('dms-delete') //add a new class for custom JS to handle the delete action
|
||||
->setAttribute('data-pages-count', $numberOfRelations) //add the number of pages attached to this field as a data-attribute
|
||||
->removeExtraClass('gridfield-button-delete'); //remove the base gridfield behaviour
|
||||
|
||||
//set a class telling JS what kind of warning to display when clicking the delete button
|
||||
if ($numberOfRelations > 1) $field->addExtraClass('dms-delete-link-only');
|
||||
else $field->addExtraClasS('dms-delete-last-warning');
|
||||
|
||||
return $field->Field();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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' || $actionName == 'unlinkrelation') {
|
||||
$item = $gridField->getList()->byID($arguments['RecordID']);
|
||||
if(!$item) {
|
||||
return;
|
||||
}
|
||||
if($actionName == 'deleterecord' && !$item->canDelete()) {
|
||||
throw new ValidationException(_t('GridFieldAction_Delete.DeletePermissionsFailure',"No delete permissions"),0);
|
||||
}
|
||||
|
||||
$delete = false;
|
||||
if ($item->Pages()->Count() <= 1) $delete = true;
|
||||
|
||||
$gridField->getList()->remove($item); //remove the relation
|
||||
if ($delete) $item->delete(); //delete the DMSDocument
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +1,6 @@
|
||||
(function($) {
|
||||
|
||||
$.entwine('ss', function($) {
|
||||
$('.ui-state-success-text').entwine({
|
||||
|
||||
onmatch: function() {
|
||||
var form = this.closest('.cms-edit-form');
|
||||
console.log(form);
|
||||
},
|
||||
|
||||
onunmatch: function() {
|
||||
var form = this.closest('.cms-edit-form');
|
||||
console.log(form);
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
});
|
32
javascript/DMSGridField.js
Normal file
32
javascript/DMSGridField.js
Normal file
@ -0,0 +1,32 @@
|
||||
(function($){
|
||||
|
||||
$.entwine('ss', function($) {
|
||||
|
||||
|
||||
$('.ss-gridfield .action.dms-delete').entwine({
|
||||
onclick: function(e){
|
||||
//work out how many pages are left attached to this document
|
||||
var pagesCount = this.data('pages-count');
|
||||
var pagesCountAfterDeletion = pagesCount - 1;
|
||||
var addS = 's';
|
||||
if (pagesCountAfterDeletion == 1) addS = '';
|
||||
|
||||
//display an appropriate message
|
||||
var message = '';
|
||||
if (this.hasClass('dms-delete-last-warning')) message = "Permanently delete this document?\n\nWarning: this document is attached only to this page, deleting it here will delete it permanently.";
|
||||
if (this.hasClass('dms-delete-link-only')) message = "Unlink this document from this page?\n\nNote: it will remain attached to "+pagesCountAfterDeletion+" other page"+addS+".";
|
||||
|
||||
if(!confirm(message)) {
|
||||
e.preventDefault();
|
||||
return false;
|
||||
} else {
|
||||
//user says "okay", so go ahead and do the action
|
||||
this._super(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
}(jQuery));
|
Loading…
Reference in New Issue
Block a user