<?php
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Load module layout before trying to load global layout
 */
class Fiktiv_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{

    protected $_default = 'default';

    public function  preDispatch(Zend_Controller_Request_Abstract $request) {

        $layout = Zend_Layout::getMvcInstance();
        
        $moduleName = strtolower($request->getModuleName());

        if (file_exists(APPLICATION_PATH . '/modules/' . $moduleName . '/views/layout/' . $this->_default . '.phtml')) {

            $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $moduleName . '/views/layout');
        } else {

            $layout->setLayoutPath(APPLICATION_PATH . '/modules/default/views/layout');
        }

        $layout->setLayout($this->_default);
        
    }
}