configurationService = $configurationService; } /** * * @var BackupService */ protected $backupService; /** * Inject the BackupService * * @param BackupService $backupService */ public function injectBackupService(BackupService $backupService) { $this->backupService = $backupService; } /** * Initializes the actions. * - Initializes the session object. * - Fetches configuration. * * @return void * @throws \SGalinski\Lfeditor\Exceptions\DirectoryAccessRightsException */ public function initializeAction() { parent::initializeAction(); if (!($this->session instanceof PhpSession)) { $this->session = GeneralUtility::makeInstance(PhpSession::class); $this->session->setSessionKey('tx_lfeditor_sessionVariables'); } $this->configurationService->prepareConfig(); } /** * Saves in session currently selected values of select tags. * * @param string $extensionSelection * @param string $languageFileSelection * @param string $referenceLanguageSelection * @param string $constantSelection * @param string $languageSelection * @param string $constantTypeSelection * @param string $bottomReferenceLanguageSelection * @param string $numSiteConstsSelection * @param string $extkey * @return void */ protected function saveSelectionsInSession( $extensionSelection = NULL, $languageFileSelection = NULL, $referenceLanguageSelection = NULL, $constantSelection = NULL, $languageSelection = NULL, $constantTypeSelection = NULL, $bottomReferenceLanguageSelection = NULL, $numSiteConstsSelection = NULL, $extkey = NULL ) { /* Extension/language file select box can't be unselected. Only situation when $extensionSelection === NULL is when the form is submitted by selection change of some other box. That is because with 'optgroup' tags is used */ if ($extensionSelection) { $this->session->setDataByKey('extensionSelection', $extensionSelection); } if ($languageFileSelection) { $this->session->setDataByKey('languageFileSelection', $languageFileSelection); } if ($referenceLanguageSelection) { $this->session->setDataByKey('referenceLanguageSelection', $referenceLanguageSelection); } if ($constantSelection) { $this->session->setDataByKey('constantSelection', $constantSelection); } if ($languageSelection) { $this->session->setDataByKey('languageSelection', $languageSelection); } if ($constantTypeSelection) { $this->session->setDataByKey('constantTypeSelection', $constantTypeSelection); } if ($bottomReferenceLanguageSelection) { $this->session->setDataByKey('bottomReferenceLanguageSelection', $bottomReferenceLanguageSelection); } if ($numSiteConstsSelection) { $this->session->setDataByKey('numSiteConstsSelection', $numSiteConstsSelection); } if ($extkey) { $this->session->setDataByKey('extkey', $extkey); } } /** * Assigns view width menu options and default menu selection which is fetched from session. * * @param string $menuName Name of the menu, which will be used as prefix of view keys for menu options and menu selection. * Example: menuName 'extension' will produce view keys 'extensionOptions' and 'extensionSelection' * @param array $options menu options to be assigned to view * @return void */ protected function assignViewWidthMenuVariables($menuName, $options) { $this->view->assign($menuName . 'Options', $options); $selection = $this->checkMenuSelection($menuName, $options); $this->view->assign($menuName . 'Selection', $selection); } /** * Checks does selection exists in session or among menu options and if it does not, * first option becomes selected. * * @param string $menuName Name of the menu, which will be used as prefix of view keys for menu options and menu selection. * Example: menuName 'extension' will produce view keys 'extensionOptions' and 'extensionSelection' * @param array $options menu options to be checked upon. * @return string */ protected function checkMenuSelection($menuName, array $options) { $selection = $this->session->getDataByKey($menuName . 'Selection'); if (!\array_key_exists($selection, $options)) { $selection = NULL; } if ($selection === NULL && !empty($options)) { $selection = array_key_first($options); $this->session->setDataByKey($menuName . 'Selection', $selection); return $selection; } return $selection; } /** * Sets FlashMessage from LFException. * * @param LFException $lFException * @return void */ public function addLFEFlashMessage(LFException $lFException) { if ($lFException->getCode() === 0) { $this->addFlashMessage( $lFException->getMessage(), LocalizationUtility::translate('failure.failure', 'lfeditor'), AbstractMessage::ERROR ); } elseif ($lFException->getCode() === 1) { $this->addFlashMessage( $lFException->getMessage(), '', AbstractMessage::NOTICE ); } } /** * Prepares language file select options for each extension and sets combined data in view. * * @return void * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException * @throws LFException */ protected function prepareExtensionAndLangFileOptions() { /** @var CacheManager $cacheManager */ $cacheManager = GeneralUtility::makeInstance(CacheManager::class); $extensions = $cacheManager->getCache('lfeditor_select_options_cache')->get('extensionAndLangFileOptions'); if (!is_array($extensions)) { $extensions = []; $extensionOptions = $this->configurationService->menuExtList(); $extensionGroupCount = 0; foreach ($extensionOptions as $extAddress => $extLabel) { $extension['extKey'] = $extLabel; ((int) ExtensionManagementUtility::isLoaded(basename($extLabel))) ? $state = LocalizationUtility::translate('ext.loaded', 'lfeditor') : $state = LocalizationUtility::translate('ext.notLoaded', 'lfeditor'); $extension['extLabel'] = $extLabel . ' [' . $state . ']'; $extension['languageFileOptions'] = []; $isExtensionGroupStart = $extAddress === '###extensionGroup###' . $extLabel; try { if (!$isExtensionGroupStart) { $extension['languageFileOptions'] = $this->configurationService->menuLangFileList($extAddress); if (empty($extension['languageFileOptions'])) { continue; } } elseif (++$extensionGroupCount > 1) { $extensions[$extAddress . 'EmptySpaceBefore'] = [ 'extLabel' => '', 'languageFileOptions' => [], ]; } } catch (LFException $e) { continue; } $extensions[$extAddress] = $extension; if ($isExtensionGroupStart) { $extensions[$extAddress . 'DelimiterAfter'] = [ 'extLabel' => '======', 'languageFileOptions' => [], ]; } } $cacheManager->getCache('lfeditor_select_options_cache')->set('extensionAndLangFileOptions', $extensions); } $this->checkExtensionAndLangFileSelection($extensions); $extensionSelection = $this->session->getDataByKey('extensionSelection'); $this->view->assign('extensions', $extensions); $this->view->assign('extensionSelection', $extensionSelection); $this->view->assign('extensionLabel', $extensions[$extensionSelection]['extLabel'] ?? ''); $this->view->assign('languageFileSelection', $this->session->getDataByKey('languageFileSelection')); } /** * Checks do extensionSelection and languageFileSelection exist in session or among menu options and if it does not, * first language file and belonging extension become selected and saved to session. * * @param array $extensions * @return void */ protected function checkExtensionAndLangFileSelection(array $extensions) { $extensionSelection = $this->session->getDataByKey('extensionSelection'); $languageFileSelection = $this->session->getDataByKey('languageFileSelection'); $selectFirstLanguageFileAndBelongingExtension = !$extensionSelection || !$languageFileSelection || !(array_key_exists($extensionSelection, $extensions) && array_key_exists( $languageFileSelection, $extensions[$extensionSelection]['languageFileOptions'] )); if ($selectFirstLanguageFileAndBelongingExtension) { foreach ($extensions as $extAddress => $extension) { if (empty($extension['languageFileOptions'])) { continue; } $languageFileSelection = array_key_first($extension['languageFileOptions']); $this->session->setDataByKey('languageFileSelection', $languageFileSelection); $this->session->setDataByKey('extensionSelection', $extAddress); break; } } } /** * Clears cache used for storing select options. * If $identifier is set, it clears only that entry in cache, * otherwise it clears whole select options cache. * * @param string $identifier * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException */ protected function clearSelectOptionsCache($identifier = NULL) { /** @var CacheManager $cacheManager */ $cacheManager = GeneralUtility::makeInstance(CacheManager::class); if ($identifier !== NULL) { $cacheManager->getCache('lfeditor_select_options_cache')->set($identifier, NULL); } else { $cacheManager->getCache('lfeditor_select_options_cache')->flush(); } } }