pepebe
4/14/2014 - 2:12 PM

MODX OnElementNotFound plugin demo

MODX OnElementNotFound plugin demo

<?php
/**
 * MODX Revolution OnElementNotFound plugin
 * Add static elements inside categories for defined packages OnElementNotFound
 *
 * Author: Jeroen Kenters / www.kenters.com
 * Version: 1.0.0-beta
 * License: GPL
 *
 * Warning:
 * This code is provided as is without any warranty! Use at your own risk!
 * Always make backups!
 *
 * Installation:
 * - Create a new plugin using the contents of this file
 * - Change the package names + package paths in $packages to reflect your package(s)
 * - Make sure to select the OnElementNotFound event on the 'System Events' tab
 * - Save the plugin
 * - add your chunks and snippets to your content
 * - View a resource using those elements (frond end that is)
 * - The elements on that resource should be rendered and created (inside the correct categories)
 * - Enjoy all the time this saves you :-)
 *
 * Limitations:
 * - Only 1 sub category below the package category is supported
 * - MODX does not allow to use the same category name twice, so use unique names
 * - This is still a beta version. It should work ok, but has not been thoroughly tested
 *
 * How to name your folders/files (inside core/components):
 * [mypackage]
 *     [elements]
 *         [chunks]
 *             chunkname.chunk.tpl
 *             [CategoryA]
 *                 chunkinsidecategory.chunk.tpl
 *         [snippets]
 *             mycode.snippet.php
 *             [CategoryA]
 *                 snippetinsidecategory.snippet.php
 * [anotherpackage]
 *     [elements]
 *         [chunks]
 *             [CategoryA] <- this won't work; already used for "mypackage"
 */

// Define package names (=category name) + package core folders below
$packages = array(
    'MyPackage' => $modx->getOption('core_path').'components/mypackage/',
    'AnotherPackage' => $modx->getOption('core_path').'components/anotherpackage/'
);

// No changes below this line

foreach ($packages as $package => $pkgPath) {
    //get or create package category
    $cat = $modx->getObject('modCategory', array('category' => $package, 'parent' => 0));
    if(!is_object($cat)) {
        $cat = $modx->newObject('modCategory');
        $cat->set('category', $package);
        $cat->save();
    }

    //search for chunk in package directory
    if($class == 'modChunk') {
        $searchFolder = $pkgPath.'elements/chunks/';
        $searchFile = strtolower($name).'.chunk.tpl';
        $foundFile = false;
        $foundCategory = false;

        if(file_exists($searchFolder.$searchFile)) {
            $foundFile = $searchFile;
            $foundCategory = $cat->get('id');
        }
        else {
            //not in main chunks folder, so search in category directories
            $catFolders = glob($searchFolder.'*', GLOB_ONLYDIR);
            foreach ($catFolders as $catFolder) {
                if(file_exists($catFolder.'/'.$searchFile)) {
                    $catFolderName = end(explode('/', $catFolder));
                    $foundFile = $catFolderName.'/'.$searchFile;

                    //get or create category for this subcategory
                    $subcat = $modx->getObject('modCategory', array('parent' => $cat->get('id'), 'category' => $catFolderName));
                    if(!is_object($subcat)) {
                        $subcat = $modx->newObject('modCategory');
                        $subcat->set('parent', $cat->get('id'));
                        $subcat->set('category', $catFolderName);
                        $subcat->save();
                    }
                    $foundCategory = $subcat->get('id');
                    break; //we found the chunk, so no need to search any further
                }
            }
        }

        //create chunk if we found one
        if($foundFile && $foundCategory) {
            $chunk = $modx->newObject('modChunk');
            $chunk->set('name', $name);
            $chunk->set('category', $foundCategory); //pkg category id
            $chunk->set('source', 0); //media source id
            $chunk->set('static', 1); //create chunk as static file
            $chunk->set('static_file', strtolower($package).'/elements/chunks/'.$foundFile);
            $chunk->set('snippet', file_get_contents($modx->getOption('core_path').'components/'.strtolower($package).'/elements/chunks/'.$foundFile));
            $chunk->save();
            $modx->event->_output = $chunk;
            break;
        }
    }

    //search for snippet in package directory
    elseif($class == 'modSnippet') {
        $searchFolder = $pkgPath.'elements/snippets/';
        $searchFile = strtolower($name).'.snippet.php';
        $foundFile = false;
        $foundCategory = false;

        if(file_exists($searchFolder.$searchFile)) {
            $foundFile = $searchFile;
            $foundCategory = $cat->get('id');
        }
        else {
            //not in main snippets folder, so search in category directories
            $catFolders = glob($searchFolder.'*', GLOB_ONLYDIR);
            foreach ($catFolders as $catFolder) {
                if(file_exists($catFolder.'/'.$searchFile)) {
                    $catFolderName = end(explode('/', $catFolder));
                    $foundFile = $catFolderName.'/'.$searchFile;

                    //get or create category for this subcategory
                    $subcat = $modx->getObject('modCategory', array('parent' => $cat->get('id'), 'category' => $catFolderName));
                    if(!is_object($subcat)) {
                        $subcat = $modx->newObject('modCategory');
                        $subcat->set('parent', $cat->get('id'));
                        $subcat->set('category', $catFolderName);
                        $subcat->save();
                    }
                    $foundCategory = $subcat->get('id');
                    break; //we found the snippet, so no need to search any further
                }
            }
        }

        //create snippet if we found one
        if($foundFile && $foundCategory) {
            $snippet = $modx->newObject('modSnippet');
            $snippet->set('name', $name);
            $snippet->set('category', $foundCategory); //pkg category id
            $snippet->set('source', 0); //media source id
            $snippet->set('static', 1); //create chunk as static file
            $snippet->set('static_file', strtolower($package).'/elements/snippets/'.$foundFile);
            $snippet->set('snippet', file_get_contents($modx->getOption('core_path').'components/'.strtolower($package).'/elements/snippets/'.$foundFile));
            $snippet->save();
            $modx->event->_output = $snippet;
            break;
        }
    }
}