diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php index 9439b740609b63e410a00172830e0cca41ab679e..4dd7dd239cfff1a559e5215221936bbf382c1d8b 100644 --- a/Classes/Controller/JoblistController.php +++ b/Classes/Controller/JoblistController.php @@ -28,10 +28,11 @@ namespace SGalinski\SgJobs\Controller; use SGalinski\SgJobs\Service\BackendService; use SGalinski\SgMail\Service\MailTemplateService; -use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException; use TYPO3\CMS\Core\Exception; +use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; +use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility; /** * The joblist plugin controller @@ -80,8 +81,17 @@ class JoblistController extends ActionController { /** * @param array $applyData * @return void + * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException + * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException + * @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException + * @throws \Exception + * @throws \InvalidArgumentException */ public function applyAction(array $applyData = []) { + $this->handleFileUpload('coverLetter'); + $this->handleFileUpload('cv'); + $this->handleFileUpload('certificates'); + // get an instance of the mail service /** @var MailTemplateService $mailService */ $mailService = $this->objectManager->get( @@ -100,7 +110,7 @@ class JoblistController extends ActionController { * @param array $applyData * @return array */ - private function getApplicationMailMarkers(array $applyData = []) { + private function getApplicationMailMarkers(array $applyData = []): array { return [ 'jobtitle' => $applyData['jobTitle'], 'salutation' => $applyData['salutation'], @@ -115,4 +125,71 @@ class JoblistController extends ActionController { 'message' => $applyData['message'] ]; } + + /** + * Registers an uploaded file for TYPO3 native upload handling. + * + * @param array &$data + * @param string $namespace + * @param string $fieldName + * @param string $targetDirectory + * @return void + */ + private function registerUploadField(array &$data, $namespace, $fieldName, $targetDirectory = '1:/_temp_/') { + if (!isset($data['upload'])) { + $data['upload'] = []; + } + $counter = count($data['upload']) + 1; + + $keys = array_keys($_FILES[$namespace]); + foreach ($keys as $key) { + $_FILES['upload_' . $counter][$key] = $_FILES[$namespace][$key][$fieldName]; + } + $data['upload'][$counter] = [ + 'data' => $counter, + 'target' => $targetDirectory, + ]; + } + + /** + * @param string $fieldName + */ + private function handleFileUpload($fieldName = '') { + $data = []; + $namespace = key($_FILES); + + $applicationId = md5(uniqid('sg', TRUE)); + + $resourceFactory = GeneralUtility::makeInstance( + ResourceFactory::class + ); + $storage = $resourceFactory->getStorageObject(1); + if (!$storage->hasFolder('/Applications/' . $applicationId . '/')) { + $storage->createFolder('/Applications/' . $applicationId . '/'); + } + + $targetFalDirectory = '1:/Applications/' . $applicationId . '/'; + + // Register every upload field from the form: + $this->registerUploadField($data, $namespace, $fieldName, $targetFalDirectory); + + // Initializing: + /** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $fileProcessor */ + $fileProcessor = GeneralUtility::makeInstance( + ExtendedFileUtility::class + ); + $fileProcessor->setActionPermissions(['addFile' => TRUE]); + + // Actual upload + $fileProcessor->start($data); + $result = $fileProcessor->processData(); + + debug($result); + + // Do whatever you want with $result (array of File objects) + foreach ($result['upload'] as $files) { + /** @var \TYPO3\CMS\Core\Resource\File $file */ + $file = $files[0]; // Single element array due to the way we registered upload fields + } + } } diff --git a/Resources/Private/Templates/Joblist/ApplyForm.html b/Resources/Private/Templates/Joblist/ApplyForm.html index 11a5e399a5c4f93d4ff3a68b71eecce6b683fdde..e1e76fc0a3099f8aba60ac4e5425bdeedd18fa53 100644 --- a/Resources/Private/Templates/Joblist/ApplyForm.html +++ b/Resources/Private/Templates/Joblist/ApplyForm.html @@ -42,11 +42,11 @@ <f:form.upload property="cv" id="apply-cv" /> <br /> <label for="apply-certificates"><f:translate key="frontend.apply.certificates" /></label> - <f:form.upload property="certificates" id="apply-certificates" /> + <input id="apply-certificates" type="file" name="tx_sgjobs_joblist[applyData][certificates]" multiple /> <br /> <label for="apply-message"><f:translate key="frontend.apply.message" /></label> <f:form.textarea property="message" id="apply-message" data="{}" class="" /> - <br /> + <br />- <f:form.submit value="{f:translate(key:'frontend.applyNow')}" /> </f:form> </f:section> diff --git a/ext_localconf.php b/ext_localconf.php index 1decd9436d27e852703489b02c227bee9d257db1..4c0632254cab43607aa3f9585031249993e14a09 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -10,7 +10,7 @@ ], [ // Uncacheable actions - 'Joblist' => 'apply' + 'Joblist' => 'applyForm, apply' ] );