From 236857bc800547a12c7813ffa837a6c42c80d0cd Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Wed, 1 Aug 2012 15:00:43 +1200 Subject: [PATCH] API-CHANGE: deleting a DMSDocument from the GridField working --- code/DMSDocument.php | 18 +++++- code/DMSSiteTreeExtension.php | 5 +- code/cms/DMSGridFieldDeleteAction.php | 81 +++++++++++++++++++++++++++ javascript/DMSDocumentCMSFields.js | 12 ---- javascript/DMSGridField.js | 32 +++++++++++ 5 files changed, 133 insertions(+), 15 deletions(-) create mode 100644 code/cms/DMSGridFieldDeleteAction.php create mode 100644 javascript/DMSGridField.js diff --git a/code/DMSDocument.php b/code/DMSDocument.php index cad0d08..b5e58e8 100644 --- a/code/DMSDocument.php +++ b/code/DMSDocument.php @@ -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(); diff --git a/code/DMSSiteTreeExtension.php b/code/DMSSiteTreeExtension.php index 6578d74..b6fcfbc 100644 --- a/code/DMSSiteTreeExtension.php +++ b/code/DMSSiteTreeExtension.php @@ -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') ); diff --git a/code/cms/DMSGridFieldDeleteAction.php b/code/cms/DMSGridFieldDeleteAction.php new file mode 100644 index 0000000..c178790 --- /dev/null +++ b/code/cms/DMSGridFieldDeleteAction.php @@ -0,0 +1,81 @@ + + * $action = new GridFieldDeleteAction(); // delete objects permanently + * + * + * @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 + } + } +} diff --git a/javascript/DMSDocumentCMSFields.js b/javascript/DMSDocumentCMSFields.js index 659cae5..4cced54 100644 --- a/javascript/DMSDocumentCMSFields.js +++ b/javascript/DMSDocumentCMSFields.js @@ -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); - } - - }); }); }); \ No newline at end of file diff --git a/javascript/DMSGridField.js b/javascript/DMSGridField.js new file mode 100644 index 0000000..d5574e5 --- /dev/null +++ b/javascript/DMSGridField.js @@ -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));