<?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/templateDB.php');
require_once (LIB_PATH.'/common/skinDB.php');
class Admin_admin_template_New extends templateDB
{
	function prepare($view)
	{

		return null;
	}
	
	function perform($view)
	{
		//フォーム作成
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->_form->addElement('hidden','token', null);
		$this->_form->registerRule('check_token', 'callback','check_token');
		$this->_form->addRule('token','・不正なページ遷移の為処理を停止しました。管理トップよりやり直してください','check_token',null,$this->erchk);
		$this->_form->addRule('token','・不正なページ遷移の為処理を停止しました。管理トップよりやり直してください','required',null,$this->erchk);

		$this->_form->setDefaults(array('tpl_pagenavi'=>1));
		$this->_form->addElement('hidden','tpl_code', $this->_tpl_code);
		$this->_form->addElement('text','tpl_name', null,array('size'=>20,'maxlength'=>'20','id'=>'tpl_name','style'=>'width:190px'));
		$this->_form->addElement('textarea','tpl_summary', null,array('id'=>'tpl_summary','style'=>'width:190px','rows'=>5));
		$item[]=&HTML_QuickForm::createElement('radio', null, null, '表示', 1);
		$item[]=&HTML_QuickForm::createElement('radio', null, null, '非表示', 0);
		$this->_form->addGroup($item,'tpl_pagenavi',null,'&nbsp;&nbsp;&nbsp;');
		//テンプレートマスタより「利用する」となっているテンプレート一覧を取得
		$skinlist = skinDB::getList($skin_use = 1);
		$item=array();
		if(is_array($skinlist)) {
			foreach($skinlist as $key=>$val) {
				$skinlist[$key]["thum"] = TEMPLATE_DIR."/".$val["skin_path"]."/thum.jpg";
				$item[]=&HTML_QuickForm::createElement('radio', null, null, $val["skin_name"], $val["skin_code"]);
			}
			$this->_form->addGroup($item,'tpl_skin_code', null, '<br>');
		} else {
			$this->_form->addElement('hidden','tpl_skin_code', null);
		}
		$view->assign("skinlist",$skinlist);
		$view->assign('TEMPLATE_DIR',TEMPLATE_DIR);
		
		$this->_form->applyFilter('__ALL__','trim');
		$this->_form->_submitValues = DataBase::ConvColumnVals($this->_form->_submitValues,$this->columns); //ルールに従った値変換、タグの除去
		$this->_form->setJsWarnings('下記の項目が不正です。','');

		$this->_form->addRule('tpl_name','・「テンプレート名称」を入力してください。','required',null,$this->erchk);
		$this->_form->addRule('tpl_skin_code','・デザインテンプレートを選択してください。','required',null,$this->erchk);
		
		$buttons = array();
		$buttons[] = $this->_form->createElement('submit','BTNlist',' 一覧に戻る ',array('id'=>'BTNlist','class'=>'bBtnListBack'));
		$buttons[] = $this->_form->createElement('submit','BTNsave',' 保存 ',array('id'=>'BTNsave','class'=>'bBtnSave'));
		$this->_form->addGroup($buttons,"buttons", null, '&nbsp;&nbsp;');

		$req = $this->getRequest('buttons');
		if (isset($req['BTNlist'])&& $req['BTNlist']!=null) {	//キャンセル
			header('location:index.php');
			exit();	
		} elseif (isset($req['BTNsave'])&&$req['BTNsave']!=null) { //保存
			if($this->_form->validate())
			{
				$post = $this->_form->getSubmitValues();
				if($this->insert($post)) {
					header("location:index.php");
				} else {
					$this->_form->setElementError("","保存に失敗しました");
				}
			}
		}
		if(empty($this->_form->_submitValues)) {
			$this->_form->setDefaults(array("token"=>$this->getToken()));
		}
		//登録エラー時
		if(!empty($this->_form->_errors)) {
			$view->assign('ERROR',$this->_form->_errors);
			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 'template/new.tpl';
	}
}
?>