Im confused too - when put together the code wouldnt work properly, when split up into 2 plugins all was fine...
here's the latest version anyway - takes care of changing alias, publishing, deleting etc.
OnDocFormPrerender
<?php
/*CreateSymlinks Plugin for undelete */
switch ($modx->event->name) {
case OnDocFormPrerender:
if ($resource){
$docContext = $resource->get('context_key');
if ($docContext=='web'){
$_SESSION['docAlias_papa_'.$resource->get('id')] = $resource->get('alias');
}
}
break;
}
OnDocFormSave & OnResourceDelete
<?php
/*CreateSymlinks Plugin */
//$docId = $resource->get('id');
//$docAlias = $resource->get('alias');
/* The numbers on the right here are the respective template IDs
* The left members should be the names of your contexts
*/
//$modx->log($modx->LOG_LEVEL_ERROR,'some string');
$contexts = array(
'context1' => 13,
'context2' => 13,
);
$fields = array(
'docAlias' => $docAlias,
'pageTitle' => $resource->get('pagetitle'), /* not sure if this is necessary */
);
switch ($modx->event->name) {
case OnResourceDelete:
$docAlias = $resource->get('alias');
$docContext = $resource->get('context_key');
if ($docContext=='web'){
$clone1 = $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context1'));
if($clone1){
$clone1->set('deleted',true);
$clone1->save();
}
$clone2 = $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context2'));
if($clone2){
$clone2->set('deleted',true);
$clone2->save();
}
//$modx->event->output("<script type=\"text/javascript\">Ext.getCmp('modx-resource-tree').refresh();</script>");
}
case OnDocFormSave:
$e = $modx->Event;
$docId = $e->params['id'];
$docAlias = $e->params['alias'];
$pageTitle = $resource->get('pagetitle');
$pageAlias = $resource->get('alias');
$pageAliasold = $_SESSION['docAlias_papa_'.$docId];
$pagePublished = $resource->get('published');
$docContext = $resource->get('context_key');
if ($docContext=='web'){
if ($mode === modSystemEvent::MODE_NEW) {
foreach($contexts as $contextName => $templateId) {
$fields['context_key'] = $contextName;
$fields['template'] = $templateId;
$symlink = $modx->newObject('modSymlink', $fields);
$symlink->setContent($docId);
$symlink->set('pagetitle', $pageTitle);
$symlink->set('alias', $pageAlias);
if ($pagePublished) $symlink->set('published',true); else $symlink->set('published',false);
//$symlink->set('parent',$parentid);
$symlink->save();
}
}
if ($mode === modSystemEvent::MODE_UPD) {
$clone1 = $modx->getObject('modResource',array('alias' => $pageAliasold,'context_key' => 'context1'));
if($clone1){
if ($pageAliasold!=$pageAlias)$clone1->set('alias', $pageAlias);
if ($pagePublished)$clone1->set('published',true); else $clone1->set('published',false);
$clone1->set('pagetitle', $pageTitle);
$clone1->save();
}
$clone2 = $modx->getObject('modResource',array('alias' => $pageAliasold,'context_key' => 'context2'));
if($clone2){
if ($pageAliasold!=$pageAlias)$clone2->set('alias', $pageAlias);
if ($pagePublished)$clone2->set('published',true); else $clone2->set('published',false);
$clone2->set('pagetitle', $pageTitle);
$clone2->save();
}
//unset($_SESSION['docAlias_papa_'.$docId]);
}
}
break;
}
OnResourceUndelete
<?php
/*CreateSymlinks Plugin for undelete */
//$docId = $resource->get('id');
//$docAlias = $resource->get('alias');
/* The numbers on the right here are the respective template IDs
* The left members should be the names of your contexts
*/
//$modx->log($modx->LOG_LEVEL_ERROR,'some string');
$contexts = array(
'context1' => 13,
'context2' => 13,
);
$fields = array(
'docAlias' => $docAlias,
'pageTitle' => $resource->get('pagetitle'), /* not sure if this is necessary */
);
switch ($modx->event->name) {
case OnResourceUndelete:
$docContext = $resource->get('context_key');
if ($docContext=='web'){
$docAlias = $resource->get('alias');
$clone1 = $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context1'));
if($clone1){
$clone1->set('deleted',false);
$clone1->save();
}
$clone2 = $modx->getObject('modResource',array('alias' => $docAlias,'context_key' => 'context2'));
if($clone2){
$clone2->set('deleted',false);
$clone2->save();
}
break;
}
}
[ed. note: papachumba last edited this post 14 years, 3 months ago.]