<?php
/*
 * Copyright(c) 2000-2006 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 */
require_once("../../require.php");

//---- ページ表示クラス
class LC_Page {
	
	function LC_Page() {
		$this->tpl_mainpage = TEMPLATE_DIR . '/campaign/index.tpl';
	}
}

$objPage = new LC_Page();
$objView = new SC_SiteView(false);
$objQuery = new SC_Query();

// ディレクトリ名を取得
$dir_name = dirname($_SERVER['PHP_SELF']);
$arrDir = split('/', $dir_name);
$dir_name = $arrDir[count($arrDir) -1];

if(lfCheckLimitDate($dir_name)) {
	$status = CAMPAIGN_TEMPLATE_ACTIVE;
} else {
	$status = CAMPAIGN_TEMPLATE_END;
}

if($_GET['init'] != "") {
	$objPage->tpl_init = 'false';
	lfDispProductsList($_GET['ids']);
} else {
	$objPage->tpl_init = 'true';	
}

$objPage->tpl_dir_name = CAMPAIGN_TEMPLATE_PATH . $dir_name  . "/" . $status;

sfprintr($objPage);


//----　ページ表示
$objView->assignobj($objPage);
$objView->display($objPage->tpl_mainpage);


//---------------------------------------------------------------------------------------------------------------------------------------------------------
/* 
 * 関数名：lfCheckLimitDate()
 * 引数1 ：ディレクトリ名
 * 説明　：キャンペーン中かチェック
 * 戻り値：キャンペーン中なら true 終了なら false
 */
function lfCheckLimitDate($directory_name) {
	
	global $objQuery;
	
	$col = "start_date,end_date";
	$arrRet = $objQuery->select($col, "dtb_campaign", "directory_name = ? AND del_flg = 0", array($directory_name));

	// 開始日時・停止日時を成型
	$start_date = (date("YmdHis", strtotime($arrRet[0]['start_date'])));
	$end_date = (date("YmdHis", strtotime($arrRet[0]['end_date'])));
	$now_date = (date("YmdHis"));

	// キャンペーンが開催中かチェック
	if($now_date > $start_date && $now_date < $end_date) {
		return true;
	}
		
	return false;
}

/* 商品一覧の表示 */
function lfDispProductsList($ids) {
	
	global $objQuery;
	global $objPage;

	// 規格名一覧
	$arrClassName = sfGetIDValueList("dtb_class", "class_id", "name");
	// 規格分類名一覧
	$arrClassCatName = sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
	
	$arrProductIds = split('-', $ids);
	if(!is_array($arrProductIds)) {
		$arrProductIds[0] = $ids;
	}
	
	// where句生成
	$count = 0;
	$where = "product_id IN (";
	foreach($arrProductIds as $key =>$val) {
		if($count > 0) $where .= ",";
		$where .= "?";
		$arrval[] = $val;
		$count++;
	}
	$where .= ")";

	// 商品一覧
	$arrProducts = $objQuery->select("*", "vw_products_allclass AS allcls", $where, $arrval);

	for($i = 0; $i < count($arrProducts); $i++) {
		$objPage = lfMakeSelect($arrProducts[$i]['product_id'], $arrClassName, $arrClassCatName);
		// 購入制限数を取得
		$objPage = lfGetSaleLimit($arrProducts);
	}

	foreach($arrProductIds as $key =>$val) {
		$arrCamp[$val['product_id']] = $val[$val['product_id']];
	}
	
	$objPage->arrProducts = $arrCamp;
	
	return $objPage;
}

/* 規格セレクトボックスの作成 */
function lfMakeSelect($product_id, $arrClassName, $arrClassCatName) {
	global $objPage;
	
	$classcat_find1 = false;
	$classcat_find2 = false;
	// 在庫ありの商品の有無
	$stock_find = false;
	
	// 商品規格情報の取得	
	$arrProductsClass = lfGetProductsClass($product_id);
	
	// 規格1クラス名の取得
	$objPage->tpl_class_name1[$product_id] = $arrClassName[$arrProductsClass[0]['class_id1']];
	// 規格2クラス名の取得
	$objPage->tpl_class_name2[$product_id] = $arrClassName[$arrProductsClass[0]['class_id2']];
	
	// すべての組み合わせ数	
	$count = count($arrProductsClass);
	
	$classcat_id1 = "";
	
	$arrSele = array();
	$arrList = array();
	
	$list_id = 0;
	$arrList[0] = "\tlist". $product_id. "_0 = new Array('選択してください'";
	$arrVal[0] = "\tval". $product_id. "_0 = new Array(''";
	
	for ($i = 0; $i < $count; $i++) {
		// 在庫のチェック
		if($arrProductsClass[$i]['stock'] <= 0 && $arrProductsClass[$i]['stock_unlimited'] != '1') {
			continue;
		}
		
		$stock_find = true;
		
		// 規格1のセレクトボックス用
		if($classcat_id1 != $arrProductsClass[$i]['classcategory_id1']){
			$arrList[$list_id].=");\n";
			$arrVal[$list_id].=");\n";
			$classcat_id1 = $arrProductsClass[$i]['classcategory_id1'];
			$arrSele[$classcat_id1] = $arrClassCatName[$classcat_id1];
			$list_id++;
		}
		
		// 規格2のセレクトボックス用
		$classcat_id2 = $arrProductsClass[$i]['classcategory_id2'];
		
		// セレクトボックス表示値
		if($arrList[$list_id] == "") {
			$arrList[$list_id] = "\tlist". $product_id. "_". $list_id. " = new Array('選択してください', '". $arrClassCatName[$classcat_id2]. "'";
		} else {
			$arrList[$list_id].= ", '".$arrClassCatName[$classcat_id2]."'";
		}
		
		// セレクトボックスPOST値
		if($arrVal[$list_id] == "") {
			$arrVal[$list_id] = "\tval". $product_id. "_". $list_id. " = new Array('', '". $classcat_id2. "'";
		} else {
			$arrVal[$list_id].= ", '".$classcat_id2."'";
		}
	}	
	
	$arrList[$list_id].=");\n";
	$arrVal[$list_id].=");\n";
		
	// 規格1
	$objPage->arrClassCat1[$product_id] = $arrSele;
	
	$lists = "\tlists".$product_id. " = new Array(";
	$no = 0;
	foreach($arrList as $val) {
		$objPage->tpl_javascript.= $val;
		if ($no != 0) {
			$lists.= ",list". $product_id. "_". $no;
		} else {
			$lists.= "list". $product_id. "_". $no;
		}
		$no++;
	}
	$objPage->tpl_javascript.= $lists.");\n";
	
	$vals = "\tvals".$product_id. " = new Array(";
	$no = 0;
	foreach($arrVal as $val) {
		$objPage->tpl_javascript.= $val;
		if ($no != 0) {
			$vals.= ",val". $product_id. "_". $no;
		} else {
			$vals.= "val". $product_id. "_". $no;
		}
		$no++;
	}
	$objPage->tpl_javascript.= $vals.");\n";
	
	// 選択されている規格2ID
	$classcategory_id = "classcategory_id". $product_id;
	$objPage->tpl_onload .= "lnSetSelect('".$classcategory_id."_1','".$classcategory_id."_2','".$product_id."','".$_POST[$classcategory_id."_2"]."'); ";

	// 規格1が設定されている
	if($arrProductsClass[0]['classcategory_id1'] != '0') {
		$classcat_find1 = true;
	}
	
	// 規格2が設定されている
	if($arrProductsClass[0]['classcategory_id2'] != '0') {
		$classcat_find2 = true;
	}
		
	$objPage->tpl_classcat_find1[$product_id] = $classcat_find1;
	$objPage->tpl_classcat_find2[$product_id] = $classcat_find2;
	$objPage->tpl_stock_find[$product_id] = $stock_find;
		
	return $objPage;
}

/* 商品規格情報の取得 */
function lfGetProductsClass($product_id) {
	$arrRet = array();
	if(sfIsInt($product_id)) {
		// 商品規格取得
		$objQuery = new SC_Query();
		$col = "product_class_id, classcategory_id1, classcategory_id2, class_id1, class_id2, stock, stock_unlimited";
		$table = "vw_product_class AS prdcls";
		$where = "product_id = ?";
		$objQuery->setorder("rank1 DESC, rank2 DESC");
		$arrRet = $objQuery->select($col, $table, $where, array($product_id));
	}
	return $arrRet;
}

// 購入制限数の設定
function lfGetSaleLimit($product) {
	global $objPage;
	//在庫が無限または購入制限値が設定値より大きい場合
	if($product['sale_unlimited'] == 1 || $product['sale_limit'] > SALE_LIMIT_MAX) {
		$objPage->tpl_sale_limit[$product['product_id']] = SALE_LIMIT_MAX;
	} else {
		$objPage->tpl_sale_limit[$product['product_id']] = $product['sale_limit'];
	}
	
	return $objPage;
}

//支払方法の取得
//payment_id	1:代金引換　2:銀行振り込み　3:現金書留
function lfGetPayment() {
	$objQuery = new SC_Query;
	$col = "payment_id, rule, payment_method";
	$from = "dtb_payment";
	$where = "del_flg = 0";
	$order = "payment_id";
	$objQuery->setorder($order);
	$arrRet = $objQuery->select($col, $from, $where);
	return $arrRet;
}

?>