Skip to content

Instantly share code, notes, and snippets.

@josefglatz
Created August 17, 2015 15:02
Show Gist options
  • Save josefglatz/df27fbc1e141e81c5ef5 to your computer and use it in GitHub Desktop.
Save josefglatz/df27fbc1e141e81c5ef5 to your computer and use it in GitHub Desktop.
ext:typoscript2ce Update script v0.1.0 to v1.0.2
<?php
/**
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class ext_update {
/**
* @var \TYPO3\CMS\Core\Database\DatabaseConnection
*/
protected $databaseConnection;
/**
* Constructor
*/
public function __construct() {
$this->databaseConnection = $GLOBALS['TYPO3_DB'];
}
/**
* Called by the extension manager to determine if the update menu entry
* should by showed.
*
* @return bool
*/
public function access() {
return TRUE;
}
/**
* Main update function called by the extension manager.
*
* @return string
*/
public function main() {
$this->renameFlexformField('typoscript2ce_pi1', array('main', 'tsobject'), array('main', 'settings.typoscriptobjectpath'));
}
/**
* Renames a flex form field
*
* @param string $pluginName The pluginName used in list_type
* @param array $oldFieldPointer Pointer array the old field. E.g. array('sheetName', 'fieldName');
* @param array $newFieldPointer Pointer array the new field. E.g. array('sheetName', 'fieldName');
* @return void
*/
protected function renameFlexformField($pluginName, array $oldFieldPointer, array $newFieldPointer) {
$title = 'Renaming flexform field for "' . $pluginName . '" - ' .
' sheet: ' . $oldFieldPointer[0] . ', field: ' . $oldFieldPointer[1] . ' to ' .
' sheet: ' . $newFieldPointer[0] . ', field: ' . $newFieldPointer[1];
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid, pi_flexform', 'tt_content', 'CType=\'list\' AND list_type=\'' . $pluginName . '\'');
$flexformTools = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools');
/* @var $flexformTools \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools */
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$xmlArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($row['pi_flexform']);
if (!is_array($xmlArray) || !isset($xmlArray['data'])) {
// Something went wrong
} elseif (!$xmlArray['data'][$oldFieldPointer[0]]) {
// Something went wrong
} else {
$updated = false;
foreach ($xmlArray['data'][$oldFieldPointer[0]] as $language => $fields) {
if ($fields[$oldFieldPointer[1]]) {
$xmlArray['data'][$newFieldPointer[0]][$language][$newFieldPointer[1]] = $fields[$oldFieldPointer[1]];
unset($xmlArray['data'][$oldFieldPointer[0]][$language][$oldFieldPointer[1]]);
$updated = true;
}
}
if ($updated === true) {
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_content', 'uid=' . $row['uid'], array(
'pi_flexform' => $flexformTools->flexArray2Xml($xmlArray)
));
}
}
}
}
}
?>
@josefglatz
Copy link
Author

Not perfect (btw!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment