FEATURE set file metadata on upload. (from r97780) (from r99117)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@111601 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-04 05:21:21 +00:00
parent 5b2897381b
commit b212b5c98b
2 changed files with 61 additions and 21 deletions

View File

@ -85,6 +85,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");
@ -96,6 +98,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.
*/
@ -127,7 +146,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();
@ -135,28 +153,35 @@ JS
$errorsArr = '';
$status = '';
$statusMessage = '';
$processedFiles = array();
foreach($data['Files'] as $param => $files) {
if(!is_array($files)) $files = array($files);
foreach($files as $key => $value) {
$processedFiles[$key][$param] = $value;
}
}
array_shift($processedFiles);
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;
// 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++;
}
}
} else {
$proccessedFiles[] = $data['Files'];
}
// 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');
}
foreach($processedFiles as $tmpFile) {
$processedData = array_reverse($processedData);
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) {
$errorsArr[] = _t('AssetAdmin.NOTEMP', 'There is no temporary folder for uploads. Please set upload_tmp_dir in php.ini.');
break;
@ -184,7 +209,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 %>