Skip to content

Instantly share code, notes, and snippets.

@matdave
Created August 2, 2018 15:34
Show Gist options
  • Save matdave/4f994bd5764e11f5b0c052a3c51a6b88 to your computer and use it in GitHub Desktop.
Save matdave/4f994bd5764e11f5b0c052a3c51a6b88 to your computer and use it in GitHub Desktop.
Fred Migration for GPM (Run once)
<?php
$corePath = $modx->getOption('fred.core_path', null, $modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/fred/');
/** @var Fred $fred */
$fred = $modx->getService(
'fred',
'Fred',
$corePath . 'model/fred/',
array(
'core_path' => $corePath
)
);
$optionsMap = [];
$rootCategory = (int)$modx->getOption('fred.elements_category_id');
$c = $modx->newQuery('modCategory');
$c->where([
'parent' => $rootCategory
]);
/** @var \modCategory[] $categories */
$categories = $modx->getIterator('modCategory', $c);
foreach ($categories as $category) {
$newCategory = $modx->newObject('FredElementCategory');
$newCategory->set('name', $category->category);
$newCategory->set('rank', $category->rank);
$newCategory->save();
/** @var \modChunk[] $chunks */
$chunks = $modx->getIterator('modChunk', ['category' => $category->id]);
foreach ($chunks as $chunk) {
$matches = [];
preg_match('/image:([^\n]+)\n?/', $chunk->description, $matches);
$image = '';
$options = [];
$description = $chunk->description;
if (count($matches) == 2) {
$image = $matches[1];
$description = str_replace($matches[0], '', $description);
}
$matches = [];
preg_match('/options:([^\n]+)\n?/', $description, $matches);
$optionSet = 0;
if (count($matches) == 2) {
if (!empty($optionsMap[$matches[1]])) {
$optionSet = $optionsMap[$matches[1]];
} else {
/** @var modChunk $options */
$options = $modx->getObject('modChunk', ['name' => $matches[1]]);
if ($options) {
$optionSetObject = $modx->newObject('FredElementOptionSet');
$optionSetObject->set('name', $matches[1]);
$optionSetObject->set('data', json_decode($options->content, true));
$optionSetObject->set('complete', true);
$optionSetObject->save();
$optionsMap[$matches[1]] = $optionSetObject->id;
$optionSet = $optionSetObject->id;
}
}
$description = str_replace($matches[0], '', $description);
}
$newElement = $modx->newObject('FredElement');
$newElement->set('id', $chunk->id);
$newElement->set('name', $chunk->name);
$newElement->set('description', $description);
$newElement->set('image', $image);
$newElement->set('content', $chunk->content);
$newElement->set('category', $newCategory->id);
$newElement->set('option_set', $optionSet);
$newElement->save();
}
}
$globalRteConfig = $modx->getOption('fred.rte_config');
if (!empty($globalRteConfig)) {
$config = $modx->getChunk($globalRteConfig);
$config = json_decode($config, true);
if (!empty($config)) {
$fredRTEConfig = $modx->newObject('FredElementRTEConfig');
$fredRTEConfig->set('name', 'TinyMCE');
$fredRTEConfig->set('data', $config);
$fredRTEConfig->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment