<?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/>.
 */

require_once (LIB_PATH.'/common/sitebaseDB.php');
class Admin_admin_base_Index extends sitebaseDB
{
	/**
	 *	indexアクションの前処理
	 *
	 *	@access	public
	 *	@return	string		Forward先(正常終了ならnull)
	 */
	function prepare($view)
	{
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->setElements();
		$this->setButtons();
		$this->setRules();
		return null;
	}

	/**	
	 *	indexアクションの実装
	 *
	 *	@access	public
	 *	@return	string	遷移名
	 */
	function perform($view)
	{
		$req = $this->getRequest('buttons');
		if (isset($req['BTNnext'])&&$req['BTNnext']!=null) { //次へ進むボタンクリック時
			if($this->_form->validate()) { //エラーチェック
				$this->_form->freeze();
				$this->setButtons($this->_form->isFrozen()); //ボタンの作り直し
			}
		} else if(isset($req['BTNsave'])&&$req['BTNsave']!=null) { //保存ボタンクリック時
			if($this->_form->validate()){
				$post = $this->_form->getSubmitValues();
				if($this->doSave($post)) {
					return 'base/complete.tpl';
				} else {
					$this->_form->setElementError("","保存に失敗しました");
				}
			}
		} else {
			//何もクリックされていない場合は情報取得
			$this->_form->setDefaults($this->getData());
		}
		
		//登録エラー時
		if(!empty($this->_form->_errors)) {
			$view->assign('ERROR',$this->_form->_errors);
			//エラーエレメントの属性にclass="err"を追加
			foreach($this->_form->_errors as $key => $val) {
				$this->_form->updateElementAttr($key, array("class"=>"err"));
			}
		}
	
	
		// フォームパーツの描画
		$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($view);
		$this->_form->accept($renderer);
		$view->assign('form',$renderer->toArray());

        return 'base/index.tpl';
	}

}
?>