From 0478d2368ebb6eeb4477dc23098c2e794f819170 Mon Sep 17 00:00:00 2001 From: Mats Svensson Date: Tue, 18 Apr 2017 13:51:40 +0200 Subject: [PATCH] [TASK] add option to select a fixedPostVars configuration on page records (#151) Used to generate fixedPostVars in a file which can be included from realurl_conf. --- Classes/Hooks/DataHandlerHook.php | 59 +++++ .../FixedPostVarsConfigurationUtility.php | 210 ++++++++++++++++++ Classes/Utility/HelperUtility.php | 111 +++++++++ .../Realurl/predefined_fixedPostVars_conf.php | 114 ++++++++++ Configuration/TCA/Overrides/pages.php | 26 ++- Resources/Private/Language/locallang_db.xlf | 33 +++ class.ext_update.php | 129 +++++++++++ ext_conf_template.txt | 5 + ext_localconf.php | 49 +++- ext_tables.sql | 7 + 10 files changed, 737 insertions(+), 6 deletions(-) create mode 100644 Classes/Hooks/DataHandlerHook.php create mode 100644 Classes/Utility/FixedPostVarsConfigurationUtility.php create mode 100644 Classes/Utility/HelperUtility.php create mode 100644 Configuration/Realurl/predefined_fixedPostVars_conf.php create mode 100644 Resources/Private/Language/locallang_db.xlf create mode 100644 class.ext_update.php create mode 100644 ext_conf_template.txt diff --git a/Classes/Hooks/DataHandlerHook.php b/Classes/Hooks/DataHandlerHook.php new file mode 100644 index 00000000..7305bd22 --- /dev/null +++ b/Classes/Hooks/DataHandlerHook.php @@ -0,0 +1,59 @@ +updateConfiguration(); + } + } + + /** + * Update fixed post vars conf if page was removed + * + * @param string $table + * @param string|int $id + * @param array $recordToDelete + * @param boolean &$recordWasDeleted + * @param DataHandler $pObj + * @return void + */ + public function processCmdmap_deleteAction($table, $id, $recordToDelete, &$recordWasDeleted, DataHandler $pObj) + { + if ($table === 'pages' && $recordToDelete['tx_themet3kit_fixed_post_var_conf']) { + $pObj->deleteEl($table, $id); + $recordWasDeleted = true; + + /** @var FixedPostVarsConfigurationUtility $fixedPostVarsConfigurationUtility */ + $fixedPostVarsConfigurationUtility = GeneralUtility::makeInstance(FixedPostVarsConfigurationUtility::class); + $fixedPostVarsConfigurationUtility->updateConfiguration(); + } + } +} diff --git a/Classes/Utility/FixedPostVarsConfigurationUtility.php b/Classes/Utility/FixedPostVarsConfigurationUtility.php new file mode 100644 index 00000000..93c2fe90 --- /dev/null +++ b/Classes/Utility/FixedPostVarsConfigurationUtility.php @@ -0,0 +1,210 @@ +getSaveFilePath(); + + if (!$this->canWriteConfiguration($filePath)) { + throw new \RuntimeException( + $filePath . ' is not writable.', + 1485349703 + ); + } + + $configurations = $this->getConfiguration(); + $varNameToPageFixedUids = []; + + $pages = $this->getFixedPagesUids(); + + $content = 'fixIndent( + ArrayUtility::arrayExport( + $configurations[$key]['configuration'] + ) + ); + $content .= ';' . LF; + + // save var name + $varNameToPageFixedUids[$key] = [ + 'varName' => $varName, + 'fixedUids' => [$page['uid']] + ]; + } else { + $varNameToPageFixedUids[$key]['fixedUids'][] = $page['uid']; + } + } + } + + $fixedPostVarLine = ' '; + $fixedPostVarLine .= '$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'realurl\'][\'_DEFAULT\'][\'fixedPostVars\']'; + + foreach ($varNameToPageFixedUids as $key => $varNameToPageFixedUid) { + // first write configuration + $content .= $fixedPostVarLine; + $content .= sprintf( + '[\'%s\'] = %s;', + $key, + $varNameToPageFixedUid['varName'] + ); + $content .= LF; + + // now add it for each page + foreach ($varNameToPageFixedUid['fixedUids'] as $fixedUid) { + $content .= $fixedPostVarLine; + $content .= sprintf( + '[\'%s\'] = \'%s\';', + $fixedUid, + $key + ); + $content .= LF; + } + + $content .= LF; + } + + $content .= '};' . LF; + $content .= '$init();' . LF; + $content .= 'unset($init);' . LF; + + GeneralUtility::writeFile($filePath, $content, true); + } + + /** + * Fix indent for configuration array + * + * @param $string + * @return string + */ + protected function fixIndent($string) + { + $lines = explode(PHP_EOL, $string); + + foreach ($lines as &$line) { + $line = ' ' . $line; + } + + return ltrim(implode(LF, $lines)); + } + + /** + * Generate configuration array with associative keys + * + * @return array + */ + protected function getConfiguration() + { + $configuration = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['theme_t3kit']['fixedPostVars']; + $processedConfiguration = []; + + if (is_array($configuration)) { + foreach ($configuration as $item) { + $processedConfiguration[$item['key']] = $item; + } + } + + return $processedConfiguration; + } + + /** + * Get list of pages with fixed post var configuration + * + * @return array + */ + protected function getFixedPagesUids() + { + $field = 'tx_themet3kit_fixed_post_var_conf'; + + if (version_compare(TYPO3_version, '8.0', '<')) { + /** @var DatabaseConnection $dbConnection */ + $dbConnection = $GLOBALS['TYPO3_DB']; + + $pages = $dbConnection->exec_SELECTgetRows( + 'uid, ' . $field, + 'pages', + $field . ' != \'0\' AND ' . $field . ' != \'\'' + . BackendUtility::deleteClause('pages') + ); + } else { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); + $pages = $queryBuilder + ->select('uid', 'tx_themet3kit_fixed_post_var_conf') + ->from('pages') + ->where( + $queryBuilder->expr()->neq( + $field, + $queryBuilder->createNamedParameter('') + ), + $queryBuilder->expr()->neq( + $field, + $queryBuilder->createNamedParameter('0') + ) + ) + ->execute() + ->fetchAll(); + } + + return $pages; + } + + /** + * Checks if the configuration can be written. + * + * @param string $fileLocation + * @return bool + */ + protected function canWriteConfiguration($fileLocation) + { + $typo3confFolder = PATH_typo3conf; + return @is_writable($typo3confFolder) && (!file_exists($fileLocation) || @is_writable($fileLocation)); + } + + /** + * Get path where to save configuration + * + * @return string + */ + public function getSaveFilePath() + { + $extConf = HelperUtility::getExtConf(); + + if (is_array($extConf) && $extConf['fixedPostVarsSaveFilePath']) { + $filePath = PATH_site . trim($extConf['fixedPostVarsSaveFilePath']); + } else { + $filePath = PATH_site . 'typo3conf/realurl_fixedPostVars_conf.php'; + } + + return $filePath; + } +} diff --git a/Classes/Utility/HelperUtility.php b/Classes/Utility/HelperUtility.php new file mode 100644 index 00000000..90b9f11a --- /dev/null +++ b/Classes/Utility/HelperUtility.php @@ -0,0 +1,111 @@ +getAvailableFixedPostVarConfigurations()); + + $params['items'] = $items; + } + + /** + * Get predefined configurations + * + * @return array + */ + protected function getAvailableFixedPostVarConfigurations() + { + $configurations = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['theme_t3kit']['fixedPostVars']; + $availableConfigurations = []; + + if (is_array($configurations)) { + foreach ($configurations as $configuration) { + $key = (string)$configuration['key']; + + if (empty($key)) { + $this->addErrorMessage('realurl_conf.error.empty_key'); + } elseif ($key !== '--div--' && array_key_exists($key, $availableConfigurations)) { + $this->addErrorMessage('realurl_conf.error.double_key', [$key]); + } else { + $availableConfigurations[] = [$configuration['title'], $key]; + } + } + } + + return $availableConfigurations; + } + + protected function addErrorMessage($error, $arguments = []) + { + /** @var LanguageService $lang */ + $languageService = $GLOBALS['LANG']; + + $ll = 'LLL:EXT:theme_t3kit/Resources/Private/Language/locallang_db.xlf:'; + + $message = $languageService->sL($ll . $error); + if (!empty($arguments)) { + $message = sprintf($message, ...$arguments); + } + + /** @var FlashMessage $flashMessage */ + $flashMessage = GeneralUtility::makeInstance( + FlashMessage::class, + $message, + $languageService->sL($ll . 'realurl_conf.error.title'), + FlashMessage::ERROR, + true + ); + + /** @var FlashMessageQueue $flashMessageQueue */ + $flashMessageQueue = GeneralUtility::makeInstance( + FlashMessageQueue::class, + 'core.template.flashMessages' + ); + + $flashMessageQueue->addMessage($flashMessage); + } +} diff --git a/Configuration/Realurl/predefined_fixedPostVars_conf.php b/Configuration/Realurl/predefined_fixedPostVars_conf.php new file mode 100644 index 00000000..55e49191 --- /dev/null +++ b/Configuration/Realurl/predefined_fixedPostVars_conf.php @@ -0,0 +1,114 @@ + 'News single view', + 'key' => 'news_single_view', + 'configuration' => [ + [ + 'GETvar' => 'tx_news_pi1[action]', + 'noMatch' => 'bypass' + ], + [ + 'GETvar' => 'tx_news_pi1[controller]', + 'noMatch' => 'bypass' + ], + [ + 'GETvar' => 'tx_news_pi1[news]', + 'lookUpTable' => [ + 'table' => 'tx_news_domain_model_news', + 'id_field' => 'uid', + 'alias_field' => 'IF(path_segment!="",path_segment,title)', + 'addWhereClause' => ' AND NOT deleted', + 'useUniqueCache' => 1, + 'useUniqueCache_conf' => [ + 'strtolower' => 1, + 'spaceCharacter' => '-' + ], + 'languageGetVar' => 'L', + 'languageExceptionUids' => '', + 'languageField' => 'sys_language_uid', + 'transOrigPointerField' => 'l10n_parent', + 'autoUpdate' => 1, + 'expireDays' => 180 + ] + ] + ] + ]; + + // predefine configuration for news category menu + $configuration['fixedPostVars'][] = [ + 'title' => 'Article category', + 'key' => 'categories_item', + 'configuration' => [ + [ + 'GETvar' => 'tx_news_pi1[overwriteDemand][categories]', + 'lookUpTable' => [ + 'table' => 'sys_category', + 'id_field' => 'uid', + 'alias_field' => 'title', + 'addWhereClause' => ' AND NOT deleted', + 'useUniqueCache' => 1, + 'useUniqueCache_conf' => [ + 'strtolower' => 1, + 'spaceCharacter' => '-' + ] + ] + ] + ] + ]; + + // predefine configuration for news tags menu + $configuration['fixedPostVars'][] = [ + 'title' => 'Article tag', + 'key' => 'tags_item', + 'configuration' => [ + [ + 'GETvar' => 'tx_news_pi1[overwriteDemand][tags]', + 'lookUpTable' => [ + 'table' => 'tx_news_domain_model_tag', + 'id_field' => 'uid', + 'alias_field' => 'title', + 'addWhereClause' => ' AND NOT deleted', + 'useUniqueCache' => 1, + 'useUniqueCache_conf' => [ + 'strtolower' => 1, + 'spaceCharacter' => '-' + ] + ] + ] + ] + ]; + + // predefine configuration for news date menu + $configuration['fixedPostVars'][] = [ + 'title' => 'Article date', + 'key' => 'date_item', + 'configuration' => [ + [ + 'GETvar' => 'tx_news_pi1[controller]', + 'noMatch' => 'bypass', + ], + [ + 'GETvar' => 'tx_news_pi1[overwriteDemand][year]' + ], + [ + 'GETvar' => 'tx_news_pi1[overwriteDemand][month]', + ], + [ + 'GETvar' => 'tx_news_pi1[overwriteDemand][day]', + 'noMatch' => 'bypass', + ] + ] + ]; +}; + +$init(); +unset($init); diff --git a/Configuration/TCA/Overrides/pages.php b/Configuration/TCA/Overrides/pages.php index ab47c3ac..329092a8 100644 --- a/Configuration/TCA/Overrides/pages.php +++ b/Configuration/TCA/Overrides/pages.php @@ -1,7 +1,7 @@ 'user', 'userFunc' => 'T3kit\themeT3kit\UserFunction\IconFontSelector->renderField', @@ -11,4 +11,28 @@ '0' => array('None', '', ''), ), ); + + $ll = 'LLL:EXT:theme_t3kit/Resources/Private/Language/locallang_db.xlf:'; + + $tempColumns = [ + 'tx_themet3kit_fixed_post_var_conf' => [ + 'label' => $ll .'pages.tx_themet3kit_fixed_post_var_conf', + 'displayCond' => 'FIELD:tx_realurl_exclude:!=:1', + 'exclude' => 1, + 'config' => [ + 'type' => 'select', + 'max' => 1, + 'renderType' => 'selectSingle', + 'items' => [ + [$ll . 'none', '0'], + [$ll . 'new_conf', '--div--'] + ], + 'itemsProcFunc' => \T3kit\themeT3kit\Utility\HelperUtility::class . '->getTcaFixedPostVarItems' + ] + ] + ]; + + \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('pages', $tempColumns); + + $GLOBALS['TCA']['pages']['palettes']['tx_realurl']['showitem'] .= ',--linebreak--,tx_themet3kit_fixed_post_var_conf'; }); diff --git a/Resources/Private/Language/locallang_db.xlf b/Resources/Private/Language/locallang_db.xlf new file mode 100644 index 00000000..468ebb74 --- /dev/null +++ b/Resources/Private/Language/locallang_db.xlf @@ -0,0 +1,33 @@ + + + +
+ + + Path to file where to save fixedPostVars configuration + + + Path to file with predefined configuration + + + Fixed post var configuration + + + None + + + News configuration + + + + Error + + + Value of "key" could not be empty. Please, check realurl "fixedPostVars" configuration. + + + Key "%s" already in use. Please, check realurl "fixedPostVars" configuration. + + + + \ No newline at end of file diff --git a/class.ext_update.php b/class.ext_update.php new file mode 100644 index 00000000..7e925d8a --- /dev/null +++ b/class.ext_update.php @@ -0,0 +1,129 @@ +Migration scripts'; + $content .= '
'; + + $pages = $this->getT3kitExtensionToolFixedPostVarPages(); + if (is_array($pages) && count($pages) > 0) { + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + foreach ($pages as $index => $page) { + $selected = ''; + if ($page['tx_t3kitextensiontools_fixed_post_var_conf'] != $page['tx_themet3kit_fixed_post_var_conf']) { + $selected = 'checked="checked"'; + } + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + } + $content .= '
UpdatePage idPage titlet3kit_extension_tooltheme_t3kit
' . $page['uid'] . '' . $page['title'] . '' . $page['tx_t3kitextensiontools_fixed_post_var_conf'] . '' . $page['tx_themet3kit_fixed_post_var_conf'] . '
'; + $content .= ''; + } + $content .= '
'; + + return $content; + } + + /** + * Checks if the script should execute. We check for everything except table + * structure. + * + * @return bool + */ + public function access() + { + return true; + } + + protected function getT3kitExtensionToolFixedPostVarPages() + { + $hasT3kitExtensionToolsFixedPostVarField = $this->hasT3kitExtensionToolsFixedPostVarField(); + $pages = []; + + if ($hasT3kitExtensionToolsFixedPostVarField) { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); + $pages = $queryBuilder + ->select('uid', 'title', 'tx_t3kitextensiontools_fixed_post_var_conf', 'tx_themet3kit_fixed_post_var_conf') + ->from('pages') + ->where( + $queryBuilder->expr()->neq( + 'tx_t3kitextensiontools_fixed_post_var_conf', + $queryBuilder->createNamedParameter('') + ), + $queryBuilder->expr()->neq( + 'tx_t3kitextensiontools_fixed_post_var_conf', + $queryBuilder->createNamedParameter('0') + ) + ) + ->execute() + ->fetchAll(); + } + + return $pages; + } + + protected function hasT3kitExtensionToolsFixedPostVarField() + { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); + $pages = $queryBuilder + ->select('*') + ->from('pages') + ->setMaxResults(1) + ->execute() + ->fetchAll(); + + return isset($pages[0]['tx_t3kitextensiontools_fixed_post_var_conf']); + } +} diff --git a/ext_conf_template.txt b/ext_conf_template.txt new file mode 100644 index 00000000..71682189 --- /dev/null +++ b/ext_conf_template.txt @@ -0,0 +1,5 @@ +# cat=basic/enable/110; type=string; label=LLL:EXT:theme_t3kit/Resources/Private/Language/locallang_db.xlf:extmng.fixedPostVarsConfigurationfFile +fixedPostVarsConfigurationfFile = typo3conf/ext/theme_t3kit/Configuration/Realurl/predefined_fixedPostVars_conf.php + +# cat=basic/enable/120; type=string; label=LLL:EXT:theme_t3kit/Resources/Private/Language/locallang_db.xlf:extmng.fixedPostVarsSaveFilePath +fixedPostVarsSaveFilePath = typo3conf/realurl_t3kit_fixedPostVars_conf.php \ No newline at end of file diff --git a/ext_localconf.php b/ext_localconf.php index f63ec031..2a3e126c 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -1,13 +1,52 @@ getSaveFilePath(); +if (file_exists($fixedPostVarsFile)) { + \TYPO3\CMS\Core\Utility\GeneralUtility::requireOnce($fixedPostVarsFile); +} +*/ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['imageTextLink'] = \T3kit\themeT3kit\Hooks\ImageTextLinkPreviewRenderer::class; -$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['t3kit'][] = 'T3kit\\themeT3kit\\ViewHelpers'; \ No newline at end of file +$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['t3kit'][] = 'T3kit\\themeT3kit\\ViewHelpers'; diff --git a/ext_tables.sql b/ext_tables.sql index 9eea9ce7..0664fb55 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -15,3 +15,10 @@ CREATE TABLE sys_file_reference ( tx_themet3kit_slide_btn_txt varchar(255) DEFAULT '' NOT NULL, tx_themet3kit_slide_appearance int(11) unsigned DEFAULT '0' NOT NULL ); + +# +# Modifying pages table +# +CREATE TABLE pages ( + tx_themet3kit_fixed_post_var_conf varchar(100) DEFAULT '0' NOT NULL +); \ No newline at end of file