Skip to content
Snippets Groups Projects
Commit 7325d7f3 authored by Sergiu-Lucian Petrica's avatar Sergiu-Lucian Petrica
Browse files

Implemented different approach for file persistence on validation and made a temporary save folder.

It will now check for files in the folders directly instead of passing file objects through the controller.
Also it will upload the files into a temporary location if the form validation fails and move them into permanent storage when form submit is successful.
parent b0738621
No related branches found
No related tags found
1 merge request!1Feature/multi upload
...@@ -57,11 +57,6 @@ class JoblistController extends ActionController { ...@@ -57,11 +57,6 @@ class JoblistController extends ActionController {
*/ */
private $jobRepository; private $jobRepository;
/**
* @var array
*/
private $uploadedFiles;
/** /**
* Show all job offers and options to manage them * Show all job offers and options to manage them
* *
...@@ -105,6 +100,7 @@ class JoblistController extends ActionController { ...@@ -105,6 +100,7 @@ class JoblistController extends ActionController {
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function applyAction(\SGalinski\SgJobs\Domain\Model\JobApplication $applyData) { public function applyAction(\SGalinski\SgJobs\Domain\Model\JobApplication $applyData) {
$this->submitApplicationFiles($GLOBALS['TSFE']->fe_user->id);
// get an instance of the mail service // get an instance of the mail service
/** @var MailTemplateService $mailService */ /** @var MailTemplateService $mailService */
$mailService = $this->objectManager->get( $mailService = $this->objectManager->get(
...@@ -121,16 +117,19 @@ class JoblistController extends ActionController { ...@@ -121,16 +117,19 @@ class JoblistController extends ActionController {
/** /**
* Pre-apply action setup, configures model-property mapping and handles file upload * Pre-apply action setup, configures model-property mapping and handles file upload
*
* @return void
*/ */
protected function initializeApplyAction() { protected function initializeApplyAction() {
$uploadedFiles['coverLetter'] = $this->handleFileUpload('coverLetter'); $this->handleFileUpload('coverLetter');
$uploadedFiles['cv'] = $this->handleFileUpload('cv'); $this->handleFileUpload('cv');
$uploadedFiles['certificates'] = $this->handleFileUpload('certificates'); $this->handleFileUpload('certificates');
$propertyMappingConfiguration = $this->arguments->getArgument('applyData')->getPropertyMappingConfiguration(); $propertyMappingConfiguration = $this->arguments->getArgument('applyData')->getPropertyMappingConfiguration();
$propertyMappingConfiguration->allowAllProperties(); // @todo: is this necessary? Check later. $propertyMappingConfiguration->allowAllProperties(); // @todo: is this necessary? Check later.
$propertyMappingConfiguration->forProperty('coverLetter')->allowAllProperties(); $propertyMappingConfiguration->forProperty('coverLetter')->allowAllProperties();
$propertyMappingConfiguration->forProperty('cv')->allowAllProperties(); $propertyMappingConfiguration->forProperty('cv')->allowAllProperties();
$propertyMappingConfiguration->forProperty('certificates')->allowAllProperties(); $propertyMappingConfiguration->forProperty('certificates')->allowAllProperties();
$uploadedFiles = $this->getExistingApplicationFiles($GLOBALS['TSFE']->fe_user->id);
$this->request->setArgument('uploadedFiles', $uploadedFiles); $this->request->setArgument('uploadedFiles', $uploadedFiles);
} }
...@@ -206,6 +205,8 @@ class JoblistController extends ActionController { ...@@ -206,6 +205,8 @@ class JoblistController extends ActionController {
} }
/** /**
* Returns all the files in a folder
*
* @param $folder * @param $folder
* @return \TYPO3\CMS\Core\Resource\File[] * @return \TYPO3\CMS\Core\Resource\File[]
*/ */
...@@ -220,30 +221,55 @@ class JoblistController extends ActionController { ...@@ -220,30 +221,55 @@ class JoblistController extends ActionController {
return $storage->getFilesInFolder($folder); return $storage->getFilesInFolder($folder);
} }
/**
* Returns all the application files based on a folder
*
* @param $folder
* @return array
*/
private function getExistingApplicationFiles($folder) {
$files['coverLetter'] = $this->getExistingFiles('temp/' . $folder . '/coverLetter');
$files['cv'] = $this->getExistingFiles('temp/' . $folder . '/cv');
$files['certificates'] = $this->getExistingFiles('temp/' . $folder . '/certificates');
return $files;
}
/**
* Moves the application files from temporary to permanent storage
*
* @param $folder
* @return void
*/
private function submitApplicationFiles($folder) {
$resourceFactory = GeneralUtility::makeInstance(
ResourceFactory::class
);
$storage = $resourceFactory->getStorageObject(1);
$sourceFolder = $storage->getFolder('/Applications/temp/' . $folder);
$targetFolder = $storage->getFolder('/Applications/');
$storage->moveFolder($sourceFolder, $targetFolder);
}
/** /**
* @param string $fieldName * @param string $fieldName
* @param null $hash
* @return array * @return array
*/ */
private function handleFileUpload($fieldName = '', $hash = null) { private function handleFileUpload($fieldName = '') {
$data = []; $data = [];
$namespace = key($_FILES); $namespace = key($_FILES);
if (!isset($hash)) { $applicationId = $GLOBALS['TSFE']->fe_user->id;
$applicationId = md5(uniqid('sg', TRUE));
} else {
$applicationId = $hash;
}
$resourceFactory = GeneralUtility::makeInstance( $resourceFactory = GeneralUtility::makeInstance(
ResourceFactory::class ResourceFactory::class
); );
$storage = $resourceFactory->getStorageObject(1); $storage = $resourceFactory->getStorageObject(1);
if (!$storage->hasFolder('/Applications/' . $applicationId . '/')) { if (!$storage->hasFolder('/Applications/temp/' . $applicationId . '/' . $fieldName)) {
$storage->createFolder('/Applications/' . $applicationId . '/'); $storage->createFolder('/Applications/temp/' . $applicationId . '/' . $fieldName);
} }
$targetFalDirectory = '1:/Applications/' . $applicationId . '/'; $targetFalDirectory = '1:/Applications/temp/' . $applicationId . '/' . $fieldName;
// Register every upload field from the form: // Register every upload field from the form:
$this->registerUploadField($data, $namespace, $fieldName, $targetFalDirectory); $this->registerUploadField($data, $namespace, $fieldName, $targetFalDirectory);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment