<?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_Unitlistin extends Model
{
	var $db=null;
	var $_container = array();
	var $_unit = array();
	
	/**
	 * コンストラクタ php4対応・・・
	 */
	function Admin_admin_contents_page_Unitlist()
	{
		$this->__construct();
	}
	function __construct()
	{
		Model::__construct();
		$this->db = new DataBase();
	}
	
	
	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') : "";
		return null;
	}

	function perform($view)
	{

		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) {
				$this->_container[$val["pagec_code"]] = $val;
			}
		}
		//登録済みユニット構成の取得
		$units = pageunitDB::getData($this->_pidx_code);
		if(is_array($units) && count($units)>0) {
			//セッションへ格納
			foreach($units as $key=>$val) {
				$this->_unit[$val["pageu_pagec_code"]][$val["pageu_column"]][$val["pageu_code"]] = $val;
			}
		}
		$view->assign('items',$this->makeHTML($view,$this->_container, $this->_unit));
		
		/*********************/
		
		//一般ページの情報セット
		
		$pageView = new pageView();
		$pageView->_prev = 1;
		if(!$pageView->initPage(&$view)) {
			return '404.tpl';
		}
		$htmldata = $pageView->getPageData(&$view);
		$view->assign('preview', ($this->_prev) ? 1 : 0);
		$view->assign('UNITSCSS', $pageView->_allunitCSS.'<link href="../../css/imported_unitlist_in.css" rel="stylesheet" type="text/css" media="all" />');
		
		//サブナビゲーションの取得
		//$sub_menu = $pageView->getCategoryMenu();
		$view->assign('header',$pageView->_header);
		$view->assign('footer',$pageView->_footer);
		$view->assign('m_menu',$pageView->_gmenu);
		$view->assign('sub_menu',$pageView->_smenu);
		$view->assign('SIDE',$pageView->_sidebar);
		if($pageView->_skin_data["skin_path"]!=null) {
			$view->assign("CSS",$pageView->_skincss_path);
		}
		
		
		$view->assign('sidx_data',$pageView->_sidx_data);
		$view->assign('sidx_code',$this->_sidx_code);
		$view->assign('cat_data',$pageView->_cat_data);
		$view->assign('cat_code',$this->_cat_code);
		$view->assign('pidx_data',$pageView->_pidx_data);
		$view->assign('pidx_code',$this->_pidx_code);
		//$view->assign('PAGEC_IMGPATH',str_replace(DOCUMENT_ROOT_PATH,"",PAGEC_PATH));//サムネイル画像取得用
		$_SESSION["USERAREAWIDTH"]=isset($pageView->_skinsetting["USERAREAWIDTH"])?$pageView->_skinsetting["USERAREAWIDTH"]:800;
		
		$view->assign('WWW', WWW);
		$view->assign('WWW_SSL', WWW_SSL);
		$view->assign('SITENAME', SITENAME);
		$view->assign('ADMIN_MAIL', ADMIN_MAIL);
		//管理画面ではカウントさせない
		$view->assign('ANALYTICS', '');
		
		$view->assign("PAGENAVI",$pageView->getPagenavi());

		//url変換
		$url = pageView::getSiteURL($d=$pageView->_sidx_data["sidx_dir"],$c=$pageView->_cat_data["cat_dir"],$p=$pageView->_pidx_data["pidx_url"],$pageView->_sidx_data["sidx_ssl"],$prev=true,$this->_mprev);
		$view->assign('previewurl',$url);
		
		$html = $view->fetch("contents/page/unitlist-in.tpl")."\n";
        //return 'contents/page/layout-in.tpl';
		
		$view->assign('items',"".$html);
		return $pageView->_sidx_data["skin_path"];
        //return 'contents/page/unitlist-in.tpl';
	}

	
//コンテナ一覧、ユニット情報一覧からページを生成
	function makeHTML($view, $container, $unit) {
		$html=null;
		$pageunitDB = new pageunitDB();
		
		if(is_array($container)) {
			foreach($container as $key => $val) {
				$data = array();
				for($i=0;$i<pagecontainerDB::getMaxColumn($val["pagec_type"]);$i++) {
					$unithtml_ar = array();
					if(is_array($unit[$val["pagec_code"]][$i])) {
						$unithtml_ar =  $pageunitDB->makeUnitListHTML($view,$unit[$val["pagec_code"]][$i],$prev=true);
						foreach($unithtml_ar as $ukey => $uval) {
							/*
							$data[$i] .= "<div id='u".$ukey."' onmouseout=\"Element.hide('u".$ukey."-inner')\" onmouseover=\"Element.show('u".$ukey."-inner');\" style=\"display:block;\" >
							<div id='u".$ukey."-inner' style=\"position:absolute;display:none;text-align:center;z-index:10;\">
											<input type='button' class='bBtnUnitReedition' onclick=\"var imgs=getImageSize('u".$ukey."'); return GB_showCenter('ユニット編集','../../admin/contents/page/unitdetail.php?pageu_code=".$ukey."&imgs='+imgs,500,840,reloadParentOnClose)\" value='このユニットを編集する'>
							</div>
							".$uval."
							</div>";*/
$data[$i] .= "<div id='u".$ukey."' onmouseout=\"Element.hide('u".$ukey."-inner')\" onmouseover=\"Element.show('u".$ukey."-inner');\" style=\"display:block;\" >
<div id='u".$ukey."-inner' style=\"position:absolute;display:none;text-align:center;z-index:10;\">
<input type='button' class='bBtnUnitReedition' onclick=\"var imgs=getImageSize('u".$ukey."'); return parent.showBox('', '".BINGOTOPURL."/admin/contents/page/unitdetail.php?pageu_code=".$ukey."&imgs='+imgs, 840, 500)\" value='このユニットを編集する'>
</div>
".$uval."
</div>";
							
						}
					}
				}
				$view->assign("data",$data);
				$html.= $view->fetch(PAGEC_PATH.$val["pagec_type"].".tpl")."\n";
			}
		}
		return $html;
	}

}
?>
