<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - Web Application Framework for PHP
 *
 * PHP version 4 (>= 4.3.x) or 5
 *
 * Copyright (C) 2006 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_Layout.php,v 1.1 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * 設定ファイルを読み込むクラス
 */
require_once SYL_INCLUDE_DIR . '/framework/SyL_Config.php';

/**
 * 画面構成要素クラス
 *
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_Layout.php,v 1.1 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_Layout
{
    /**
     * レイアウト設定ファイル名
     * 
     * @access private
     * @param string
     */
    var $layout_file_name = 'layouts.xml';
    /**
     * 設定ファイル配列
     * 
     * @access private
     * @var array
     */
    var $config = array();
    /**
     * 表示内容のデータ
     * 
     * @access protected
     * @var string
     */
    var $render = '';
    /**
     * データ管理オブジェクト
     * 
     * @access protected
     * @var object
     */
    var $dm = null;

    /**
     * コンストラクタ
     *
     * @access public
     * @param object データ管理オブジェクト
     */
    function SyL_Layout(&$dm)
    {
        $this->dm =& $dm;
    }

    /**
     * テンプレートオブジェクトを取得
     * 
     * @access public
     * @param object データ管理オブジェクト
     * @param object コンポーネントメソッドオブジェクト
     * @return object ビューオブジェクト
     */
    function &getObject(&$dm)
    {
        $view = null;
        $type = $dm->getViewParameter('viewtype');
        if ($dm->getViewParameter('viewlayout') === 'true') {
            // Layout クラス
            switch ($type) {
            case 'smarty' : $class_name = 'SyL_LayoutSmarty';  break;
            case 'default': $class_name = 'SyL_LayoutDefault'; break;
            default: trigger_error("[SyL error] Layout type not found ({$type})", E_USER_ERROR);
            }
            $class_file = SYL_INCLUDE_DIR . "/framework/Layout/{$class_name}.php";
            include_once $class_file;
            $view =& new $class_name($dm);
            $view->parseConfig($dm->controller);
        } else {
            switch ($type) {
            case 'customize':
                $class_name = $dm->getViewParameter('customizedviewclass');
                $class_file = $dm->getViewParameter('customizedviewfile');
                if (!is_file($class_file)) {
                    $class_file1 = SYL_WEBAPP_LIB_DIR . "/{$class_file}";
                    if (!is_file($class_file1)) {
                        trigger_error("[SyL error] Customised View file not found ({$class_file} or {$class_file1})", E_USER_ERROR);
                    }
                    $class_file = $class_file1;
                }
                break;
            case 'smarty':
                $class_name = 'SyL_LayoutViewSmarty';
                $class_file = SYL_INCLUDE_DIR . "/framework/Layout/{$class_name}.php";
                break;
            case 'default':
                $class_name = 'SyL_LayoutViewDefault';
                $class_file = SYL_INCLUDE_DIR . "/framework/Layout/{$class_name}.php";
                break;
            default:
                trigger_error("[SyL error] View type not found ({$type})", E_USER_ERROR);
            }

            include_once $class_file;
            $view =& new $class_name($dm);
        }

        return $view;
    }

    /**
     * XMLファイルの解析処理
     *
     * @access public
     */
    function parseConfig(&$controller)
    {
        $config_file = SYL_APP_DIR . "/{$this->layout_file_name}";
        $reader =& SyL_Config::getObject('layouts', $config_file, $controller);
        $reader->parseXML();
        $this->config += $reader->getConfig();
    }

    /**
     * テンプレートディレクトリを取得
     * 
     * @access public
     * @return string テンプレートディレクトリ
     */
    function getTemplateDir()
    {
        return $this->dm->getTemplateDir();
    }

    /**
     * テンプレートファイルを取得
     * 
     * @access public
     * @return string テンプレートファイル
     */
    function getTemplateFile()
    {
        if ($this->dm->getViewParameter('viewlayout') === 'true') {
            return $this->config['file'];
        } else {
            return $this->dm->getTemplateFile();
        }
    }

    /**
     * アクションパラメータを取得する
     * 
     * @access public
     * @return array ビューに渡すパラメータ
     */
    function &getActionValues()
    {
        $values = array();

        // レイアウト設定値
        if (isset($this->config['templates'])) {
            $values += $this->config['templates'];
        }
        if (isset($this->config['view'])) {
            foreach (array_keys($this->config['view']) as $template) {
                $values[$template] = $this->dm->getTemplateFile();
            }
        }
        if (isset($this->config['parameters'])) {
            $values += $this->config['parameters'];
        }

        // アクションの値など
        $values += $this->dm->getParameters();

        return $values;
    }

    /**
     * HTMLレンダリング実行
     * 
     * @access public
     */
    function render()
    {
    }

    /**
     * 表示内容をセット
     * 
     * @access public
     * @param string 表示内容
     */
    function setRender($render)
    {
        $this->render = $render;
    }

    /**
     * 表示内容を取得
     * 
     * @access public
     * @return string レンダリングHTML
     */
    function getRender()
    {
        return $this->render;
    }
}


?>
