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
This commit is contained in:
Ingo Schommer 2009-11-21 03:20:03 +00:00
parent 4d60aa5c83
commit 26df8bd9af
5 changed files with 111 additions and 97 deletions

View File

@ -359,62 +359,7 @@ HTML;
return FormResponse::respond(); 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:') . '</ul>';
foreach($brokenPages as $brokenPage) {
$brokenPageList .= "<li style=&quot;font-size: 65%&quot;>" . $brokenPage->Breadcrumbs(3, true) . '</li>';
}
$brokenPageList .= '</ul>';
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. * Returns the content to be placed in Form_SubForm when editing a file.
* Called using ajax. * Called using ajax.

View File

@ -244,6 +244,65 @@ class AssetTableField extends ComplexTableField {
return $fieldContainer->FieldHolder(); 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:') . '<ul>';
foreach($brokenPages as $brokenPage) {
$brokenPageList .= "<li style=&quot;font-size: 65%&quot;>" . $brokenPage->Breadcrumbs(3, true) . '</li>';
}
$brokenPageList .= '</ul>';
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();
}
} }
?> ?>

View File

@ -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 = Class.create();
AssetTableField.applyTo('#Form_EditForm_Files'); AssetTableField.applyTo('#Form_EditForm_Files');
AssetTableField.prototype = { AssetTableField.prototype = {
@ -95,45 +139,4 @@ FileFilterButton.prototype = {
return false; 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<checkboxes.length;i++) {
if(checkboxes[i].checked) list += (list?',':'') + checkboxes[i].value;
}
if(list == "") {
alert(this.noneCheckedError);
return false;
} else {
$('Form_EditForm_FileIDs').value = list;
}
// If there is a confirmation message, show it before submitting
if('' != this.confirmMessage) {
// Only submit if OK button is clicked
if (confirm(this.confirmMessage)) {
$('Form_EditForm').save(false, null, this.action);
}
} else {
$('Form_EditForm').save(false, null, this.action);
}
return false;
}
} }

View File

@ -28,6 +28,7 @@ if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
'LeftAndMain.CONFIRMUNSAVEDSHORT': "WARNING: Your changes have not been saved.", 'LeftAndMain.CONFIRMUNSAVEDSHORT': "WARNING: Your changes have not been saved.",
'CMSMAIN.ALERTCLASSNAME': 'The page type will be updated after the page is saved', 'CMSMAIN.ALERTCLASSNAME': 'The page type will be updated after the page is saved',
'CMSMAIN.URLSEGMENTVALIDATION': 'URLs can only be made up of letters, digits and hyphens.', 'CMSMAIN.URLSEGMENTVALIDATION': 'URLs can only be made up of letters, digits and hyphens.',
'AssetAdmin.BATCHACTIONSDELETECONFIRM': "Do you really want to delete %s folders?" 'AssetAdmin.BATCHACTIONSDELETECONFIRM': "Do you really want to delete %s folders?",
'AssetTableField.REALLYDELETE': 'Do you really want to delete the marked files?'
}); });
} }

View File

@ -30,4 +30,10 @@
<% end_if %> <% end_if %>
</tbody> </tbody>
</table> </table>
<div class="utility">
$DeleteMarkedButton
<% control Utility %>
<span class="item"><a href="$Link">$Title</a></span>
<% end_control %>
</div>
</div> </div>