Skip to content
Browse files

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
  • Loading branch information...
1 parent 5b28973 commit b212b5c98bcc88c4a979ac2ada0a0008f3d2554d @sminnee sminnee committed Oct 4, 2010
Showing with 61 additions and 21 deletions.
  1. +57 −20 code/AssetAdmin.php
  2. +4 −1 templates/AssetAdmin_uploadiframe.ss
View
77 code/AssetAdmin.php
@@ -85,6 +85,8 @@ function uploadiframe() {
//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");
@@ -97,6 +99,23 @@ function uploadiframe() {
}
/**
+ * @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.
*/
function UploadForm() {
@@ -127,36 +146,42 @@ function UploadForm() {
* 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();
$fileSizeWarnings = '';
$errorsArr = '';
$status = '';
$statusMessage = '';
-
- 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;
- }
+ $processedFiles = array();
+
+ 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++;
+ }
+ }
}
-
- 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 @@ function doUpload($data, $form) {
}
// 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
5 templates/AssetAdmin_uploadiframe.ss
@@ -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 %>

0 comments on commit b212b5c

Please sign in to comment.
Something went wrong with that request. Please try again.