<?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');
require_once (LIB_PATH.'/common/sectionIdxDB.php');
class Admin_admin_template_Index extends templateDB
{
	function prepare($view)
	{
		return null;
	}
	
	function perform($view)
	{
		//設定済みテンプレート一覧取得
		$tpllist = $this->getList();
		
		$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"]."<br>\n";
					}
				}
				if(isset($val["sidx_tpl_code"])&&is_numeric($val["sidx_tpl_code"])) {
					$menu[$val["sidx_tpl_code"]] .= $val["sidx_title"]."<br>\n";
				}
			}
		}
		
		if(is_array($tpllist)) {
			foreach($tpllist as $key2=>$val2) {
				$tpllist[$key2]["thum"] = TEMPLATE_DIR."/".$val2["skin_path"]."/thum.jpg";
				//適用されているメニューの取得
				$tpllist[$key2]["menu"] = $menu[$val2["tpl_code"]];
				$tpllist[$key2]["menu_tmp"] = $menu_tmp[$val2["tpl_code"]];
			}
		}

		$view->assign('tpllist',$tpllist);
		$view->assign('TEMPLATE_DIR',TEMPLATE_DIR);
		
		//フォーム作成
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		
		$this->_form->addElement('hidden','tpl_code', $this->getrequest("tpl_code"));
		$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);
		
		//$buttons = array();
		//$buttons[] = $this->_form->createElement('submit','BTNcopy',' 現在のテンプレートを複製 ',array('id'=>'BTNcopy','class'=>'bBtnSkyL'));
		//$buttons[] = $this->_form->createElement('submit','BTNnew',' マスタから新規作成 ',array('id'=>'BTNnew','class'=>'bBtnSkyL'));
		//$buttons[] = $this->_form->createElement('submit','BTNsave',' 保存 ',array('id'=>'BTNsave','class'=>'bBtnBlueM'));
		//$this->_form->addGroup($buttons,"buttons", null, '&nbsp;&nbsp;');
		
		
		if ($this->getRequest('delete')!=null) {//削除
			$del_code = $this->getRequest('tpl_code');
			if(is_numeric($del_code)) {
				$rtn = $this->delete($del_code);
			}
			if($rtn===true) {
				header("location:index.php");
			} else if($rtn===false){
				$this->_form->setElementError("","削除に失敗しました");
			} else {
				$this->_form->setElementError("",$rtn);
			}
		} else if($this->getRequest('copy')!=null) {//複製
			$copy_code = $this->getRequest('tpl_code');
			if(is_numeric($copy_code)) {
				$rtn = $this->copy($copy_code);
			}
			if($rtn===true) {
				header("location:index.php?copyend=1");
			} else if($rtn===false){
				$this->_form->setElementError("","複製に失敗しました");
			} else {
				$this->_form->setElementError("",$rtn);
			}
		} else if($this->getRequest('copyend')==1) {//複製完了
			$this->_form->setElementError("","複製処理が完了しました");
		}
		//登録エラー時
		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/index.tpl';
	}
}
?>