From 4ab1c68b3a4a5db5f1366bbeb63edb297d6ff145 Mon Sep 17 00:00:00 2001 From: Hannes Lau Date: Wed, 22 Mar 2023 23:43:55 +0100 Subject: [PATCH] [BUGFIX] Implement update wizard for flexform changes The tx_news plugin's flexform field 'settings.eventRestriction' was moved to its own flexform sheet. Provide an update wizard to adapt the flexform data in the tt_content rows accordingly. Fixes: #101 --- Classes/Update/UpdatePluginFlexforms.php | 129 +++++++++++++++++++++++ Configuration/Services.yaml | 3 + ext_localconf.php | 3 + 3 files changed, 135 insertions(+) create mode 100644 Classes/Update/UpdatePluginFlexforms.php diff --git a/Classes/Update/UpdatePluginFlexforms.php b/Classes/Update/UpdatePluginFlexforms.php new file mode 100644 index 0000000..9c7b941 --- /dev/null +++ b/Classes/Update/UpdatePluginFlexforms.php @@ -0,0 +1,129 @@ +logger = $logger; + $this->connectionPool = $connectionPool; + $this->flexFormTools = $flexFormTools; + } + + /** + * @inheritDoc + */ + public function getIdentifier(): string + { + return 'tx_eventnews_updateFlexforms'; + } + + /** + * @inheritDoc + */ + public function getTitle(): string + { + return 'Eventnews flexform data update'; + } + + /** + * @inheritDoc + */ + public function getDescription(): string + { + return 'The tx_news plugin flexform field \'settings.eventRestriction\' was moved to its own flexform sheet. Update the flexform data in the tt_content rows accordingly.'; + } + + /** + * @inheritDoc + */ + public function updateNecessary(): bool + { + $flexForms = $this->getFlexFormsToUpdate(); + return $flexForms->valid(); + } + + /** + * @inheritDoc + */ + public function executeUpdate(): bool + { + $updateStatement = $this->getUpdateStatement(); + + foreach($this->getFlexFormsToUpdate() as $uid => $flexForm) { + $newFlexForm = $this->updateFlexForm($flexForm); + $newFlexFormString = $this->flexFormTools->flexArray2Xml($newFlexForm, addPrologue: true); + $updateStatement->executeStatement([$newFlexFormString, $uid]); + } + + return true; + } + + /** + * @inheritDoc + */ + public function getPrerequisites(): array + { + return []; + } + + protected function getFlexFormsToUpdate(): \Generator + { + $queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content'); + $queryResult = $queryBuilder + ->select('uid', 'pi_flexform') + ->from('tt_content') + ->where( + $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('list')), + $queryBuilder->expr()->eq('list_type', $queryBuilder->createNamedParameter('news_pi1')), + ) + ->executeQuery(); + + while($contentRow = $queryResult->fetchAssociative()) { + $flexForm = GeneralUtility::xml2array($contentRow['pi_flexform']); + if ( + isset($flexForm['data']['sDEF']['lDEF']['settings.eventRestriction']) + ) { + $uid = $contentRow['uid']; + $oldValue = $flexForm['data']['sDEF']['lDEF']['settings.eventRestriction']['vDEF'] ?? null; + $newValue = $flexForm['data']['extraEntryEventNews']['lDEF']['settings.eventRestriction']['vDEF'] ?? null; + if ($oldValue != $newValue) { + yield $uid => $flexForm; + } + } + } + } + + protected function getUpdateStatement(): \Doctrine\DBAL\Statement + { + $connection = $this->connectionPool->getConnectionForTable('tt_content'); + $queryBuilder = $connection->createQueryBuilder(); + $updateSql = $queryBuilder + ->update('tt_content') + ->set('pi_flexform', $queryBuilder->createPositionalParameter('flex'), false) + ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createPositionalParameter(0, \PDO::PARAM_INT))) + ->getSQL(); + return $connection->prepare($updateSql); + } + + protected function updateFlexForm(array $flexForm): array + { + $newFlexForm = $flexForm; + $newFlexForm['data']['extraEntryEventNews']['lDEF']['settings.eventRestriction'] = $flexForm['data']['sDEF']['lDEF']['settings.eventRestriction']; + unset($newFlexForm['data']['sDEF']['lDEF']['settings.eventRestriction']); + return $newFlexForm; + } +} \ No newline at end of file diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 8eab867..1de8d0f 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -7,3 +7,6 @@ services: GeorgRinger\Eventnews\: resource: '../Classes/*' exclude: '../Classes/Domain/Model/*' + + GeorgRinger\Eventnews\Update\UpdatePluginFlexforms: + public: true diff --git a/ext_localconf.php b/ext_localconf.php index 82b8baa..91322da 100755 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -6,6 +6,9 @@ $GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['newItems']['News->month'] = 'Month view'; +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['tx_eventnews_updateFlexforms'] + = \GeorgRinger\Eventnews\Update\UpdatePluginFlexforms::class; + /*********** * Hooks */