<?php
/**
 * Front controller plugin for blog admin functionality.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Front controller plugin for blog admin functionality.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class Blog_Plugin_Admin extends Zend_Controller_Plugin_Abstract
{
    /** Hook is executed after router recognizes current route.
     * - Prefixes all controllers in the 'blogAdmin' route with 'admin_' prefix
     *  because we want use Admin subdirectory.
     * - Sets blog admin action helper for all 'admin_' controllers in the blog
     * module.
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        /* @var $bootstrap Zend_Filter_Alnum */
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        /* @var $appLog Zend_Log */
        $appLog = $bootstrap->getResource("log");

        $currentRouteName = Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName();

        $appLog->info("current matched route name: $currentRouteName");

        //modify controller name for some routes
        if ($currentRouteName == 'blogAdmin' || $currentRouteName == 'blogAdminItemAction') {
            //if there isn't admin_ prefix in the controller name, add it
            if (strpos($request->getControllerName(),"admin_") !== 0) {
                $request->setControllerName('admin_'.$request->getControllerName());
            }
        }

        //for all blog admin controllers, add blog admin helper plugin, we must be
        //careful about it, because helper uses init hook, when we would add the helper
        //globally, init hook will be called for all controllers, this would be a bad
        //behaviour
        if ($request->getModuleName()=='blog' &&
            strpos($request->getControllerName(),"admin_") === 0) {
            Zend_Controller_Action_HelperBroker::addHelper(new Blog_Controller_Helper_Admin());
        }
    }
}
