pepebe
11/19/2014 - 9:06 AM

This plugin sets a new document's template automatically to the one it's siblings have or, if it has no siblings, to the one it's parent has

This plugin sets a new document's template automatically to the one it's siblings have or, if it has no siblings, to the one it's parent has.

<?php
/** 
    * This is a copy of "autotemplate" a MODx plugin written @Maarten.
    * On November, 19th 2014 it is still not available for revo 2.3.x, 
    * As it seems to be fully functional I post it here as "inheritTemplate" for future reference.
    * 
    * Usage: Create a new plugin "inheritTemplate" and trigger it on onDocFormRender
    * 
    * Changelog
    * 2014-11-26 - stop plugin if isset($_GET['template']) is true.
    * (had to add this line to stop the plugin from messing with autotemplate set by collections)
    * 
    * */

    /** 
    * inheritTemplate a.k.a "AutoTemplate" for MODx Revolution 
    * 
    * This plugin sets a new document's template automatically to the one it's siblings 
    * have or, if it has no siblings, to the one it's parent has. 
    * 
    * inheritTemplate is free software; you can redistribute it and/or modify it under 
    * the terms of the GNU General Public License as published by the Free Software
    * Foundation; either version 2 of the License, or (at your option) any later 
    * version. 
    * 
    * inheritTemplate is distributed in the hope that it will be useful, but WITHOUT 
    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
    * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
    * details. 
    * 
    * You should have received a copy of the GNU General Public License along with 
    * Inherit Template; if not, write to the Free Software Foundation, Inc., 59 Temple 
    * Place, Suite 330, Boston, MA 02111-1307 USA
    * 
    * @author      Maarten     <@maarten> 
    * @copyright   Copyrightu00a0(c)u00a02009,u00a0Magnatron 
    * @license     GPL v2 
    * 
    */
    

    if(isset($_GET['template'])){
        return;
    }
    
    if ($modx->event->name === 'OnDocFormRender') {
        // Only when new
        if (empty($scriptProperties['mode']) || $scriptProperties['mode'] !== 'new') {
            return;
        }
        // Siblings
        $c = $modx->newQuery('modResource', array('parent'=>$_REQUEST['parent']));
        $c->sortby('id', 'desc');
        // Reference last added sibling
        $siblings = $modx->getCollection('modResource', $c);
        if (count($siblings)>0) {
            $bro = array_shift($siblings);
            $modx->controller->setProperty('template', $bro->get('template'));
            return;
        }
        // No siblings, use parent
        if ((int)$_REQUEST['parent']!=0) {
            $dad = $modx->getObject('modResource', $_REQUEST['parent']);
            $modx->controller->setProperty('template', $dad->get('template'));
            return;
        }
    }
    return;