<?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/sectionIdxDB.php');
require_once (LIB_PATH.'/common/categoryDB.php');
require_once (LIB_PATH.'/common/pagecontainerDB.php');
require_once (LIB_PATH.'/common/unitDB.php');
require_once (LIB_PATH.'/common/pageunitDB.php');
require_once (LIB_PATH.'/common/pageView.php');
class Admin_admin_contents_page_Unitlayout extends pageunitDB
{
	function prepare($view)
	{
		$this->_mprev = $this->getRequest('mprev') ? true : false;
		$view->assign("mprev",$this->_mprev);
		$this->_sidx_code = $this->getRequest('sidx_code') ? $this->getRequest('sidx_code') : "";
		$this->_cat_code = $this->getRequest('cat_code') ? $this->getRequest('cat_code') : "";
		$this->_pidx_code = $this->getRequest('pidx_code') ? $this->getRequest('pidx_code') : "";
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->setElements();
		$this->setRules();
		$this->setButtons();

		if(is_numeric($this->_sidx_code)) {
			$this->_sidx_data = sectionIdxDB::getData($this->_sidx_code,$this->_mprev);
		}
		if(is_numeric($this->_cat_code)) {
			$this->_cat_data = categoryDB::getData($this->_cat_code,$this->_mprev);
		}
		if(is_numeric($this->_pidx_code)) {
			$this->_pidx_data = pageIdxDB::getTmpData($this->_pidx_code);
		}
		//ユニット一覧
		//$unitlist = unitDB::getlist($unit_type=array(2,3));
		//$view->assign('unitlist',$unitlist);
		if($this->getRequest("save")=="done"){
			$this->_form->setElementError("","保存しました");
		}
		return null;
	}

	function perform($view)
	{

		$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:unitlayout.php?save=done&pidx_code='.$this->_pidx_code.'&sidx_code='.$this->_sidx_code.'&cat_code='.$this->_cat_code.'&mprev='.$this->_mprev);
					exit();
				} else {
					$this->_form->setElementError("",$rtn);
				}
			}
		} else {
			//まずはセッションをクリア	
			unset($_SESSION["page_container"]); //ページ構成コンテナ
			unset($_SESSION["page_unit"]); //コンテナ構成ページ
			unset($_SESSION["new_page_unit_code"]); //ユニット新規追加code
			//DBから登録済みコンテナリストの取得
			$data = pagecontainerDB::getData($this->_pidx_code,$prev=true);
			//登録済みあった場合
			if(is_array($data) && count($data)>0) {
				//セッションへ格納
				foreach($data as $key=>$val) {
					$_SESSION["page_container"][$val["pagec_code"]] = $val;
				}
			}
			
			//登録済みユニット構成の取得
			$units = $this->getData($this->_pidx_code);
			if(is_array($units) && count($units)>0) {
				//セッションへ格納
				foreach($units as $key=>$val) {
					$_SESSION["page_unit"][$val["pageu_pagec_code"]][$val["pageu_column"]][$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"));
			}
		}
		
		$view->assign('items',$this->makeHTML($view));
		
		$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($view);
		$this->_form->accept($renderer);
		$view->assign('form',$renderer->toArray());
		$view->assign('sidx_data',$this->_sidx_data);
		$view->assign('cat_data',$this->_cat_data);
		$view->assign('pidx_code',$this->_pidx_code);
		$view->assign('pidx_data',$this->_pidx_data);
		$view->assign('sidx_code',$this->_sidx_code);
		$view->assign('cat_code',$this->_cat_code);
		
		//url変換
		$url = pageView::getSiteURL($d=$this->_sidx_data["sidx_dir"],$c=$this->_cat_data["cat_dir"],$p=$this->_pidx_data["pidx_url"],$this->_sidx_data["sidx_ssl"],$prev=true,$this->_mprev);
		$view->assign('previewurl',$url);
		
		
        return 'contents/page/unitlayout.tpl';
	}
}
?>
