Skip to content
Snippets Groups Projects
ConfigurationController.php 9.22 KiB
<?php /** @noinspection CallableParameterUseCaseInTypeContextInspection */

namespace SGalinski\SgMail\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\SgMail\Service\BackendService;
use SGalinski\SgMail\Service\RegisterService;
use SGalinski\SgMail\Session\PhpSession;
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
 * Controller for the configuration mode of the backend module
 */
class ConfigurationController extends ActionController {
	public const DEFAULT_EXTENSION_KEY = 'sg_mail';

	/**
	 * DocHeaderComponent
	 *
	 * @var DocHeaderComponent
	 */
	protected $docHeaderComponent;

	/**
	 * @var \SGalinski\SgMail\Session\PhpSession
	 */
	protected $session;

	/**
	 *
	 * @param string $mode
	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @param array $filters
	 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
	 */
	public function indexAction($mode = 'new', $selectedTemplate = NULL, $selectedExtension = NULL, array $filters = []
	): void {
		if ($this->request->hasArgument('message')) {
			$this->addFlashMessage($this->request->getArgument('message'), '', FlashMessage::ERROR);
		}

		$pageUid = (int) GeneralUtility::_GP('id');
		$registerArray = BackendService::getNonBlacklistedTemplates($pageUid);
		if ($selectedTemplate === NULL || $selectedTemplate === '') {
			if (!empty($registerArray)) {
				$selectedExtension = \key($registerArray);
			}
			if (!empty($registerArray)) {
				$selectedTemplate = \key($registerArray[$selectedExtension]);
			}
		}

		$this->view->assign('extensionKey', self::DEFAULT_EXTENSION_KEY);
		$this->view->assign('mode', $mode);
		if ($mode === 'edit') {
			$templateToEdit = $registerArray[$selectedExtension][$selectedTemplate];

			$csv = '';
			foreach ($templateToEdit['marker'] as $arr) {
				$csv .= implode(';', $arr) . "\r\n";

			}

			$this->view->assignMultiple(
				[
					'templateName' => $selectedTemplate,
					'subject' => $templateToEdit['subject'],
					'templateContent' => $templateToEdit['templateContent'],
					'csv' => $csv,
					'extensionKey' => $selectedExtension,
					'description' => $templateToEdit['description'],
					'editMode' => 1
				]
			);
		}

		// make docheader
		$this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class);
		$pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(1));
		if ($pageInfo === FALSE) {
			$pageInfo = ['uid' => $pageUid];
		}
		$this->docHeaderComponent->setMetaInformation($pageInfo);
		BackendService::makeButtons($this->docHeaderComponent, $this->request);

		$this->view->assignMultiple(
			[
				'docHeader' => $this->docHeaderComponent->docHeaderContent(),
				'selectedTemplateFilter' => $filters['filterTemplate'],
				'selectedExtensionFilter' => $filters['filterExtension'],
				'typo3Version' => VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version),
				'selectedTemplateKey' => $selectedTemplate,
				'selectedExtensionKey' => $selectedExtension,
				'mode' => 'editor'
			]
		);
	}

	/**
	 * Create the template or display errors that occured
	 *
	 * @throws StopActionException
	 * @throws UnsupportedRequestTypeException
	 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
	 */
	public function createAction(): void {
		if (!$this->request->hasArgument('configuration')) {
			$this->redirect(
				'index', 'Configuration', NULL,
				['message' => LocalizationUtility::translate('backend.create_error', 'sg_mail')]
			);
		}

		/** @var array $configuration */
		$configuration = $this->request->getArgument('configuration');

		$templateName = $configuration['templateName'];
		$extensionKey = $configuration['extensionKey'];
		$csv = \str_replace("\r", '', $configuration['csv']);
		$subject = $configuration['subject'];
		$description = $configuration['description'];

		$markersCsv = \explode("\n", $csv);
		$markers = [];

		foreach ($markersCsv as $markerCsv) {
			$rowArray = str_getcsv($markerCsv, ';');
			if (!$rowArray[0]) {
				continue;
			}
			$markers[] = [
				'identifier' => $rowArray[0],
				'value' => $rowArray[1],
				'description' => $rowArray[2]
			];
		}

		$registerService = $this->objectManager->get(RegisterService::class);
		$registerService->writeRegisterFile(
			$templateName, $extensionKey, $markers, $subject, $description
		);

		$registerService->clearCaches();

		// store selected template & extension key in the session
		if (!($this->session instanceof PhpSession)) {
			$this->session = $this->objectManager->get(PhpSession::class);
			$this->session->setSessionKey('sg_mail_controller_session');
		} else {
			$this->session->setSessionKey('sg_mail_controller_session');
		}
		$this->session->setDataByKey('selectedTemplate', $templateName);
		$this->session->setDataByKey('selectedExtension', self::DEFAULT_EXTENSION_KEY);

		$this->redirect(
			'index', 'Mail', NULL,
			['message' => LocalizationUtility::translate('backend.create_message', 'sg_mail')]
		);
	}

	/**
	 * Edit the template or display errors that occured
	 *
	 * @throws StopActionException
	 * @throws UnsupportedRequestTypeException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
	 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
	 */
	public function editAction(): void {
		if (!$this->request->hasArgument('configuration')) {
			$this->redirect(
				'index', 'Configuration', NULL,
				['message' => LocalizationUtility::translate('backend.create_error', 'sg_mail')]
			);
		}
		/** @var array $configuration */
		$configuration = $this->request->getArgument('configuration');

		$templateName = $configuration['templateName'];
		$extensionKey = $configuration['extensionKey'];
		$oldTemplateName = $configuration['oldTemplateName'];
		$oldExtensionKey = $configuration['oldExtensionKey'];
		$csv = \str_replace("\r", '', $configuration['csv']);
		$subject = $configuration['subject'];
		$description = $configuration['description'];

		$markersCsv = \explode("\n", $csv);
		$markers = [];
		foreach ($markersCsv as $markerCsv) {
			$rowArray = \str_getcsv($markerCsv, ';');
			if (!$rowArray[0]) {
				continue;
			}
			$markers[] = [
				'identifier' => $rowArray[0],
				'value' => $rowArray[1],
				'description' => $rowArray[2]
			];
		}

		$registerService = $this->objectManager->get(RegisterService::class);
		$registerService->migrateTemplateEntries($oldTemplateName, $oldExtensionKey, $templateName, $extensionKey);
		$registerService->deleteRegisterFile($oldTemplateName);
		$registerService->writeRegisterFile(
			$templateName, $extensionKey, $markers, $subject, $description
		);

		$registerService->clearCaches();

		// store selected template & extension key in the session
		if (!($this->session instanceof PhpSession)) {
			$this->session = $this->objectManager->get(PhpSession::class);
			$this->session->setSessionKey('sg_mail_controller_session');
		} else {
			$this->session->setSessionKey('sg_mail_controller_session');
		}
		$this->session->setDataByKey('selectedTemplate', $templateName);
		$this->session->setDataByKey('selectedExtension', self::DEFAULT_EXTENSION_KEY);

		$this->redirect(
			'index', 'Mail', NULL,
			['message' => LocalizationUtility::translate('backend.edit_message', 'sg_mail')]
		);
	}

	/**
	 * Edit the template or display errors that occured
	 *
	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @throws StopActionException
	 * @throws UnsupportedRequestTypeException
	 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
	 */
	public function deleteAction(string $selectedTemplate, string $selectedExtension): void {
		$registerService = $this->objectManager->get(RegisterService::class);
		$registerService->deleteTemplate($selectedExtension, $selectedTemplate);
		$this->redirect(
			'index', 'Mail', NULL,
			['message' => LocalizationUtility::translate('backend.delete_message', 'sg_mail')]
		);
	}
}