From 66a7a0a7385cc243821568e94398e335a9379ca1 Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Wed, 1 Aug 2012 17:59:22 +1200 Subject: [PATCH] Edit form for upload --- code/DMSDocument.php | 3 +- code/cms/DMSUploadField.php | 116 ++++++++++++++++++ javascript/DMSUploadField_downloadtemplate.js | 3 + 3 files changed, 120 insertions(+), 2 deletions(-) diff --git a/code/DMSDocument.php b/code/DMSDocument.php index 3d0821f..183721c 100644 --- a/code/DMSDocument.php +++ b/code/DMSDocument.php @@ -544,7 +544,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { CompositeField::create( new ReadonlyField("ID", "ID number". ':', $this->ID), new ReadonlyField("FileType", _t('AssetTableField.TYPE','File type') . ':', self::get_file_type($extension)), - new ReadonlyField("Size", _t('AssetTableField.SIZE','File size') . ':', File::format_size(filesize($this->getFullPath()))), + new ReadonlyField("Size", _t('AssetTableField.SIZE','File size') . ':', $this->getFileSizeFormatted()), $urlField = new ReadonlyField('ClickableURL', _t('AssetTableField.URL','URL'), sprintf('%s', $this->getDownloadLink(), $this->getDownloadLink()) ), @@ -571,7 +571,6 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { $this->replaceDocument($file); $file->delete(); } - } class DMSDocument_Controller extends Controller { diff --git a/code/cms/DMSUploadField.php b/code/cms/DMSUploadField.php index 9e21047..ccdde6e 100644 --- a/code/cms/DMSUploadField.php +++ b/code/cms/DMSUploadField.php @@ -27,6 +27,107 @@ class DMSUploadField extends UploadField { $document = $dms->storeDocument($file); $file->delete(); $document->addPage($page); + + return $document; + } + + /** + * Action to handle upload of a single file + * + * @param SS_HTTPRequest $request + * @return string json + */ + public function upload(SS_HTTPRequest $request) { + if($this->isDisabled() || $this->isReadonly()) return $this->httpError(403); + + // Protect against CSRF on destructive action + $token = $this->getForm()->getSecurityToken(); + if(!$token->checkRequest($request)) return $this->httpError(400); + + $name = $this->getName(); + $tmpfile = $request->postVar($name); + $record = $this->getRecord(); + + // Check if the file has been uploaded into the temporary storage. + if (!$tmpfile) { + $return = array('error' => _t('UploadField.FIELDNOTSET', 'File information not found')); + } else { + $return = array( + 'name' => $tmpfile['name'], + 'size' => $tmpfile['size'], + 'type' => $tmpfile['type'], + 'error' => $tmpfile['error'] + ); + } + + // Check for constraints on the record to which the file will be attached. + if (!$return['error'] && $this->relationAutoSetting && $record && $record->exists()) { + $tooManyFiles = false; + // Some relationships allow many files to be attached. + if ($this->getConfig('allowedMaxFileNumber') && ($record->has_many($name) || $record->many_many($name))) { + if(!$record->isInDB()) $record->write(); + $tooManyFiles = $record->{$name}()->count() >= $this->getConfig('allowedMaxFileNumber'); + // has_one only allows one file at any given time. + } elseif($record->has_one($name)) { + $tooManyFiles = $record->{$name}() && $record->{$name}()->exists(); + } + + // Report the constraint violation. + if ($tooManyFiles) { + if(!$this->getConfig('allowedMaxFileNumber')) $this->setConfig('allowedMaxFileNumber', 1); + $return['error'] = _t( + 'UploadField.MAXNUMBEROFFILES', + 'Max number of {count} file(s) exceeded.', + array('count' => $this->getConfig('allowedMaxFileNumber')) + ); + } + } + + // Process the uploaded file + if (!$return['error']) { + $fileObject = null; + + if ($this->relationAutoSetting) { + // Search for relations that can hold the uploaded files. + if ($relationClass = $this->getRelationAutosetClass()) { + // Create new object explicitly. Otherwise rely on Upload::load to choose the class. + $fileObject = Object::create($relationClass); + } + } + + // Get the uploaded file into a new file object. + try { + $this->upload->loadIntoFile($tmpfile, $fileObject, $this->folderName); + } catch (Exception $e) { + // we shouldn't get an error here, but just in case + $return['error'] = $e->getMessage(); + } + + if (!$return['error']) { + if ($this->upload->isError()) { + $return['error'] = implode(' '.PHP_EOL, $this->upload->getErrors()); + } else { + $file = $this->upload->getFile(); + + // Attach the file to the related record. + $document = $this->attachFile($file); + + // Collect all output data. + $return = array_merge($return, array( + 'id' => $document->ID, + 'name' => $document->getTitle(), + 'thumbnail_url' => $document->UploadFieldThumbnailURL, + 'edit_url' => $this->getItemHandler($document->ID)->EditLink(), + 'size' => $document->getFileSizeFormatted(), + 'buttons' => $document->UploadFieldFileButtons, + 'showeditform' => true + )); + } + } + } + $response = new SS_HTTPResponse(Convert::raw2json(array($return))); + $response->addHeader('Content-Type', 'text/plain'); + return $response; } @@ -47,4 +148,19 @@ class DMSUploadField extends UploadField { return $fields; } + + /** + * @param int $itemID + * @return UploadField_ItemHandler + */ + public function getItemHandler($itemID) { + return DMSUploadField_ItemHandler::create($this, $itemID); + } +} + +class DMSUploadField_ItemHandler extends UploadField_ItemHandler { + function getItem() { + return DataObject::get_by_id('DMSDocument', $this->itemID); + } + } diff --git a/javascript/DMSUploadField_downloadtemplate.js b/javascript/DMSUploadField_downloadtemplate.js index c1bbbcd..5f22282 100644 --- a/javascript/DMSUploadField_downloadtemplate.js +++ b/javascript/DMSUploadField_downloadtemplate.js @@ -22,6 +22,9 @@ window.tmpl.cache['ss-uploadfield-downloadtemplate'] = tmpl( '
(refresh the page to display new file information)
' + '{% } %}' + '' + + '{% if (!file.error) { %}' + + '
' + + '{% } %}' + '' + '{% } %}' ); \ No newline at end of file