<?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/sectionIdxDB.php');
class Admin_admin_template_Edit extends templateDB
{
	var $_tpl_code;
	
	function prepare($view)
	{
		$this->_tpl_code = $this->getrequest("tpl_code");
		//フォーム作成
		$this->_form = new HTML_QuickForm('form','get',$_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->addElement('hidden','tpl_code', $this->_tpl_code);
		$this->_form->addElement('text','tpl_name', null,array('size'=>20,'maxlength'=>'20','id'=>'tpl_name','style'=>'width:100%'));
		$item = array();
		$item[]=&HTML_QuickForm::createElement('radio', null, null, '表示', 1, array('onclick'=>'setPageNavi(1);'));
		$item[]=&HTML_QuickForm::createElement('radio', null, null, '非表示', 0, array('onclick'=>'setPageNavi(0);'));
		$this->_form->addGroup($item,'tpl_pagenavi',null,'&nbsp;&nbsp;&nbsp;');
		$this->_form->addElement('textarea','tpl_summary', null,array('id'=>'tpl_summary','style'=>'width:100%','rows'=>5));
		
		$this->_form->applyFilter('__ALL__','trim');
		$this->_form->_submitValues = DataBase::ConvColumnVals($this->_form->_submitValues,$this->columns); //ルールに従った値変換、タグの除去
		$this->_form->setJsWarnings('下記の項目が不正です。','');

		$this->_form->addRule('tpl_code','・「コード」が不明です。　テンプレート設定トップよりやり直してください','required',null,$this->erchk);
		$this->_form->addRule('tpl_name','・「テンプレート名称」を入力してください。','required',null,$this->erchk);
		
		$buttons = array();
		$buttons[] = $this->_form->createElement('submit','BTNback',' 一覧へ戻る ',array('id'=>'BTNback','class'=>'bBtnListBack'));
		$buttons[] = $this->_form->createElement('submit','BTNsave',' 変更 ',array('id'=>'BTNsave','class'=>'bBtnSave'));
		$this->_form->addGroup($buttons,"buttons", null, '&nbsp;&nbsp;');
		
		return null;
	}
	
	function perform($view)
	{
		$gmenulist = sectionIdxDB::getlistAll();
		//取得したグローバルメニューをtpl_code別に並び替え
		$menu=array();
		$menu_tmp=array();
		if(is_array($gmenulist)) {
			foreach($gmenulist as $key=>$val) {
				if(isset($val["sidx_tpl_code_tmp"])&&is_numeric($val["sidx_tpl_code_tmp"])) {
					if($val["sidx_view_tmp"]!=2) {
						$menu_tmp[$val["sidx_tpl_code_tmp"]][] .= $val["sidx_title_tmp"];
					}
				}
				if(isset($val["sidx_tpl_code"])&&is_numeric($val["sidx_tpl_code"])) {
					$menu[$val["sidx_tpl_code"]][] = $val["sidx_title"];
				}
			}
		}
		$view->assign("menulist",$menu[$this->_tpl_code]);
		$view->assign("tmpmenulist",$menu_tmp[$this->_tpl_code]);
		if($this->getRequest('save')==1) {
			$this->_form->setElementError("","保存しました。メニュー管理にてヘッダー、フッター等の登録内容を確認ください。");
		}
		$req = $this->getRequest('buttons');
		if (isset($req['BTNback'])&& $req['BTNback']!=null) {	//戻る
			header('location:index.php');
			exit();	
		} elseif (isset($req['BTNsave'])&&$req['BTNsave']!=null) { //保存
			if($this->_form->validate())
			{
				$post = $this->_form->getSubmitValues();
				if($this->update($this->_tpl_code,$post)) {
					//$this->_form->setElementError("","保存しました。メニュー管理にてヘッダー、フッター等の登録内容を確認ください。");
					header("location:edit.php?save=1&tpl_code=".$this->_tpl_code);
				} else {
					$this->_form->setElementError("","保存に失敗しました");
				}
			}
		} else {
			$this->_form->setDefaults($this->getData($this->_tpl_code));
		}
		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());	
		
		$view->assign("tpl_code",$this->_tpl_code);
		
        return 'template/edit.tpl';
	}
}
?>