From 0b1349b738569e324172bf1bd535e2ea69156517 Mon Sep 17 00:00:00 2001 From: Patrick Finkbeiner Date: Thu, 17 Jul 2014 20:03:03 +0200 Subject: [PATCH 1/2] [TASK] Adds example how to add attributes. With that commit comes the ability to add a specific title and description to an image directly while uploading them. There is even more possible like adding links or alternative text or any other already existing field in sys_file_reference table. Even extending the sys_file_reference reference is possible and fill it up with custom data. Just extend the FileReference model in your extension, assign your content via the model setter in UploadedFileReferenceConverter and thats it. Happy uploading. --- Classes/Domain/Model/FileReference.php | 53 ++++++++++++++++++- .../UploadedFileReferenceConverter.php | 27 ++++++---- .../Private/Partials/Example/FormFields.html | 23 ++++---- .../Private/Partials/Example/Properties.html | 10 +++- 4 files changed, 88 insertions(+), 25 deletions(-) diff --git a/Classes/Domain/Model/FileReference.php b/Classes/Domain/Model/FileReference.php index dfed650..4907340 100644 --- a/Classes/Domain/Model/FileReference.php +++ b/Classes/Domain/Model/FileReference.php @@ -37,6 +37,20 @@ class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference { */ protected $uidLocal; + /** + * title + * + * @var string + */ + protected $title; + + /** + * description + * + * @var string + */ + protected $description; + /** * @param \TYPO3\CMS\Core\Resource\FileReference $originalResource */ @@ -44,4 +58,41 @@ public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $orig $this->originalResource = $originalResource; $this->uidLocal = $originalResource->getOriginalFile()->getUid(); } -} + + /** + * Set title + * + * @param string $title + */ + public function setTitle($title) { + $this->title = $title; + } + + /** + * Get title + * + * @return string + */ + public function getTitle() { + return $this->title; + } + + /** + * Set description + * + * @param string $description + */ + public function setDescription($description) { + $this->description = $description; + } + + /** + * Get description + * + * @return string + */ + public function getDescription() { + return $this->description; + } + +} \ No newline at end of file diff --git a/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php b/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php index c7f512f..d9ec225 100644 --- a/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php +++ b/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php @@ -141,12 +141,12 @@ public function convertFrom($source, $targetType, array $convertedChildPropertie if ($source['error'] !== \UPLOAD_ERR_OK) { switch ($source['error']) { - case \UPLOAD_ERR_INI_SIZE: - case \UPLOAD_ERR_FORM_SIZE: - case \UPLOAD_ERR_PARTIAL: - return new \TYPO3\CMS\Extbase\Error\Error(\TYPO3\Flow\Utility\Files::getUploadErrorMessage($source['error']), 1264440823); - default: - return new \TYPO3\CMS\Extbase\Error\Error('An error occurred while uploading. Please try again or contact the administrator if the problem remains', 1340193849); + case \UPLOAD_ERR_INI_SIZE: + case \UPLOAD_ERR_FORM_SIZE: + case \UPLOAD_ERR_PARTIAL: + return new \TYPO3\CMS\Extbase\Error\Error(\TYPO3\Flow\Utility\Files::getUploadErrorMessage($source['error']), 1264440823); + default: + return new \TYPO3\CMS\Extbase\Error\Error('An error occurred while uploading. Please try again or contact the administrator if the problem remains', 1340193849); } } @@ -194,7 +194,7 @@ protected function importUploadedResource(array $uploadInfo, PropertyMappingConf $uploadFolder = $this->resourceFactory->retrieveFileOrFolderObject($uploadFolderId); $uploadedFile = $uploadFolder->addUploadedFile($uploadInfo, $conflictMode); - $fileReferenceModel = $this->createFileRefrenceFromFalFileObject($uploadedFile); + $fileReferenceModel = $this->createFileRefrenceFromFalFileObject($uploadedFile, $uploadInfo); if (isset($uploadInfo['submittedFile']['resourcePointer']) && $replaceResource) { $this->removeFileReference($this->hashService->validateAndStripHmac($uploadInfo['submittedFile']['resourcePointer'])); @@ -222,9 +222,10 @@ protected function getDatabaseConnection() { /** * @param File $file + * @param array $additionalAttributes * @return \Helhum\UploadExample\Domain\Model\FileReference */ - protected function createFileRefrenceFromFalFileObject(File $file) { + protected function createFileRefrenceFromFalFileObject(File $file, $additionalAttributes) { $fileReference = $this->resourceFactory->createFileReferenceObject( array( 'uid_local' => $file->getUid(), @@ -232,18 +233,24 @@ protected function createFileRefrenceFromFalFileObject(File $file) { 'uid' => uniqid('NEW_'), ) ); - return $this->createFileReferenceFromFalFileReferenceObject($fileReference); + return $this->createFileReferenceFromFalFileReferenceObject($fileReference, $additionalAttributes); } /** * @param FileReference $fileReference + * @param array $additionalAttributes * @return \Helhum\UploadExample\Domain\Model\FileReference */ - protected function createFileReferenceFromFalFileReferenceObject(FileReference $fileReference) { + protected function createFileReferenceFromFalFileReferenceObject(FileReference $fileReference, $additionalAttributes) { /** @var $fileReferenceModel \Helhum\UploadExample\Domain\Model\FileReference */ $fileReferenceModel = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Model\\FileReference'); $fileReferenceModel->setOriginalResource($fileReference); + // I would recommend to escape all incomming data to + // ensure that the data which will be stored in DB is secure! + $fileReferenceModel->setTitle(mysql_real_escape_string( htmlspecialchars( trim($additionalAttributes['title'])))); + $fileReferenceModel->setDescription( mysql_real_escape_string( htmlspecialchars( trim($additionalAttributes['description'])))); + return $fileReferenceModel; } } \ No newline at end of file diff --git a/Resources/Private/Partials/Example/FormFields.html b/Resources/Private/Partials/Example/FormFields.html index 9b6b067..1e95843 100644 --- a/Resources/Private/Partials/Example/FormFields.html +++ b/Resources/Private/Partials/Example/FormFields.html @@ -2,22 +2,21 @@
-
+
+
+Image:
+Additional image title:
+Additional image description: + +
- - - - -

- - - - - -
\ No newline at end of file +Image
+Additional image title:
+Additional image description: +
\ No newline at end of file diff --git a/Resources/Private/Partials/Example/Properties.html b/Resources/Private/Partials/Example/Properties.html index 1c7cd75..ae20fe7 100644 --- a/Resources/Private/Partials/Example/Properties.html +++ b/Resources/Private/Partials/Example/Properties.html @@ -13,7 +13,10 @@ - + +

+ {example.image.description} +

@@ -22,7 +25,10 @@ - + +

+ {image.description} +

From 80b00d3c7968628111d925b5d4ba9d407df1131f Mon Sep 17 00:00:00 2001 From: Patrick Finkbeiner Date: Thu, 17 Jul 2014 20:33:43 +0200 Subject: [PATCH 2/2] [TASK] Removes redundant validation. Also improves the storage handling of additionalAttributes. If the given value is empty, even after trimming, it uses NULL as value. The same as the origin value. --- .../TypeConverter/UploadedFileReferenceConverter.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php b/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php index d9ec225..1859fae 100644 --- a/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php +++ b/Classes/Property/TypeConverter/UploadedFileReferenceConverter.php @@ -246,11 +246,12 @@ protected function createFileReferenceFromFalFileReferenceObject(FileReference $ $fileReferenceModel = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Model\\FileReference'); $fileReferenceModel->setOriginalResource($fileReference); - // I would recommend to escape all incomming data to - // ensure that the data which will be stored in DB is secure! - $fileReferenceModel->setTitle(mysql_real_escape_string( htmlspecialchars( trim($additionalAttributes['title'])))); - $fileReferenceModel->setDescription( mysql_real_escape_string( htmlspecialchars( trim($additionalAttributes['description'])))); + $title = trim($additionalAttributes['title']); + $fileReferenceModel->setDescription($title === '' ? NULL : $title); + $description = trim($additionalAttributes['description']); + $fileReferenceModel->setDescription($description === '' ? NULL : $description); + return $fileReferenceModel; } } \ No newline at end of file