Skip to content

Instantly share code, notes, and snippets.

@morelmm
Created November 17, 2016 18:51
Show Gist options
  • Save morelmm/0b00bd67b5f4f9b40a16ea2ed8c80bc9 to your computer and use it in GitHub Desktop.
Save morelmm/0b00bd67b5f4f9b40a16ea2ed8c80bc9 to your computer and use it in GitHub Desktop.
Modx/babel recursive translation generation
<?php
$id =$scriptProperties['id'];
$ctxSource = $scriptProperties['ctxSource'];
$ctxDest = $scriptProperties['ctxDest'];
$count = 0;
global $babel;
$babel = $modx->getService('babel','Babel',$modx->getOption('babel.core_path',null,$modx->getOption('core_path').'components/babel/').'model/babel/');
$resources = $modx->getCollection('modResource',
array('parent'=> 0, 'context_key' => $ctxSource));
function generateChildren($resid,$ctxSource,$ctxDest,&$count){
global $modx;
global $babel;
$children = $modx->getCollection('modResource',
array('parent' => $resid, 'context_key' => $ctxSource));
foreach ($children as $child) {
$linkedResources = $babel->getLinkedResources($child->get('id'));
if ($linkedResources[$ctxDest] == '') {
$newObject = $babel->duplicateResource($child, $ctxDest);
$count++;
$linkedResources = $babel->getLinkedResources($child->get('id'));
if(empty($linkedResources)) {
$linkedResources[$ctxSource] = $child->get('id');
}
$linkedResources[$ctxDest] = $newObject->get('id');
$babel->updateBabelTv($linkedResources, $linkedResources);
unset($newObject);
}
generateChildren($child->get('id'),$ctxSource,$ctxDest,$count);
}
}
foreach($resources as $resource) {
$linkedResources = $babel->getLinkedResources($resource->get('id'));
if ($linkedResources[$ctxDest] == '') {
$newObject = $babel->duplicateResource($resource, $ctxDest);
$count++;
if(empty($linkedResources)) {
$linkedResources[$ctxSource] = $resource->get('id');
}
$linkedResources[$ctxDest] = $newObject->get('id');
$babel->updateBabelTv($linkedResources, $linkedResources);
unset($newObject);
}
generateChildren($resource->get('id'),$ctxSource,$ctxDest,$count);
}
return($count);
@hugopeek
Copy link

Thanks, worked like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment