<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class Controller {
    
    //アクション定義オブジェクト
    var $model = null;
    //Smartyオブジェクト
    var $view = null;
    var $view_path = null;
    
	function Controller()
	{
		$this->__construct();
	}
    function __construct() {
    
    }
    
	/**
     * Smartyの出力
	 * @author		Nobuo Nishino
	 * @param		$tpl_name		String	テンプレート名
	 */
    function output($tpl_name)
    {
        $this->view->display($tpl_name);
    }

	/**
     * アクションオブジェクトセット
	 * @param		$model_class_name	String	Modelクラス名
	 */
     function doAction($model_class_name)
     {
	 	//ファイルパス判別
		$model_fpath = "";
		$path_ar = array();
		$path_ar = split ("_", $model_class_name);
		
		for($i=0; $i<count($path_ar); $i++) {
			if($i==0) {
				$model_fpath = MODEL_PATH.'/';
			} else if($i==(count($path_ar)-1)){
				$model_fpath .= $path_ar[0].$path_ar[$i].'.php';
			} else {
				$model_fpath .= $path_ar[$i].'/';
			}
		}	 
        if (file_exists($model_fpath)) {
            include_once($model_fpath);
			$model_class_name = str_replace("-", "", $model_class_name);
            $this->model =& new $model_class_name;
            $tpl_name = $this->model->prepare(&$this->view);
			if($tpl_name == null) {
            	$tpl_name = $this->model->perform(&$this->view);
            }
			$this->output($tpl_name);
        } else {
            die('[ERROR] doAction : Not Found '.$model_fpath);
        }
     }
}
?>