Skip to content

Instantly share code, notes, and snippets.

@Llewellynvdm
Created May 30, 2020 17:37
Show Gist options
  • Save Llewellynvdm/e053dc39ae3b2bf769c76a3e62c75b95 to your computer and use it in GitHub Desktop.
Save Llewellynvdm/e053dc39ae3b2bf769c76a3e62c75b95 to your computer and use it in GitHub Desktop.
// load the helper class
JLoader::register('[[[Component]]]Helper', JPATH_ADMINISTRATOR . '/components/com_[[[component]]]/helpers/[[[component]]].php');
// check the version of JCB
$manifest = [[[Component]]]Helper::manifest();
if (isset($manifest->version) && strpos($manifest->version, '.') !== false)
{
// get the version
$this->JCBversion = explode('.', $manifest->version);
// Get a db connection.
$db = JFactory::getDbo();
// target version less then or equal to 2.11.2 // <-- change these!!!!
if (count($this->JCBversion) == 3 && $this->JCBversion[0] <= 2 && ($this->JCBversion[1] < 11 || ($this->JCBversion[1] == 11 && $this->JCBversion[2] <= 2)))
{
// we need to make a database correction for the field categories and the fieldtype categories
$fix_categories = array(
'com_componentbuilder.fields' => 'com_componentbuilder.field', // <-- change these!!!!
'com_componentbuilder.fieldtypes' => 'com_componentbuilder.fieldtype' // <-- change these!!!!
);
// targeted tables (to fix all places categories are mapped into Joomla)
$fix_tables = array(
'content_types' => array(
'id' => 'type_id',
'key' => 'type_alias',
'suffix' => '.category'),
'contentitem_tag_map' => array(
'id' => 'type_id',
'key' => 'type_alias',
'suffix' => '.category'),
'ucm_content' => array(
'id' => 'core_content_id',
'key' => 'core_type_alias',
'suffix' => '.category'),
'categories' => array(
'id' => 'id',
'key' => 'extension',
'suffix' => '')
);
// the script that does the work
foreach ($fix_categories as $fix => $category)
{
// loop over the targeted tables
foreach ($fix_tables as $_table => $_update)
{
// Create a new query object.
$query = $db->getQuery(true);
// get all type_ids
$query->select($db->quoteName($_update['id']));
$query->from($db->quoteName('#__' . $_table));
$query->where( $db->quoteName($_update['key']) . ' = ' . $db->quote($fix . $_update['suffix']));
// Reset the query using our newly populated query object.
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
// all these must be updated
$ids = $db->loadColumn();
// Fields to update.
$fields = array(
$db->quoteName($_update['key']) . ' = ' . $db->quote($category . $_update['suffix'])
);
// Conditions for which records should be updated.
$conditions = array(
$db->quoteName($_update['id']) . ' IN (' . implode(', ', $ids) . ')'
);
$query->update($db->quoteName('#__' . $_table))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
// on success
if ($result)
{
$app->enqueueMessage("<p>Updated <b>#__$_table - " . $_update['key'] . "</b> from <b>$fix</b>" . $_update['suffix'] . " to <b>$category</b>" . $_update['suffix'] . "!</p>", 'Notice');
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment