Skip to content

Instantly share code, notes, and snippets.

@bmoex
Last active April 26, 2019 10:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoex/5135f3e6685a358a54e6c955bf64d21b to your computer and use it in GitHub Desktop.
Save bmoex/5135f3e6685a358a54e6c955bf64d21b to your computer and use it in GitHub Desktop.
Migration from "old" mailforms to "new" form extension
<?php
// Location: EXT:yourextension/ext_localconf.php
defined('TYPO3_MODE') or die ('Access denied.');
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['serfhos_deprecated_mailform'] = \Serfhos\OldMailMigration\Install\Updates\MailFormDataUpdate::class;
<?php
// Location: EXT:yourextension/Classes/Install/Updates/MailFormDataUpdate.php
namespace Serfhos\OldMailMigration\Install\Updates;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MailUtility;
/**
* Migrate "mailform" data to new 7.6 ext:form
*/
class MailFormDataUpdate extends \TYPO3\CMS\Install\Updates\AbstractUpdate
{
/**
* @var array
*/
protected $currentRow = [];
/**
* @var string
*/
protected $title = 'Migrate "mailform" data to new 7.6 ext:form';
/**
* Checks if an update is needed
*
* @param string &$description The description for the update
* @return bool Whether an update is needed (TRUE) or not (FALSE)
*/
public function checkForUpdate(&$description)
{
$query = $this->getMigrateFormElementsQuery();
$oldDataSets = $this->getDatabaseConnection()->exec_SELECTcountRows('tt_content.uid', $query['FROM'], $query['WHERE']);
if ($this->isWizardDone() || $oldDataSets === 0) {
return false;
}
$description = 'As the old mailform is moved to compatibility6 (since 7), this migration is to move the data to the new mailform functionality.';
return true;
}
/**
* Performs the database migrations if requested
*
* @param array &$databaseQueries Queries done in this update
* @param mixed &$customMessages Custom messages
* @return boolean
*/
public function performUpdate(array &$databaseQueries, &$customMessages)
{
$res = $this->getDatabaseConnection()->exec_SELECT_queryArray($this->getMigrateFormElementsQuery());
while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
$this->currentRow = $row;
$updateFields = [
'tstamp' => time(),
'subheader' => '',
'pages' => '',
'bodytext' => $this->getNewFormSetup($row),
];
$this->getDatabaseConnection()->exec_UPDATEquery('tt_content', 'uid = ' . $row['uid'], $updateFields);
$databaseQueries[] = $this->getDatabaseConnection()->debug_lastBuiltQuery;
}
$this->markWizardAsDone();
return true;
}
/**
* Get all old configuration for crmpost
*
* @param array $row
* @return string
*/
protected function getNewFormSetup($row)
{
$formSetup = [
'prefix' => 'tx_form',
'confirmation' => 1,
'postProcessor.' => [
'1' => 'mail',
'1.' => [
'recipientEmail' => $row['subheader'],
'senderEmail' => MailUtility::getSystemFromAddress(),
'subject' => '',
],
'2' => 'redirect',
'2.' => [
'destination' => $row['pages'],
],
],
];
$formFields = GeneralUtility::trimExplode(LF, $row['bodytext'], true);
foreach ($formFields as $i => $field) {
$this->addFieldSetup($i + 1, $field, $formSetup);
}
return $this->typoscriptArrayToString($formSetup);
}
/**
* Calculate field setup based on setup line
*
* @param integer $index
* @param string $setupLine
* @param array $formSetup
* @throws \Exception
*/
protected function addFieldSetup($index, $setupLine, &$formSetup)
{
$configs = explode('|', $setupLine);
$label = trim(array_shift($configs));
list($name, $typeSetup) = explode('=', array_shift($configs));
$name = trim($name);
$required = false;
if (strpos($name, '*') === 0) {
$name = substr($name, 1);
$required = true;
}
$typeSetup = explode(',', $typeSetup);
$type = trim($typeSetup[0]);
$options = trim(implode('|', $configs));
unset($configs);
switch ($type) {
case 'input':
$formSetup[$index . '0'] = 'TEXTLINE';
$formSetup[$index . '0.'] = [
'type' => 'text',
'name' => $name,
'label.' => [
'value' => $label
]
];
if ($required) {
$formSetup[$index . '0.']['required'] = 'required';
}
break;
case 'textarea':
$formSetup[$index . '0'] = 'TEXTAREA';
$formSetup[$index . '0.'] = [
'name' => $name,
'cols' => ($typeSetup[1] ?: 40),
'rows' => ($typeSetup[2] ?: 5),
'label.' => [
'value' => $label
]
];
if ($required) {
$formSetup[$index . '0.']['required'] = 'required';
}
break;
case 'select':
$values = explode(',', $options);
$formSetup[$index . '0'] = 'SELECT';
$formSetup[$index . '0.'] = [
'name' => $name,
'label.' => [
'value' => $label
],
];
if ($required) {
$formSetup[$index . '0.']['required'] = 'required';
}
foreach ($values as $i => $option) {
$formSetup[$index . '0.'][$i + 1 . '0'] = 'OPTION';
$formSetup[$index . '0.'][$i + 1 . '0.'] = [
'text' => trim($option),
'value' => trim($option),
];
// Make sure first is selected
if ($i === 0) {
$formSetup[$index . '0.'][$i + 1 . '0.']['selected'] = 'selected';
}
}
if ($required) {
$formSetup[$index . '0.']['required'] = 'required';
}
break;
case 'hidden':
if ($name === 'subject') {
$formSetup['postProcessor.']['1.']['subject'] = $options;
} elseif ($name === 'html_enabled') {
// Deprecated..
} else {
$formSetup[$index . '0'] = 'HIDDEN';
$formSetup[$index . '0.'] = [
'type' => 'hidden',
'name' => $name,
'value' => $options,
];
}
break;
case 'submit':
$formSetup[$index . '0'] = 'SUBMIT';
$formSetup[$index . '0.'] = [
'type' => 'submit',
'name' => $name,
'value' => $options,
];
break;
case '':
// No form setup in this line, maybe a comment?
break;
default:
throw new \Exception('Unknown type: ' . $type . ' - Page: ' . $this->currentRow['pid']);
}
}
/**
* Get query parts for generic workflow
*
* @return array
*/
protected function getMigrateFormElementsQuery()
{
return [
'SELECT' => 'tt_content.*',
'FROM' => 'tt_content, pages',
'WHERE' => 'tt_content.pid= pages.uid'
. ' AND tt_content.CType = "mailform"'
. ' AND tt_content.subheader <> ""'
. ' AND tt_content.bodytext LIKE "%formtype_mail=submit%"'
. ' AND tt_content.hidden = 0 AND tt_content.deleted = 0'
. ' AND pages.hidden = 0 AND pages.deleted = 0'
];
}
/**
* Converts a TypoScript array to a formatted string
* Takes care of indentation, curly brackets and parentheses
*
* @param array $typoscriptArray The TypoScript array
* @param string $addKey Key which has underlying configuration
* @param int $tabCount The amount of tabs for indentation
* @return string The formatted TypoScript string
* @see \TYPO3\CMS\Form\Domain\Factory\JsonToTypoScript::typoscriptArrayToString()
*/
protected function typoscriptArrayToString(array $typoscriptArray, $addKey = '', $tabCount = -1)
{
$typoscript = '';
if ($addKey != '') {
$typoscript .= str_repeat(TAB, $tabCount) . str_replace('.', '', $addKey) . ' {' . LF;
}
$tabCount++;
foreach ($typoscriptArray as $key => $value) {
if (!is_array($value)) {
if (strstr($value, LF)) {
$typoscript .= str_repeat(TAB, $tabCount) . $key . ' (' . LF;
if ($key !== 'text') {
$value = str_replace(LF, LF . str_repeat(TAB, ($tabCount + 1)), $value);
$typoscript .= str_repeat(TAB, ($tabCount + 1)) . $value . LF;
} else {
$typoscript .= $value . LF;
}
$typoscript .= str_repeat(TAB, $tabCount) . ')' . LF;
} else {
$typoscript .= str_repeat(TAB, $tabCount) . $key . ' = ' . $value . LF;
}
} else {
$typoscript .= $this->typoscriptArrayToString($value, $key, $tabCount);
}
}
if ($addKey != '') {
$tabCount--;
$typoscript .= str_repeat(TAB, $tabCount) . '}' . LF;
}
return $typoscript;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment