From 26df8bd9af6b8709a778ee6e7973b2827583e85b Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 21 Nov 2009 03:20:03 +0000 Subject: [PATCH] ENHANCEMENT Moved AssetAdmin->deletemarked() logic into AssetTableField git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@92826 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/AssetAdmin.php | 55 ----------------- code/AssetTableField.php | 59 +++++++++++++++++++ javascript/AssetTableField.js | 85 ++++++++++++++------------- javascript/lang/en_US.js | 3 +- templates/Includes/AssetTableField.ss | 6 ++ 5 files changed, 111 insertions(+), 97 deletions(-) diff --git a/code/AssetAdmin.php b/code/AssetAdmin.php index 75bbcdd5..a6e3f94e 100755 --- a/code/AssetAdmin.php +++ b/code/AssetAdmin.php @@ -359,62 +359,7 @@ HTML; return FormResponse::respond(); } } - - /** - * Perform the "delete marked" action. - * Called and returns in same way as 'save' function - */ - public function deletemarked($urlParams, $form) { - $fileList = "'" . ereg_replace(' *, *',"','",trim(addslashes($_REQUEST['FileIDs']))) . "'"; - $numFiles = 0; - $folderID = 0; - $deleteList = ''; - $brokenPageList = ''; - - if($fileList != "''") { - $files = DataObject::get("File", "\"File\".\"ID\" IN ($fileList)"); - if($files) { - foreach($files as $file) { - if($file instanceof Image) { - $file->deleteFormattedImages(); - } - if(!$folderID) { - $folderID = $file->ParentID; - } - $file->delete(); - $numFiles++; - } - if($brokenPages = Notifications::getItems('BrokenLink')) { - $brokenPageList = " ". _t('AssetAdmin.NOWBROKEN', 'These pages now have broken links:') . ''; - foreach($brokenPages as $brokenPage) { - $brokenPageList .= "
  • " . $brokenPage->Breadcrumbs(3, true) . '
  • '; - } - $brokenPageList .= ''; - Notifications::notifyByEmail("BrokenLink", "Page_BrokenLinkEmail"); - } else { - $brokenPageList = ''; - } - - $deleteList = ''; - if($folderID) { - $remaining = DB::query("SELECT COUNT(*) FROM \"File\" WHERE \"ParentID\" = $folderID")->value(); - if(!$remaining) $deleteList .= "Element.removeClassName(\$('sitetree').getTreeNodeByIdx('$folderID').getElementsByTagName('a')[0],'contents');"; - } - } else { - user_error("No files in $fileList could be found!", E_USER_ERROR); - } - } - $message = sprintf(_t('AssetAdmin.DELETEDX',"Deleted %s files.%s"),$numFiles,$brokenPageList) ; - - FormResponse::add($deleteList); - FormResponse::status_message($message, "good"); - FormResponse::add("$('Form_EditForm').getPageFromServer($('Form_EditForm_ID').value)"); - - return FormResponse::respond(); - } - - /** * Returns the content to be placed in Form_SubForm when editing a file. * Called using ajax. diff --git a/code/AssetTableField.php b/code/AssetTableField.php index ce8cae71..560eb257 100755 --- a/code/AssetTableField.php +++ b/code/AssetTableField.php @@ -244,6 +244,65 @@ class AssetTableField extends ComplexTableField { return $fieldContainer->FieldHolder(); } + /** + * @return FormField|null + */ + function DeleteMarkedButton() { + if(!$this->isReadonly() ) { + $deleteButton = new InlineFormAction( + 'deletemarked', + _t('Folder.DELSELECTED','Delete selected files'), + 'delete' + ); + $deleteButton->includeDefaultJS(false); + return $deleteButton; + } + } + + /** + * @return string HTML + */ + public function deletemarked($request) { + $fileIDs = $request->requestVar($this->Name()); + $numFiles = 0; + $brokenPageList = ''; + + if($fileIDs && count($fileIDs)) { + $files = DataObject::get( + "File", + sprintf("\"File\".\"ID\" IN (%s)", Convert::raw2sql(implode(',', $fileIDs))) + ); + if($files) { + foreach($files as $file) { + $file->delete(); + $numFiles++; + } + if($brokenPages = Notifications::getItems('BrokenLink')) { + $brokenPageList = " ". _t('AssetAdmin.NOWBROKEN', 'These pages now have broken links:') . ''; + Notifications::notifyByEmail("BrokenLink", "Page_BrokenLinkEmail"); + } else { + $brokenPageList = ''; + } + + } + } + + $response = $this->form->Controller()->getResponse(); + $response->addHeader( + 'X-Status', + sprintf( + _t('AssetAdmin.DELETEDX',"Deleted %s file(s) %s"), + $numFiles, + $brokenPageList + ) + ); + + return $response->output(); + } } ?> diff --git a/javascript/AssetTableField.js b/javascript/AssetTableField.js index 6f0cca19..170f3f23 100644 --- a/javascript/AssetTableField.js +++ b/javascript/AssetTableField.js @@ -1,3 +1,47 @@ +(function($) { + $.concrete('ss', function($){ + + $('.AssetTableField :checkbox').concrete({ + onchange: function() { + var container = this.parents('.AssetTableField'); + var input = container.find('input#deletemarked'); + if(container.find(':input[name=Files\[\]]:checked').length) { + input.removeAttr('disabled'); + } else { + input.attr('disabled', 'disabled'); + } + } + }) + + /** + * Batch delete files marked by checkboxes in the table. + * Refreshes the form field afterwards via ajax. + */ + $('.AssetTableField input#deletemarked').concrete({ + onmatch: function() { + this.attr('disabled', 'disabled'); + this._super(); + }, + onclick: function(e) { + if(!confirm(ss.i18n._t('AssetTableField.REALLYDELETE'))) return false; + + var container = this.parents('.AssetTableField'); + var self = this; + this.addClass('loading'); + $.post( + container.attr('href') + '/deletemarked', + this.parents('form').serialize(), + function(data, status) { + self.removeClass('loading'); + container[0].refresh(); + } + ); + return false; + } + }); + }); +}(jQuery)); + AssetTableField = Class.create(); AssetTableField.applyTo('#Form_EditForm_Files'); AssetTableField.prototype = { @@ -95,45 +139,4 @@ FileFilterButton.prototype = { return false; } -} - -MarkingPropertiesButton = Class.create(); -MarkingPropertiesButton.applyTo( - '#Form_EditForm_deletemarked', - "Please select some files to delete!", 'deletemarked', 'Do you really want to delete the marked files?' -); - -MarkingPropertiesButton.prototype = { - initialize: function(noneCheckedError, action, confirmMessage) { - this.noneCheckedError = noneCheckedError; - this.action = action; - this.confirmMessage = confirmMessage; - }, - - onclick: function() { - var i, list = "", checkboxes = $('Form_EditForm').elements['Files[]']; - if(!checkboxes) checkboxes = []; - if(!checkboxes.length) checkboxes = [ checkboxes ]; - for(i=0;i +
    + $DeleteMarkedButton + <% control Utility %> + $Title + <% end_control %> +
    \ No newline at end of file