/*
 * Inherit Parent Template
 *
 * Written By Raymond Irving - 12 Oct 2006
 *
 * Simply results in new documents inheriting the template 
 * of their parent folder upon creating a new document
 *
 * Configuration:
 * check the OnDocFormPrerender event
 *
 * Version 1.1
 *
 */

global $content;
$e = &$modx->Event;

switch($e->name) {
    case 'OnDocFormPrerender':        
        if ($inheritTemplate == 'From First Sibling') {
            if ($_REQUEST['pid'] > 0 && $id == 0) {
                if ($sibl = $modx->getDocumentChildren($_REQUEST['pid'], 1, 0, 'template', '', 'menuindex', 'ASC', 1)) {
                    $content['template'] = $sibl[0]['template'];
                } else if ($sibl = $modx->getDocumentChildren($_REQUEST['pid'], 0, 0, 'template', '', 'menuindex', 'ASC', 1)) {
                    $content['template'] = $sibl[0]['template'];
                } else if ($parent = $modx->getPageInfo($_REQUEST['pid'], 0, 'template')) {
                    $content['template'] = $parent['template'];
                }
            }
        } else {
             if ($parent = $modx->getPageInfo($_REQUEST['pid'],0,'template')) {
                 $content['template'] = $parent['template'];
             }
        }
        break;
    default:
        break;
}