Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CMS PR 40626] Add migration on update #14

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions administrator/components/com_admin/script.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,103 @@ public function postflight($action, $installer)
}

// Add here code which shall be executed only when updating from an older version than 5.0.0
if (!$this->migrateTinymceConfiguration()) {
return false;
}

return true;
}

/**
* Migrate TinyMCE editor plugin configuration
*
* @return boolean True on success
*
* @since __DEPLOY_VERSION__
*/
private function migrateTinymceConfiguration(): bool
{
$db = Factory::getDbo();

try {
// Get the TinyMCE editor plugin's parameters
$params = $db->setQuery(
$db->getQuery(true)
->select($db->quoteName('params'))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('folder') . ' = ' . $db->quote('editors'))
->where($db->quoteName('element') . ' = ' . $db->quote('tinymce'))
)->loadResult();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';

return false;
}

$params = json_decode($params, true);

// If there are no toolbars there is nothing to migrate
if (!isset($params['configuration']['toolbars'])) {
return true;
}

// Each set has its own toolbar configuration
foreach ($params['configuration']['toolbars'] as $setIdx => $toolbarConfig) {
// Migrate menu items if there is a menu
if (isset($toolbarConfig['menu'])) {
/**
* Replace array values with menu item names ("old name" -> "new name"):
* "blockformats" -> "blocks"
* "fontformats" -> "fontfamily"
* "fontsizes" -> "fontsize"
* "formats" -> "styles"
* "template" -> "jtemplate"
*/
$params['configuration']['toolbars'][$setIdx]['menu'] = str_replace(
['blockformats', 'fontformats', 'fontsizes', 'formats', 'template'],
['blocks', 'fontfamily', 'fontsize', 'styles', 'jtemplate'],
$toolbarConfig['menu']
);
}

// There could be no toolbar at all, or only toolbar1, or both toolbar1 and toolbar2
foreach (['toolbar1', 'toolbar2'] as $toolbarIdx) {
// Migrate toolbar buttons if that toolbar exists
if (isset($toolbarConfig[$toolbarIdx])) {
/**
* Replace array values with button names ("old name" -> "new name"):
* "fontselect" -> "fontfamily"
* "fontsizeselect" -> "fontsize"
* "formatselect" -> "blocks"
* "styleselect" -> "styles"
* "template" -> "jtemplate"
*/
$params['configuration']['toolbars'][$setIdx][$toolbarIdx] = str_replace(
['fontselect', 'fontsizeselect', 'formatselect', 'styleselect', 'template'],
['fontfamily', 'fontsize', 'blocks', 'styles', 'jtemplate'],
$toolbarConfig[$toolbarIdx]
);
}
}
}

$params = json_encode($params);

$query = $db->getQuery(true)
->update($db->quoteName('#__extensions'))
->set($db->quoteName('params') . ' = ' . $db->quote($params))
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('folder') . ' = ' . $db->quote('editors'))
->where($db->quoteName('element') . ' = ' . $db->quote('tinymce'));

try {
$db->setQuery($query)->execute();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';

return false;
}

return true;
}
Expand Down