<?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_Footer extends templateDB
{
	var $_tpl_code;
	function prepare($view)
	{
		$this->_tpl_code = $this->getrequest("tpl_code");
		if(is_numeric($this->_tpl_code)) {
			$this->_template_data = $this->getData($this->_tpl_code);
		}
		//デザインテンプレートデータ
		$skinDB = new skinDB();
		if(is_array($this->_template_data)) {
			foreach($this->_template_data as $key => $val) {
				if(ereg('^skin_', $key)) {
					$this->_skin_data[$key] = $val;
				}
			}
		}
		$this->_sidx_data["skin_path"] = skinDB::getSkinPath($this->_skin_data); //デザインテンプレートパス
		$this->_hsetting = $skinDB->getSetting($this->_skin_data["skin_path"]);
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->setElementsFooter();
		$this->setRulesFooter();
		$this->setButtons();
		return null;
	}
	
	function perform($view)
	{
		$req = $this->getRequest('buttons');
		if(isset($req['BTNsave'])&&$req['BTNsave']!=null) {
			if($this->_form->validate()){
				$post = $this->_form->getSubmitValues();
				$rtn = $this->doSave($post);
				if($rtn==null) {
					$this->clrToken();
					$js =  '<script language="javascript">parent.closeBox();</script>';
					$view->assign('closeJS',$js);
				} else {
					$this->_form->setElementError("",$rtn);
				}
			}
		} else {
			if(is_array($this->_template_data)) {
				$record = $this->fsavnavi_explode($this->_template_data["tpl_fsubnavi"]);
				foreach($this->_template_data as $snavikey=>$snavival) {
					$record[$snavikey] = $snavival;
				}
			}
			$this->_form->setDefaults($record);
		}
		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('skin_data',$this->_skin_data);
		$view->assign('hsetting',$this->_hsetting);
		$view->assign('TEMPLATE_DIR',TEMPLATE_DIR);
        return 'template/footer.tpl';
	}
	
	
	function doSave($post)
	{
		$result = null;
		foreach($this->columns as $val) {
			if(isset($post[$val["column"]])){
				if($post[$val["column"]]!=null) {
					$record[$val["column"]] = $post[$val["column"]];
				} else {
					$record[$val["column"]] = null;
				}
			}
		}
		$record["tpl_fsubnavi"] = $this->fsavnavi_implode($post);
		$this->db->query('BEGIN');
		if(!$this->db->update("tbl_template",$record, "tpl_code=".$record["tpl_code"])) {
			$this->db->query('ROLLBACK');
			return "・データの保存に失敗しました";
		}
		$this->db->query('COMMIT');
		return null;
	}
}
?>