Skip to content
Snippets Groups Projects
JoblistController.php 6.91 KiB
Newer Older
<?php

namespace SGalinski\SgJobs\Controller;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use SGalinski\SgJobs\Service\BackendService;
use SGalinski\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Resource\DuplicationBehavior;
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
 */
class JoblistController extends ActionController {
	/**
	 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
	 * @inject
	 */
	protected $objectManager;
	 * @var \SGalinski\SgJobs\Domain\Repository\CompanyRepository
	private $companyRepository;

	/**
	 * @var \SGalinski\SgJobs\Domain\Repository\JobRepository
	 * @inject
	 */
	private $jobRepository;

	/**
	 * Show all job offers and options to manage them
	 *
	 * @return void
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	 * @throws \InvalidArgumentException
	 */
	public function indexAction() {
		$pageUid = (int) GeneralUtility::_GP('id');
		$recordPageId = $this->configurationManager->getConfiguration(
			\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK
		)['persistence']['storagePid'];
		$this->assignFilterValues($recordPageId);

		// get all jobs for the next root page
		$jobs = $this->jobRepository->findJobs($recordPageId);
		$this->view->assign('jobs', $jobs);
	}
	/**
	 * @param string $jobTitle
	 */
	public function applyFormAction($jobTitle = '') {
		$this->view->assign('jobTitle', $jobTitle);
	/**
	 * @param array $applyData
	 * @return void
	 * @throws \BadFunctionCallException
	 * @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(
			MailTemplateService::class, 'application_mail', 'sg_jobs', $this->getApplicationMailMarkers($applyData)
		);
		$mailService->setIgnoreMailQueue(TRUE);
		$mailService->setToAddresses('torsten@sgalinski.de');
		try {
			$mailService->sendEmail();
		} catch (Exception $exception) {
	private function assignFilterValues($rootPageId) {
		// get all countries
		$countries = $this->companyRepository->getAllCountries($rootPageId);
		$this->view->assign('countries', $countries);

		// get all cities
		$cities = $this->companyRepository->getAllCities($rootPageId);
		$this->view->assign('cities', $cities);

		// get all cities
		$companies = $this->companyRepository->getAllCompanyNames($rootPageId);
		$this->view->assign('companies', $companies);

		// get all areas
		$areas = $this->jobRepository->getAllAreas($rootPageId);
		$this->view->assign('areas', $areas);

		// get all areas
		$functions = $this->jobRepository->getAllFunctions($rootPageId);
		$this->view->assign('functions', $functions);
	}

	/**
	 * @param array $applyData
	 * @return array
	 */
	private function getApplicationMailMarkers(array $applyData = []): array {
		return [
			'jobtitle' => $applyData['jobTitle'],
			'salutation' => $applyData['salutation'],
			'firstname' => $applyData['firstName'],
			'lastname' => $applyData['lastName'],
			'street' => $applyData['street'],
			'city' => $applyData['city'],
			'country' => $applyData['country'],
			'phone' => $applyData['phone'],
			'mobile' => $applyData['mobile'],
			'email' => $applyData['email'],
			'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]['applyData'][$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);
		$fileProcessor->setExistingFilesConflictMode(DuplicationBehavior::CANCEL); // This was previously passed as empty string, why and from where? It breaks the upload if it's not set.
		$result = $fileProcessor->processData();

		// 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
		}
	}