<?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/sidebarDB.php');
require_once (LIB_PATH.'/common/templateDB.php');
class Admin_admin_template_Sidelayout extends sidebarDB
{
	var $_tpl_code;
	function prepare($view)
	{
		$this->_tpl_code = $this->getrequest("tpl_code");
		$tpldata = templateDB::getData($this->_tpl_code);
		$view->assign("tpldata",$tpldata);
		return null;
	}
	
	function perform($view)
	{
		
		$view->assign("tpl_code",$this->_tpl_code);
		
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		if($this->getRequest("save")=="done"){
			$this->_form->setElementError("","保存しました");
		}
		//フォームエレメントの作成
		$this->_form->addElement('hidden','sdbr_tpl_code', $this->_tpl_code, array("id"=>"sdbr_tpl_code"));
		$this->_form->addElement('hidden','tpl_code', $this->_tpl_code, array("id"=>"tpl_code"));
		
		//フォーム入力エラーチェックの設定
		$this->_form->applyFilter('__ALL__','trim');
		$this->_form->_submitValues = DataBase::ConvColumnVals($this->_form->_submitValues,$this->columns); //ルールに従った値変換、タグの除去
		$this->_form->setJsWarnings('下記の項目が不正です。','');		
		$this->_form->addRule('sdbr_tpl_code','・「テンプレートコード」が不明です。テンプレート設定よりやり直してください','required',null,$this->erchk);
		//$this->setButtons();

		$req = $this->getRequest('buttons');
		if(isset($req['BTNsave'])&&$req['BTNsave']!=null) {
			if($this->_form->validate())
			{
				$rtn = $this->doSave();//DBの内容消去//セッションの内容をDBへ保存
				if($rtn==null) {
					header('location:sidelayout.php?tpl_code='.$this->_tpl_code.'&save=done');
					exit();
				} else {
					$this->_form->setElementError("",$rtn);
				}
			}
		} else {
			//まずはセッションをクリア	
			unset($_SESSION["sidebar_unit"]);
			unset($_SESSION["new_side_unit_code"]);
			//登録済みユニット構成の取得
			$units = $this->getData($this->_tpl_code);
			if(is_array($units) && count($units)>0) {
				foreach($units as $key=>$val) {
					$_SESSION["sidebar_unit"][$val["pageu_code"]] = $val;
				}
			}
		}
		//登録エラー時
		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/sidelayout.tpl';
	}
}
?>