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

WIP make file upload move files correctly on form submit

parent 4deaf9fb
No related branches found
No related tags found
No related merge requests found
......@@ -35,6 +35,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
/**
* The joblist plugin controller
......@@ -103,7 +104,8 @@ class JoblistController extends ActionController {
*/
public function applyAction(JobApplication $applyData) {
try {
$this->submitApplicationFiles($applyData);
$folderName = $this->request->getArgument('folderName');
$this->submitApplicationFiles($applyData, $folderName);
/** @noinspection PhpMethodParametersCountMismatchInspection */
$mailService = $this->objectManager->get(
......@@ -143,22 +145,28 @@ class JoblistController extends ActionController {
* @throws \Exception
*/
protected function initializeApplyAction() {
if ($this->request->hasArgument('folderName')) {
$uniqueFolderName = $this->request->getArgument('folderName');
} else {
$uniqueFolderName = uniqid('sgjobs-', TRUE);
}
try {
$this->handleFileUpload('coverLetter');
$this->handleFileUpload('cv');
$this->handleFileUpload('certificates');
$this->handleFileUpload('coverLetter', $uniqueFolderName);
$this->handleFileUpload('cv', $uniqueFolderName);
$this->handleFileUpload('certificates', $uniqueFolderName);
} catch (\Exception $e) {
// possible errors, because of wrong mails
// @TODO output them in some way?
}
$propertyMappingConfiguration = $this->arguments->getArgument('applyData')->getPropertyMappingConfiguration();
$propertyMappingConfiguration->forProperty('coverLetter')->allowAllProperties();
$propertyMappingConfiguration->forProperty('cv')->allowAllProperties();
$propertyMappingConfiguration->forProperty('certificates')->allowAllProperties();
$uploadedFiles = $this->getExistingApplicationFiles($GLOBALS['TSFE']->fe_user->id);
$uploadedFiles = $this->getExistingApplicationFiles($uniqueFolderName);
$this->request->setArgument('uploadedFiles', $uploadedFiles);
$this->request->setArgument('folderName', $uniqueFolderName);
}
/**
......@@ -240,8 +248,8 @@ class JoblistController extends ActionController {
/**
* Moves the application files from temporary to permanent storage
*
* @param string $folder
* @param JobApplication $applicationData
* @param string $folderName
* @return void
* @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException
* @throws \TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException
......@@ -253,13 +261,13 @@ class JoblistController extends ActionController {
* @throws \Exception
* @throws \InvalidArgumentException
*/
private function submitApplicationFiles(JobApplication $applicationData) {
private function submitApplicationFiles(JobApplication $applicationData, $folderName) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$resourceFactory = $objectManager->get(ResourceFactory::class);
$storage = $resourceFactory->getStorageObject(1);
$newName = date('Ymd-His') . '_' . $applicationData->getFirstName();
$sourceFolder = $storage->getFolder('/Extension/temp/' . uniqid('sgjobs-', TRUE));
$sourceFolder = $storage->getFolder('/Extension/temp/' . $folderName);
$subFolders = $storage->getFoldersInFolder($sourceFolder);
$fileNames = [];
......@@ -365,6 +373,7 @@ class JoblistController extends ActionController {
/**
* @param string $fieldName
* @param string $folderName
* @return array
* @throws \UnexpectedValueException
* @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException
......@@ -374,19 +383,18 @@ class JoblistController extends ActionController {
* @throws \InvalidArgumentException
* @throws \TYPO3\CMS\Core\Resource\Exception
*/
private function handleFileUpload($fieldName = ''): array {
private function handleFileUpload($fieldName, $folderName): array {
$data = [];
$namespace = key($_FILES);
$applicationId = $GLOBALS['TSFE']->fe_user->id;
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$resourceFactory = $objectManager->get(ResourceFactory::class);
$storage = $resourceFactory->getStorageObject(1);
if (!$storage->hasFolder('/Extension/temp/' . $applicationId . '/' . $fieldName)) {
$storage->createFolder('/Extension/temp/' . $applicationId . '/' . $fieldName);
if (!$storage->hasFolder('/Extension/temp/' . $folderName . '/' . $fieldName)) {
$storage->createFolder('/Extension/temp/' . $folderName . '/' . $fieldName);
}
$targetFalDirectory = '1:/Extension/temp/' . $applicationId . '/' . $fieldName;
$targetFalDirectory = '1:/Extension/temp/' . $folderName . '/' . $fieldName;
// Register every upload field from the form:
$this->registerUploadField($data, $namespace, $fieldName, $targetFalDirectory);
......
......@@ -121,19 +121,19 @@ class JobApplication extends AbstractEntity {
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $coverLetter
* @validate NotEmpty
*
*/
protected $coverLetter;
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $cv
* @validate NotEmpty
*
*/
protected $cv;
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $certificates
* @validate NotEmpty
*
*/
protected $certificates;
......
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