<?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/>.
 */

include_once("../../app_path.php");
include_once(APP_PATH.'/AdminController.php');
require_once (LIB_PATH.'/common/pagecontainerDB.php');
$obj = new AdminController();
$obj->model =new Model();
switch($obj->model->getRequest("mode")) {
	case "add":
		//追加するidを取得
		$pagec_type = $obj->model->getRequest("id");
		$torder = $obj->model->getRequest("order");
		$pagec_pidx_code = $obj->model->getRequest("pagec_pidx_code");
		//セッションの値を捜査してid,orderを取得する
		$pagec_code = 0;
		$pagec_order = 0;
		if(isset($_SESSION["page_container"]) && is_array($_SESSION["page_container"]) && count($_SESSION["page_container"])>0) {
			foreach($_SESSION["page_container"] as $key=>$val) {
				//新規登録用id=pagec_codeの最低値を取得
				if($val["pagec_code"]<$pagec_code) {
					$pagec_code = $val["pagec_code"];
				}
				//新規登録用order=pagec_orderの最大値
				if($val["pagec_order"]>$pagec_order) {
						$pagec_order = $val["pagec_order"];
				}
			}
			$pagec_code--;
			$pagec_order++;
		}
		//セッションにセット
		if(is_numeric($torder)&&$torder>=0) {
			$pagec_order = $torder;
		}
		$new = array("pagec_code"=>$pagec_code,
					"pagec_pidx_code"=>$pagec_pidx_code,
					"pagec_type"=>$pagec_type,
					"pagec_order"=>$pagec_order);
		if(isset($_SESSION["page_container"]) && is_array($_SESSION["page_container"]) && count($_SESSION["page_container"])>0) {
			foreach($_SESSION["page_container"] as $key => $val) {
				if($val["pagec_order"] >= $pagec_order) {
					$_SESSION["page_container"][$key]["pagec_order"] += 1;
				}
			}
		}
		$_SESSION["page_container"][$new["pagec_code"]] = $new;
		
		//セッション配列の並び替え
		foreach($_SESSION["page_container"] as $key=>$val) {
			$order_ar[$val["pagec_order"]] = $val;
		}
		ksort($order_ar);
		unset($_SESSION["page_container"]);
		foreach($order_ar as $key=>$val) {
			$_SESSION["page_container"][$val["pagec_code"]] = $val;
		}
		
		//セッションの値を元にhtmlを生成
		$pagecontainerDB = new pageContainerDB();
		echo $pagecontainerDB->ContainerAdminmakeHTML($obj->view,$_SESSION["page_container"], $_SESSION["page_unit"]);
		break;
	case "sort":
		$pagec_code = explode(",",$obj->model->getRequest("pagec_code"));
		//削除時にも実行されてしまう場合ありそのためほんとにセッションに居る物だけソートする
		//ほんとにいるcodeリスト作成
		foreach($_SESSION["page_container"] as $key=>$val) {
			$tmp[] = $val["pagec_code"];
		}
		$new = array();
		$cnt = 0;
		foreach($pagec_code as $key=>$val) {
			if(in_array($val, $tmp)) {
				$new[$val] = $_SESSION["page_container"][$val];
				$new[$val]["pagec_order"] = $cnt;
				$cnt++;
			}
		}
		$_SESSION["page_container"] = $new;
		break;
	case "delete":
		//削除できたら1,　削除不可なら０を出力
		//削除するエレメント名を取得id=addcunit_-2とか id=addcunit_24とかで来ます(0>=の場合は新規追加のコンテナ,0<の場合はDB保存済みのコンテナ)
		$delete_id = explode("_",$obj->model->getRequest("id"));
		if(is_numeric($delete_id[1])&&$delete_id[1]>0) {
			//コンテナ内にモジュール専用ユニットがないか確認
			$pagec_code = $delete_id[1]; //273
			$db = new DataBase();
			$sql = "select count(*) from tbl_extmodule
inner join tbl_unit on unit_extmodule_code = extmodule_code
inner join tbl_page_unit_tmp on pageu_unit_code = unit_code
where pageu_pagec_code = '{$pagec_code}' and unit_active = 1 and extmodule_active = 1 and unit_extmodule_private = 1";
			$privatemodulecount = $db->getOne($sql);
			if($privatemodulecount>0) {
				echo 0;
				exit();
			}
		}
		if($delete_id[1]!=null&&is_numeric($delete_id[1])){
			unset($_SESSION["page_container"][$delete_id[1]]);
		}
		//並び替え
		$cnt = 0;
		foreach($_SESSION["page_container"] as $key=>$val) {
			$_SESSION["page_container"][$key]["pagec_order"] = $cnt;
			$cnt++;
		}
		echo 1;
		break;
	default:
		break;
}
?>