Skip to content

Instantly share code, notes, and snippets.

@matdave
Last active June 21, 2024 16:27
Show Gist options
  • Save matdave/6ad905d4af03f34e38e9e44eb2b48ac1 to your computer and use it in GitHub Desktop.
Save matdave/6ad905d4af03f34e38e9e44eb2b48ac1 to your computer and use it in GitHub Desktop.
Custom GPM Resolvers
<?php
use MODX\Revolution\modResource;
use MODX\Revolution\modTemplate;
use MODX\Revolution\modContentType;
use MODX\Revolution\modX;
use xPDO\Transport\xPDOTransport;
/**
* @var xPDOTransport $transport
* @var array $object
* @var array $options
*/
if ($options[xPDOTransport::PACKAGE_ACTION] === xPDOTransport::ACTION_UNINSTALL ||
$options[xPDOTransport::PACKAGE_ACTION] === xPDOTransport::ACTION_UPGRADE
) {
return true;
}
/** @var modX $modx */
$modx =& $transport->xpdo;
$namespace = 'package';
$corePath = $modx->getOption($namespace . '.core_path', null, $modx->getOption('core_path') . 'components/' . $namespace . '/');
$resourcePath = realpath($corePath . 'resources') . '/';
// Load resources from the HTML and CSS files in the $resourcePath
$resources = [];
$files = glob($resourcePath . '{**/*,*}', GLOB_BRACE);
foreach ($files as $file) {
// skip directories
if (is_dir($file)) {
continue;
}
$parent = basename(dirname($file));
$pagetitle = basename($file, '.html');
// remove css and js extensions from pagetitle as well
$pagetitle = str_replace(['.css', '.js'], '', $pagetitle);
$resources[basename($file, '.html')] = [
'pagetitle' => $pagetitle,
'template' => ($pagetitle === 'Home') ? 'Home Page' : 'Inside Page',
'parent' => ($parent === 'resources') ? 0 : $parent,
'published' => true,
'isfolder' => false,
'content' => file_get_contents($file),
'content_type' => '.' . pathinfo($file, PATHINFO_EXTENSION),
];
}
// Create the resources
foreach ($resources as $resource) {
if ($resource['parent'] !== 0) {
$parent = $modx->getObject(modResource::class, ['alias' => $resource['parent']]);
if (!$parent) {
$modx->log(modX::LOG_LEVEL_INFO, 'Creating parent resource ' . $resource['parent']);
$parent = $modx->newObject(modResource::class);
$parent->set('pagetitle', $resource['parent']);
$parent->set('alias', $resource['parent']);
$parent->set('published', true);
$parent->set('isfolder', true);
$parent->set('hidemenu', true);
$parent->set('alias', $parent->cleanAlias($resource['parent']));
$parent->save();
}
$resource['parent'] = $parent->get('id');
}
$template = $modx->getObject(modTemplate::class, ['templatename' => $resource['template']]);
if (!$template) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Template ' . $resource['template'] . ' not found.');
continue;
}
$resource['template'] = $template->get('id');
if ($resource['content_type'] !== '.html') {
$resource['template'] = 0;
}
$contentType = $modx->getObject(modContentType::class, ['file_extensions' => $resource['content_type']]);
if (!$contentType) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Content type ' . $resource['content_type'] . ' not found.');
continue;
}
$resource['content_type'] = $contentType->get('id');
$resourceObj = $modx->getObject(modResource::class, ['pagetitle' => $resource['pagetitle']]);
if ($resourceObj) {
$modx->log(modX::LOG_LEVEL_INFO, 'Resource ' . $resource['pagetitle'] . ' already exists.');
continue;
}
$resourceObj = $modx->newObject(modResource::class);
$resourceObj->fromArray($resource);
$resourceObj->set('alias', $resourceObj->cleanAlias($resource['pagetitle']));
if ($resourceObj->save()) {
$modx->log(modX::LOG_LEVEL_INFO, 'Created resource ' . $resource['pagetitle']);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not create resource ' . $resource['pagetitle']);
}
}
<?php
use MODX\Revolution\modTemplate;
use MODX\Revolution\modTemplateVar;
use MODX\Revolution\modTemplateVarTemplate;
use MODX\Revolution\modCategory;
use MODX\Revolution\modX;
use xPDO\Transport\xPDOTransport;
/**
* @var xPDOTransport $transport
* @var array $object
* @var array $options
*/
if ($options[xPDOTransport::PACKAGE_ACTION] === xPDOTransport::ACTION_UNINSTALL ||
$options[xPDOTransport::PACKAGE_ACTION] === xPDOTransport::ACTION_UPGRADE
) {
return true;
}
/** @var modX $modx */
$modx =& $transport->xpdo;
$namespace = 'package';
$corePath = $modx->getOption($namespace . '.core_path', null, $modx->getOption('core_path') . 'components/' . $namespace . '/');
$tvsPath = realpath($corePath . 'elements/tvs') . '/';
// Load TVs from the json files in the $tvsPath
$files = glob($tvsPath . '*.json', GLOB_BRACE);
foreach ($files as $tv) {
$content = file_get_contents($tv);
$tv = json_decode($content, true);
$templateVar = $modx->getObject(modTemplateVar::class, ['name' => $tv['name']]);
if (!$templateVar) {
$templateVar = $modx->newObject(modTemplateVar::class);
}
if (isset($tv['category'])) {
$category = $modx->getObject(modCategory::class, ['category' => $tv['category']]);
if ($category) {
$tv['category'] = $category->get('id');
} else {
$tv['category'] = 0;
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not find category ' . $tv['category']);
}
}
$templateVar->fromArray($tv);
if ($templateVar->save()) {
$modx->log(modX::LOG_LEVEL_INFO, 'Created TV ' . $tv['name']);
if (isset($tv['templates'])) {
foreach ($tv['templates'] as $templatename) {
$template = $modx->getObject(modTemplate::class, ['templatename' => $templatename]);
if ($template) {
$templateVarTemplate = $modx->getObject(modTemplateVarTemplate::class, [
'tmplvarid' => $templateVar->get('id'),
'templateid' => $template->get('id'),
]);
if ($templateVarTemplate) {
$modx->log(modX::LOG_LEVEL_INFO, 'TV ' . $tv['name'] . ' already assigned to template ' . $templatename);
continue;
}
$templateVarTemplate = $modx->newObject(modTemplateVarTemplate::class);
$templateVarTemplate->set('tmplvarid', $templateVar->get('id'));
$templateVarTemplate->set('templateid', $template->get('id'));
$templateVarTemplate->save();
} else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not find template ' . $templatename);
}
}
}
} else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not create TV ' . $tv['name']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment