Created
December 10, 2015 20:08
-
-
Save mneuhaus/74f4642e6914be7e01fb to your computer and use it in GitHub Desktop.
Command controller to move values inside a flexform array into another sheet. this is sometimes needed, if you move your fields in the backend into other sheets, etc. which causes an issue because the data is still in the old sheet and gets outputted in the frontend, but isn't accessible anymore in the backend. beware, this might break your cont…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Famelo\Template\Command; | |
| use TYPO3\CMS\Core\Utility\GeneralUtility; | |
| use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools; | |
| /* * | |
| * This script is part of the TYPO3 project - inspiring people to share! * | |
| * * | |
| * TYPO3 is free software; you can redistribute it and/or modify it under * | |
| * the terms of the GNU General Public License version 2 as published by * | |
| * the Free Software Foundation. * | |
| * * | |
| * This script is distributed in the hope that it will be useful, but * | |
| * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- * | |
| * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * | |
| * Public License for more details. * | |
| * */ | |
| class FlexformCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController { | |
| /** | |
| * @var \FluidTYPO3\Fluidpages\Provider\PageProvider | |
| * @inject | |
| */ | |
| protected $pageProvider; | |
| public function repairCommand() { | |
| $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'pages', '1=1'); | |
| $fieldName = 'tx_fed_page_flexform'; | |
| $flexFormTools = new FlexFormTools(); | |
| foreach ($pages as $key => $page) { | |
| if (!empty($page[$fieldName])) { | |
| echo 'processing page: ' . $page['title'] . chr(10); | |
| $start = microtime(TRUE); | |
| $form = $this->pageProvider->getForm($page); | |
| echo 'got form'; | |
| $map = array(); | |
| foreach ($form->getSheets() as $sheet) { | |
| foreach ($sheet->getFields() as $field) { | |
| $map[$field->getName()] = $sheet->getName(); | |
| } | |
| } | |
| echo ' found ' . count($map) . ' fields'; | |
| $flexFormArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($page[$fieldName]); | |
| foreach ($flexFormArray['data'] as $sheetName => $sheetArray) { | |
| foreach ($sheetArray['lDEF'] as $flexFieldName => $fieldArray) { | |
| echo '.'; | |
| if ($map[$flexFieldName] !== $sheetName) { | |
| unset($flexFormArray['data'][$sheetName]['lDEF'][$flexFieldName]); | |
| if (!isset($flexFormArray['data'][$map[$flexFieldName]]['lDEF'][$flexFieldName])) { | |
| $flexFormArray['data'][$map[$flexFieldName]]['lDEF'][$flexFieldName] = $fieldArray; | |
| } | |
| } | |
| } | |
| if (empty($flexFormArray['data'][$sheetName]['lDEF'])) { | |
| unset($flexFormArray['data'][$sheetName]); | |
| } | |
| } | |
| $page[$fieldName] = $flexFormTools->flexArray2Xml($flexFormArray, TRUE); | |
| $GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'uid = ' . $page['uid'], array( | |
| $fieldName => $page[$fieldName] | |
| )); | |
| echo chr(10); | |
| echo number_format((microtime(TRUE) - $start), 2) . 'ms / ' . intval(memory_get_peak_usage(TRUE) / 1024 / 1024) . 'mb' . chr(10) . chr(10); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment