<?php
/**
 * @version $Id: AssetManager.class.php 679 2012-08-20 12:41:35Z hodaka $
 * @package myuploader
 */

if(!defined('XOOPS_ROOT_PATH'))
{
    exit;
}

/**
 * Myuploader_AssetManager
**/
class Myuploader_AssetManager
{
    /**
     * @var  string
     *
     * @public
    **/
    var $mDirname = '';

    /**
     * @var  string
     *
     * @public
    **/
    var $mTrustDirname = null;

    /**
     * @var  string[][][]
     *
     * @public
    **/
    var $mAssetList = array();

    /**
     * @var  object[][]
     *
     * @private
    **/
    var $_mCache = array();

    /**
     * Myuploader_AssetManager
     *
     * @param   string  $dirname
     *
     * @return  void
     *
     * @private
    **/
    function Myuploader_AssetManager($dirname)
    {
        $this->mDirname = $dirname;
        $this->mTrustDirname = $this->_getTrustDirname($dirname);
    }

    /**
     * &getInstance
     *
     * @param   string  $dirname
     *
     * @return  Myuploader_AssetManager
     *
     * @public
    **/
    function &getInstance($dirname)
    {
        /**
         *  @var    Myuploader_AssetManager[]
        **/
        static $instance = array();

        if(!isset($instance[$dirname]))
        {
            $instance[$dirname] = new Myuploader_AssetManager($dirname);
        }

        return $instance[$dirname];
    }

    /**
     * &getObject
     *
     * @param   string  $type
     * @param   string  $name
     * @param   bool  $isAdmin
     * @param   string  $mode
     *
     * @return  &object<XCube_ActionFilter,XCube_ActionForm,XoopsObjectGenericHandler>
     *
     * @public
    **/
    function &getObject($type, $name, $isAdmin=false, $mode=null)
    {
        if(isset($this->_mCache[$type][$name]))
        {
            return $this->_mCache[$type][$name];
        }

        $instance = null;

        $methodName = 'create'.ucfirst($name).ucfirst($mode).ucfirst($type);
        if(method_exists($this, $methodName))
        {
            $instance =& $this->$methodName();
        }

        if($instance === null)
        {
            $instance =& $this->_fallbackCreate($type, $name, $isAdmin, $mode);
        }

        $this->_mCache[$type][$name] =& $instance;

        return $instance;
    }

    /**
     * &_fallbackCreate
     *
     * @param   string  $type
     * @param   string  $name
     * @param   bool  $isAdmin
     * @param   string  $mode
     *
     * @return  &object<XCube_ActionFilter,XCube_ActionForm,XoopsObjectGenericHandler>
     *
     * @private
    **/
    function &_fallbackCreate($type, $name, $isAdmin=false, $mode=null)
    {
        $className = null;
        $instance = null;

        if(isset($this->mAssetList[$type][$name]) && isset($this->mAssetList[$type][$name]['class']))
        {
            $asset = $this->mAssetList[$type][$name];
            if(isset($asset['absPath']) && $this->_loadClassFile($asset['absPath'],$asset['class']))
            {
                $className = $asset['class'];
            }

            if($className == null && isset($asset['path']))
            {
                if($this->_loadClassFile($this->_getPublicPath().$asset['path'], $asset['class']))
                {
                    $className = $asset['class'];
                }

                if($className == null && $this->_loadClassFile($this->_getTrustPath().$asset['path'], $asset['class']))
                {
                    $className = $asset['class'];
                }
            }
        }

        if($className == null)
        {
            switch($type)
            {
                case 'filter':
                    $className = $this->_getFilterName($name, $isAdmin);
                    break;
                case 'form':
                    $className = $this->_getActionFormName($name, $isAdmin, $mode);
                    break;
                case 'handler':
                    $className = $this->_getHandlerName($name);
                    break;
                default:
                    return $instance;
            }
        }

        if($type == 'handler')
        {
            $root = XCube_Root::getSingleton();
            $instance = new $className($root->mController->getDB(), $this->mDirname);
        }
        else
        {
            $instance = new $className();
        }
        return $instance;
    }

    /**
     * _getFilterName
     *
     * @param   string  $name
     * @param   bool  $isAdmin
     *
     * @return  string
     *
     * @private
    **/
    function _getFilterName($name, $isAdmin=false)
    {
        $name = ucfirst($name).'FilterForm';
        $path = 'forms/'.$name.'.class.php';
        $className = ucfirst($this->mTrustDirname).($isAdmin ? '_Admin_' : '_').$name;
        return (
            $this->_loadClassFile($this->_getTrustPath($isAdmin).$path, $className) ||
            $this->_loadClassFile($this->_getPublicPath($isAdmin).$path, $className)
        ) ? $className : null;
    }

    /**
     * _getActionFormName
     *
     * @param   string  $name
     * @param   bool  $isAdmin
     * @param   string  $mode
     *
     * @return  string
     *
     * @private
    **/
    function _getActionFormName($name, $isAdmin=false, $mode=null)
    {
        $name = ucfirst($name).ucfirst($mode).'Form';
        $path = 'forms/'.$name.'.class.php';
        $className = ucfirst($this->mTrustDirname).($isAdmin ? '_Admin_' : '_').$name;
        return (
            $this->_loadClassFile($this->_getTrustPath($isAdmin).$path, $className) ||
            $this->_loadClassFile($this->_getPublicPath($isAdmin).$path, $className)
        ) ? $className : null;
    }

    /**
     * _getHandlerName
     *
     * @param   string  $name
     *
     * @return  string
     *
     * @private
    **/
    function _getHandlerName($name)
    {
        $path = 'class/handler/'.ucfirst($name).'.class.php';
        $className = ucfirst($this->mTrustDirname).'_'.ucfirst($name).'Handler';
        return (
            $this->_loadClassFile($this->_getTrustPath().$path, $className) ||
            $this->_loadClassFile($this->_getPublicPath().$path, $className)
        ) ? $className : null;
    }

    /**
     * _loadClassFile
     *
     * @param   string  $path
     * @param   string  $class
     *
     * @return  bool
     *
     * @private
    **/
    function _loadClassFile($path, $class)
    {
        if(!file_exists($path))
        {
            return false;
        }

        // TODO
        $mytrustdirname = $this->mTrustDirname;

        require_once $path;

        return class_exists($class);
    }

    /**
     * _getPublicPath
     *
     * @param   bool  $isAdmin
     *
     * @return  string
     *
     * @private
    **/
    function _getPublicPath($isAdmin=false)
    {
        return XOOPS_MODULE_PATH.'/'.$this->mDirname.($isAdmin ? '/admin/' : '/');
    }

    /**
     * _getTrustPath
     *
     * @param   bool  $isAdmin
     *
     * @return  string
     *
     * @private
    **/
    function _getTrustPath($isAdmin=false)
    {
        return XOOPS_TRUST_PATH.'/modules/'.$this->mTrustDirname.($isAdmin ? '/admin/' : '/');
    }

    function _getTrustDirname($dirname) {
        $file = XOOPS_ROOT_PATH.'/modules/'.$dirname.'/mytrustdirname.php';
        if(file_exists($file)) {
            require $file;
            return $mytrustdirname;
        }
        return null;
    }

}

?>