Skip to content

Commit

Permalink
[TASK] add option to select a fixedPostVars configuration on page rec…
Browse files Browse the repository at this point in the history
…ords (#151)

Used to generate fixedPostVars in a file which can be included from realurl_conf.
  • Loading branch information
pixelmatseriks authored and MattiasNilsson committed Apr 18, 2017
1 parent 2724783 commit 0478d23
Show file tree
Hide file tree
Showing 10 changed files with 737 additions and 6 deletions.
59 changes: 59 additions & 0 deletions Classes/Hooks/DataHandlerHook.php
@@ -0,0 +1,59 @@
<?php


namespace T3kit\themeT3kit\Hooks;

use T3kit\themeT3kit\Utility\FixedPostVarsConfigurationUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;

/**
* Class DataHandlerHook
* @package T3kit\themeT3kit\Hooks
*/
class DataHandlerHook
{
/**
* If page was created/edited and fixedPostVarConf changed we need to update configuration file
*
* @param string $status
* @param string $table
* @param int $id
* @param array $fieldArray
* @param DataHandler $pObj
* @return void
*/
public function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, DataHandler $pObj)
{
// if field tx_themet3kit_fixed_post_var_conf was modify we need to update configuration
if ($table === 'pages' && isset($fieldArray['tx_themet3kit_fixed_post_var_conf'])) {
/** @var FixedPostVarsConfigurationUtility $fixedPostVarsConfigurationUtility */
$fixedPostVarsConfigurationUtility = GeneralUtility::makeInstance(FixedPostVarsConfigurationUtility::class);
$fixedPostVarsConfigurationUtility->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();
}
}
}
210 changes: 210 additions & 0 deletions Classes/Utility/FixedPostVarsConfigurationUtility.php
@@ -0,0 +1,210 @@
<?php

namespace T3kit\themeT3kit\Utility;

use Doctrine\DBAL\Query\QueryBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class FixedPostVarsConfigurationUtility
* @package T3kit\themeT3kit\Utility
*/
class FixedPostVarsConfigurationUtility
{

/**
* Update configuration file of fixed post vars
*
* @return void
*/
public function updateConfiguration()
{
$filePath = $this->getSaveFilePath();

if (!$this->canWriteConfiguration($filePath)) {
throw new \RuntimeException(
$filePath . ' is not writable.',
1485349703
);
}

$configurations = $this->getConfiguration();
$varNameToPageFixedUids = [];

$pages = $this->getFixedPagesUids();

$content = '<?php' . LF;
$content .= '$init = function () {' . LF;

// generate fixed post var config vars
foreach ($pages as $page) {
$key = $page['tx_themet3kit_fixed_post_var_conf'];

if (array_key_exists($key, $configurations)) {
if (!array_key_exists($key, $varNameToPageFixedUids)) {
$varName = '$' . GeneralUtility::underscoredToLowerCamelCase($key);
$content .= ' ' . $varName . ' = ';
$content .= $this->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;
}
}
111 changes: 111 additions & 0 deletions Classes/Utility/HelperUtility.php
@@ -0,0 +1,111 @@
<?php


namespace T3kit\themeT3kit\Utility;

use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Lang\LanguageService;

/**
* Class MainUtility
* @package T3kit\T3kitExtensionTools\Utility
*/
class HelperUtility
{
/**
* Keep configuration of extension
*
* @var array
*/
protected static $extConf;

/**
* Get extension configuration
*
* @return array
*/
public static function getExtConf()
{
if (self::$extConf === null) {
if (isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['theme_t3kit'])) {
self::$extConf = unserialize((string)$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['theme_t3kit']);
} else {
self::$extConf = [];
}
}

return self::$extConf;
}

/**
* Get items for select field
*
* @param array &$params
* @param object $pObj
*/
public function getTcaFixedPostVarItems(&$params, $pObj)
{
$items = array_merge($params['items'], $this->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);
}
}

0 comments on commit 0478d23

Please sign in to comment.