Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carnevlu/600520636e9814a332979acc8f57a222 to your computer and use it in GitHub Desktop.
Save carnevlu/600520636e9814a332979acc8f57a222 to your computer and use it in GitHub Desktop.
MODX install package __ContentBlock__ and import Fields,Layouts,Templates from multiple export files
<?php
define('MODX_API_MODE', true);
// Full path to the index
require_once('index.php');
$modx = new modX();
$modx->initialize('mgr');
######################## Variables
######################## Config
// Config
$provider = "modmore"; // the name of the provider modmore
$searchedPackage = "contentblocks"; // the name of the package to install
// Systems Settings to update
$settings['contentblocks.base_url_mode'] = array(
"key" => "contentblocks.base_url_mode",
"value" => "full",
"namespace" => "contentblocks"
);
$settings['contentblocks.image.upload_path'] = array(
"key" => "contentblocks.image.upload_path",
"value" => "assets/public/uploads/",
"namespace" => "contentblocks"
);
// Content Block settings
$pathToContentBlockExports = "/webroot/contentblocks/";
// Paramenters for the import of Fields/Layouts/Templates
$settings_cb['fields'] = array(
"classKey" => "cbField", // optional
"mode" => "overwrite", // optional valid value: insert | overwrite | replace
"file" => array( // not elegan way to bypass contentblock / simulate a uploaded file by MODX
"name" => "FakeNameFile.xml",
"tmp_name" => $pathToContentBlockExports . "cbField_v1.0.2.xml", // path to the file hosted and accessible on the webserver
"error" => 0,
"type" => "text/xml"
)
);
$settings_cb['layouts'] = array(
"classKey" => "cbLayout",
"mode" => "overwrite",
"file" => array(
"name" => "FakeNameFile.xml",
"tmp_name" => $pathToContentBlockExports . "cbLayout_v1.0.1.locale.xml",
"error" => 0,
"type" => "text/xml"
)
);
$settings_cb['templates'] = array(
"classKey" => "cbTemplate",
"mode" => "overwrite",
"file" => array(
"name" => "FakeNameFile.xml",
"tmp_name" => $pathToContentBlockExports . "cbTemplate_v0.1.0.xml",
"error" => 0,
"type" => "text/xml"
)
);
######################## Main code
// to avoid conflic if the package is already here
$modx2 = clone $modx;
// Detect the provider Id
$tmp = $modx->getObject('transport.modTransportProvider', array(
"name" => $provider
));
$provider = $tmp->_fields['id'];
$provider = (is_numeric($provider)) ? $provider : 1; // in case of not found => use the provider 1
// Detect the package informations from the provider
$someMoreInformations = getPackageUrl($modx2, $searchedPackage, $provider);
$package_url = $someMoreInformations['package_url'];
$package_signature = $someMoreInformations['signature'];
// Download the package
$modx->runProcessor('workspace/packages/rest/download', array(
'info' => $package_url . '::' . $package_signature,
'provider' => $provider
));
// Create modTransportPackage object in new modx install
$scan = $modx->runProcessor('workspace/packages/scanlocal');
// Run install command
$response = $modx->runProcessor('workspace/packages/install', array(
'signature' => $package_signature
));
echo $response->response['message'] . "\n\r";
// update the MODX systems settings
$processor = "system/settings/update";
$options = array();
foreach ($settings as $s) {
$result = $modx2->runProcessor($processor, $s, $options);
if (method_exists($result, "isError")) {
if ($result->isError()) {
echo "Unable to update " . $s['key'] . ": " . $s['value'] . "\n\r";
} else {
echo "Update " . $s['key'] . ": " . $s['value'] . "\n\r";
}
}
}
// Start the import of the standard Fields,Layouts,Template
// import the standard Fields,Layouts,Template from the array $settings_cb
$corePath = $modx->getOption('contentblocks.core_path', null, $modx->getOption('core_path') . 'components/contentblocks/');
$contentBlocks = $modx->getService('contentblocks', 'ContentBlocks', $corePath . 'model/contentblocks/');
if (!$contentBlocks) {
echo 'Unable to load ContentBlocks service\n\r';
} else {
foreach ($settings_cb as $key => $scb) {
// set the properties/ScriptProperties with the correct processors path as option
$props = $scb;
$options = array(
'processors_path' => $contentBlocks->config['processorsPath']
);
// Simulate the upload of the file (the path should be accessible from the webserver)
$_FILES['file'] = $scb['file'];
unset($scb['file']);
// Run the ContentBlock process via MODX (Thx mhamstra)
$response = $modx->runProcessor('mgr/' . $key . '/import', $props, $options);
// Print the message generate from CB (at the moment is a emtpy string)
echo $response->response['message'] . "\n\r";
// Give a feedback to the user.
if ($response->isError()) {
echo "Error unable to import $key\n\r";
} else {
echo "$key imported correctly\n\r";
}
}
// Now regenerate all the contents
$props = array();
$options = array(
'processors_path' => $contentBlocks->config['processorsPath']
);
// run the process content rebuild
$response = $modx->runProcessor('mgr/rebuild_content', $props, $options);
echo $response->response['message'] . "\n\r";
// give a feedback
if ($response->isError()) {
echo "Error unable to rebuild the content\n\r";
} else {
echo "Content rebuild correctly\n\r";
}
}
######################## Functions
function getPackageUrl($modx, $query = "content", $provider = 5)
{
// retrive the list of packages available with the query
$result = $modx->runProcessor('workspace/packages/rest/getlist', array(
'query' => $query,
"provider" => $provider
));
// decode the JSON reply
$result = json_decode($result->response, 1);
$result = $result['results'];
// check if there is element in the reply
if (count($result) == 0) {
return false;
}
// isolate the first
// TODO implement a test of the signature?
$current = current($result);
return array(
"signature" => $current['signature'],
"package_url" => $current['location']
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment