FEATURE set file metadata on upload. (from r97780)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.4@99117 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-02-16 04:00:59 +00:00 committed by Sam Minnee
parent bf2924e159
commit 8302f97ffc
2 changed files with 61 additions and 21 deletions

View File

@ -102,6 +102,8 @@ JS
Requirements::javascript(CMS_DIR . "/javascript/LeftAndMain.js");
Requirements::javascript(CMS_DIR . "/thirdparty/multifile/multifile.js");
Requirements::css(CMS_DIR . "/thirdparty/multifile/multifile.css");
Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/jquery/jquery.js");
Requirements::javascript(SAPPHIRE_DIR . "/javascript/jquery_improvements.js");
Requirements::css(CMS_DIR . "/css/typography.css");
Requirements::css(CMS_DIR . "/css/layout.css");
Requirements::css(CMS_DIR . "/css/cms_left.css");
@ -113,6 +115,23 @@ JS
return array( 'CanUpload' => $folder->canEdit());
}
/**
* @return String
*/
function UploadMetadataHtml() {
$fields = singleton('File')->uploadMetadataFields();
// Return HTML with markers for easy replacement
$fieldHtml = '';
foreach($fields as $field) $fieldHtml = $fieldHtml . $field->FieldHolder();
$fieldHtml = preg_replace('/(name|for|id)="(.+?)"/', '$1="$2[__X__]"', $fieldHtml);
// Icky hax to fix certain elements with fixed ids
$fieldHtml = preg_replace('/-([a-zA-Z0-9]+?)\[__X__\]/', '[__X__]-$1', $fieldHtml);
return $fieldHtml;
}
/**
* Return the form object shown in the uploadiframe.
*/
@ -144,7 +163,6 @@ JS
* It will save the uploaded files to /assets/ and create new File objects as required.
*/
function doUpload($data, $form) {
$processedFiles = array();
$newFiles = array();
$fileIDs = array();
$fileNames = array();
@ -153,28 +171,35 @@ JS
$jsErrors = '';
$status = '';
$statusMessage = '';
$processedFiles = array();
if(!isset($data['Files'])) return Director::set_status_code("404");
if(is_array($data['Files'])) {
foreach($data['Files'] as $param => $files) {
if(!is_array($files)) $files = array($files);
foreach($files as $key => $value) {
$processedFiles[$key][$param] = $value;
}
}
} else {
$proccessedFiles[] = $data['Files'];
}
array_shift($processedFiles);
// get the folder to upload to.
if(isset($data['FolderID']) && $data['FolderID'] && $data['FolderID'] != "root") {
$folder = DataObject::get_by_id('Folder', $data['FolderID']);
} else {
$folder = singleton('Folder');
// Load POST data from arrays in to the correct dohickey.
$processedData = array();
foreach($data as $dataKey => $value) {
if ($dataKey == 'Files') continue;
if (is_array($value)) {
$i = 0;
foreach($value as $fileId => $dataValue) {
if (!isset($processedData[$i])) $processedData[$i] = array();
$processedData[$i][$dataKey] = $dataValue;
$i++;
}
}
}
$processedData = array_reverse($processedData);
foreach($processedFiles as $tmpFile) {
if($data['ID'] && $data['ID'] != 'root') $folder = DataObject::get_by_id("Folder", $data['ID']);
else $folder = singleton('Folder');
foreach($processedFiles as $filePostId => $tmpFile) {
if($tmpFile['error'] == UPLOAD_ERR_NO_TMP_DIR) {
$status = 'bad';
$statusMessage = _t('AssetAdmin.NOTEMP', 'There is no temporary folder for uploads. Please set upload_tmp_dir in php.ini.');
@ -206,7 +231,19 @@ JS
}
// move file to given folder
if($valid) $newFiles[] = $folder->addUploadToFolder($tmpFile);
if($valid) {
$newFile = $folder->addUploadToFolder($tmpFile);
if (isset($processedData[$filePostId])) {
$fileObject = DataObject::get_by_id('File', $newFile);
$metadataForm = new Form($this, 'MetadataForm', $fileObject->uploadMetadataFields(), new FieldSet());
$metadataForm->loadDataFrom($processedData[$filePostId]);
$metadataForm->saveInto($fileObject);
$fileObject->write();
}
$newFiles[] = $newFile;
}
}
}

View File

@ -1,5 +1,5 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" style="overflow:auto">
<head>
<% base_tag %>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
@ -21,6 +21,9 @@
<body>
<% if CanUpload %>
$UploadForm
<div id="metadataFormTemplate" style="display:none">
$UploadMetadataHtml
</div>
<% else %>
<% _t('PERMFAILED','You do not have permission to upload files into this folder.') %>
<% end_if %>