Skip to content

Instantly share code, notes, and snippets.

@matdave
Last active October 11, 2019 15:14
Show Gist options
  • Save matdave/af88ff730faf78af478f562fb298d007 to your computer and use it in GitHub Desktop.
Save matdave/af88ff730faf78af478f562fb298d007 to your computer and use it in GitHub Desktop.
Maxi to MIGX
<?php
ini_set('max_execution_time', 72000);
ini_set('memory_limit', '2048M');
define('MODX_CORE_PATH', '/home/www/core/');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
if(!function_exists('csv2array')){
function csv2array($filename = '', $delimiter = ',', $asHash = true)
{
if (!(is_readable($filename) || (($status = get_headers($filename)) && strpos($status[0], '200')))) {
return FALSE;
}
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE) {
if ($asHash) {
while ($row = fgetcsv($handle, 0, $delimiter)) {
if (!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
} else {
while ($row = fgetcsv($handle, 0, $delimiter)) {
$data[] = $row;
}
}
fclose($handle);
}
return $data;
}
}
$modx= new modX();
$modx->initialize('mgr');
//adjust import path
$imports = 'import/';
$csv = 'modx_maxigallery.csv';
$array = csv2array($imports.$csv);
$galleryTV = 7;
//used for reassigning page if id's changed
$galleries = array(
108 => array('newid' => 96, 'items'=> array()),
106 => array('newid' => 101, 'items'=> array()),
111 => array('newid' => 97, 'items'=> array()),
109 => array('newid' => 98, 'items'=> array()),
107 => array('newid' => 100, 'items'=> array()),
105 => array('newid' => 43, 'items'=> array()),
128 => array('newid' => 128, 'items'=> array()),
110 => array('newid' => 99, 'items'=> array())
);
//"id","gal_id","filename","title","date","descr","pos","own_id","hide"
//{"MIGX_id":"1","image":"bill_with_group_for_web.jpg","deleted":"0","published":"1","published_ro":"{\"MIGX_id\":\"2\",\"name\":\"published\",\"use_as_fallback\":\"\",\"value\":\"1\",\"clickaction\":\"switchOption\",\"handler\":\"\",\"image\":\"assets\\/components\\/migx\\/style\\/images\\/cb_ticked.png\",\"idx\":0,\"_renderer\":\"this.renderSwitchStatusOptions\",\"selectorconfig\":\"\"}"},
if(!empty($array)){
//separate galleries
foreach($array as $v){
$v['pos'] = (int)$v['pos'];
$v['gal_id'] = (int)$v['gal_id'];
$galleries[$v['gal_id']]['items'][$v['pos']] = array(
'MIGX_id' => $v['pos'],
'image'=>$v['filename'],
'title' => $v['title'],
'description' => $v['desc'],
'deleted' => 0,
'published' => 1,
'published_ro' => '{\"MIGX_id\":\"2\",\"name\":\"published\",\"use_as_fallback\":\"\",\"value\":\"1\",\"clickaction\":\"switchOption\",\"handler\":\"\",\"image\":\"assets\\/components\\/migx\\/style\\/images\\/cb_ticked.png\",\"idx\":0,\"_renderer\":\"this.renderSwitchStatusOptions\",\"selectorconfig\":\"\"}'
);
}
//process into TVs
foreach($galleries as $g){
$gallery = $modx->getObject('modTemplateVarResource', array(
'tmplvarid' => $galleryTV,
'contentid' => $g['newid']
));
if(!empty($gallery) && !empty($g['items'])){
echo "Altering Gallery for ".$g['newid']." \r\n";
$gallery->set('value', $modx->toJSON(array_values($g['items'])));
$gallery->save();
}else{
echo "Skipping Gallery for ".$g['newid']." \r\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment