pepebe
2/11/2015 - 7:36 PM

Inherit resource group of last sibling added or if no sibling is present of the current parent

Inherit resource group of last sibling added or if no sibling is present of the current parent

<?php
/*
    Inherit resource group of last sibling added 
    or if no sibling is present of the current parent
*/
$debug = false;
$msg = array();

switch ($modx->event->name) {
 
    case 'OnDocFormSave':

        if ($mode == 'new') {// resource created
        
            $msg['mode'] = $mode;
            
            $msg['id'] = $resource->get('id');

            if($resource->get('parent')){
                
                // Parent
                $parentId = $resource->get('parent');
                
                $msg['parent'] = $parentId;
                
                // Siblings
                $c = $modx->newQuery('modResource', array('parent'=> $parentId  ));
                $c->sortby('menuindex', 'desc');
                
                $siblings = $modx->getCollection('modResource', $c);
                if (count($siblings)>0) {
                    
                    end($siblings);
                    prev($siblings);
                    
                    $sibling = prev($siblings);
                    $inheritId = $sibling->get('id');
                }

                
                // No siblings, use parent
                else {
                    $inheritId = $parentId;
                }

                $msg['inheritId'] = $inheritId;
    
                if($inheritId !== false) {

                    $childId = $resource->get('id');
                    $childResourceGroupCollection = $modx->getCollection('modResourceGroupResource', array('document' => $childId));
                    // Build an array of resource group IDs for this resource
                    $childResourceGroups = array();
                    foreach($childResourceGroupCollection as $group) {
                        $childResourceGroups[] = $group->get('document_group');
                    }
                    
                    $msg['childResourceGroups'] = $childResourcceGroups;
                    
                    $resourceGroups = $modx->getCollection('modResourceGroupResource',array('document' => $inheritId));
                   
                    $msg['resourceGroups'] = $resourceGroups; 
                   
                    if ($resourceGroups) {
                    // For each of the inherited resource groups, get the group ID and join the 
                    // new resource to that group.
                        foreach($resourceGroups as $group) {
                            $groupId = $group->get('document_group');
                            // If the resource is not already in the RG, join the group.
                            if ( ! in_array($groupId, $childResourceGroups)) {
                                $resource->joinGroup($groupId);
                            }
                        }
                    }
                    
                    
                }
            }
        }
        else {
           // existing resource was updated mod=='upd'
        }
        
    break;
}
if($debug){
    $modx->log(modX::LOG_LEVEL_ERROR, '[inheritResourceGroup]' . print_r($msg,true));
}