<?php
/*
 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 */
require_once("../require.php");
require_once("./inc_mailmagazine.php");

if(file_exists(MODULE_PATH . 'mdl_combz/mdl_combz.inc')) {
	require_once(MODULE_PATH . 'mdl_combz/mdl_combz.inc');
}

class LC_Page {
	var $arrSession;
	var $arrHtmlmail;
	var $arrNowDate;
	function LC_Page() {
		$this->tpl_mainpage = 'mail/index.tpl';
		$this->tpl_mainno = 'mail';
		$this->tpl_subnavi = 'mail/subnavi.tpl';
		$this->tpl_subno = "index";
		$this->tpl_pager = DATA_PATH . 'Smarty/templates/admin/pager.tpl';
		$this->tpl_subtitle = '配信内容設定';
		
		global $arrPref;
		$this->arrPref = $arrPref;
		global $arrJob;
		$arrJob["不明"] = "不明";
		$this->arrJob = $arrJob;
		global $arrSex;		
		$this->arrSex = $arrSex;
		global $arrMailType;
		$this->arrMailType = $arrMailType;
		global $arrDomain;
		$this->arrDomain = $arrDomain;
		global $arrPageRows;
		$this->arrPageRows = $arrPageRows;
		// ページナビ用
		$this->tpl_pageno = $_POST['search_pageno'];
		global $arrMAILMAGATYPE;
		$this->arrMAILMAGATYPE = $arrMAILMAGATYPE;
		$this->arrHtmlmail[''] = "すべて";
		$this->arrHtmlmail[1] = $arrMAILMAGATYPE[1];
		$this->arrHtmlmail[2] = $arrMAILMAGATYPE[2];
		global $arrCustomerType;
		$this->arrCustomerType = $arrCustomerType;
		global $arrDOMAIN;
		$this->arrDomain = $arrDOMAIN;
		$this->arrDomain[''] = "指定しない";
		$this->arrDomain[1] = $arrDOMAIN[1];
		$this->arrDomain[2] = $arrDOMAIN[2]; 
	}
}

class LC_HTMLtemplate {
	var $list_data;
}

//---- ページ初期設定
$conn = new SC_DBConn();
$objPage = new LC_Page();
$objView = new SC_AdminView();
$objDate = new SC_Date();
$objQuery = new SC_Query();
$objPage->objDate = $objDate;
$objPage->arrTemplate = getTemplateList($conn);

$objSess = new SC_Session();

// 認証可否の判定
sfIsSuccess($objSess);

/*
	query:配信履歴「確認」
*/
if ($_GET["mode"] == "query" && sfCheckNumLength($_GET["send_id"])) {
	// 送信履歴より、送信条件確認画面
	$sql = "SELECT search_data FROM dtb_send_history WHERE send_id = ?";
	$result = $conn->getOne($sql, array($_GET["send_id"]));
	$tpl_path = "mail/query.tpl";
		
	$list_data = unserialize($result);
	
	// 都道府県を変換
	$list_data['pref_disp'] = $objPage->arrPref[$list_data['pref']];
	
	//ドメイン指定を変換
	$list_data['domain_disp'] = $objPage->arrDomain[$list_data['domain']];
	
	// 配信形式
	$list_data['htmlmail_disp'] = $objPage->arrHtmlmail[$list_data['htmlmail']];
	
	// 性別の変換
	if (count($list_data['sex']) > 0) {
		foreach($list_data['sex'] as $key => $val){
			$list_data['sex'][$key] = $objPage->arrSex[$val];
			$sex_disp .= $list_data['sex'][$key] . " ";
		}
		$list_data['sex_disp'] = $sex_disp;
	}
	
	// 職業の変換
	if (count($list_data['job']) > 0) {
		foreach($list_data['job'] as $key => $val){
			$list_data['job'][$key] = $objPage->arrJob[$val];
			$job_disp .= $list_data['job'][$key] . " ";
		}
		$list_data['job_disp'] = $job_disp;
	}
		
	// カテゴリ変換
	$arrCatList = sfGetCategoryList();
	$list_data['category_name'] = $arrCatList[$list_data['category_id']];
	
	$objPage->list_data = $list_data;

	$objView->assignobj($objPage);
	$objView->display($tpl_path);
	exit;
}

if($_POST['mode'] == 'delete') {
}

switch($_POST['mode']) {
/*
	search:「検索」ボタン
	back:検索結果画面「戻る」ボタン
*/
case 'delete':
case 'search':
case 'back':
// コンビーズ連携用
case 'combz':
	//-- 入力値コンバート
	$objPage->list_data = lfConvertParam($_POST, $arrSearchColumn);
		
	//-- 入力エラーのチェック
	$objPage->arrErr = lfErrorCheck($objPage->list_data);

	//-- 検索開始
	if (!is_array($objPage->arrErr)) {
		$objPage->list_data['name'] = sfManualEscape($objPage->list_data['name']);
		// hidden要素作成
		$objPage->arrHidden = lfGetHidden($objPage->list_data);

		//-- 検索データ取得	
		$objSelect = new SC_CustomerList($objPage->list_data, "magazine");
		// 生成されたWHERE文を取得する		
		list($where, $arrval) = $objSelect->getWhere();
		// 「WHERE」部分を削除する。
		$where = ereg_replace("^WHERE", "", $where);

		// 検索結果の取得
		$objQuery = new SC_Query();
		$from = "dtb_customer";

		// 行数の取得
		$linemax = $objQuery->count($from, $where, $arrval);
		$objPage->tpl_linemax = $linemax;				// 何件が該当しました。表示用

		// ページ送りの取得
		$objNavi = new SC_PageNavi($_POST['search_pageno'], $linemax, SEARCH_PMAX, "fnResultPageNavi", NAVI_PMAX);
		$objPage->arrPagenavi = $objNavi->arrPagenavi;	
		$startno = $objNavi->start_row;

		// 取得範囲の指定(開始行番号、行数のセット)
		$objQuery->setlimitoffset(SEARCH_PMAX, $startno);
		// 表示順序
		$objQuery->setorder("customer_id DESC");
		
		// 検索結果の取得	
		$col = $objSelect->getMailMagazineColumn(lfGetIsMobile($_POST['mail_type']));
		$objPage->arrResults = $objQuery->select($col, $from, $where, $arrval);
		//現在時刻の取得
		$objPage->arrNowDate = lfGetNowDate();
	}
	
	if($_POST['mode'] == 'combz' && function_exists('sfCombzPost')) {
		$objPage->combz_return = sfCombzPost($_POST['combz_type'], $where, $arrval);
	}
	break;
/*
	input:検索結果画面「htmlmail内容設定」ボタン
*/
case 'input':
	//-- 入力値コンバート
	$objPage->list_data = lfConvertParam($_POST, $arrSearchColumn);
	//-- 入力エラーのチェック
	$objPage->arrErr = lfErrorCheck($objPage->list_data);
	//-- エラーなし
	if (!is_array($objPage->arrErr)) {
		//-- 現在時刻の取得
		$objPage->arrNowDate = lfGetNowDate();
		$objPage->arrHidden = lfGetHidden($objPage->list_data); // hidden要素作成
		$objPage->tpl_mainpage = 'mail/input.tpl';
	}
	break;
/*
	template:テンプレート選択
*/
case 'template':
	//-- 入力値コンバート
	$objPage->list_data = lfConvertParam($_POST, $arrSearchColumn);
	
	//-- 時刻設定の取得
	$objPage->arrNowDate['year'] = $_POST['send_year'];
	$objPage->arrNowDate['month'] = $_POST['send_month'];
	$objPage->arrNowDate['day'] = $_POST['send_day'];
	$objPage->arrNowDate['hour'] = $_POST['send_hour'];
	$objPage->arrNowDate['minutes'] = $_POST['send_minutes'];
	
	//-- 入力エラーのチェック
	$objPage->arrErr = lfErrorCheck($objPage->list_data);

	//-- 検索開始
	if ( ! is_array($objPage->arrErr)) {
		$objPage->list_data['name'] = sfManualEscape($objPage->list_data['name']);
		$objPage->arrHidden = lfGetHidden($objPage->list_data); // hidden要素作成
	
		$objPage->tpl_mainpage = 'mail/input.tpl';
		$template_data = getTemplateData($conn, $_POST['template_id']);
		if ( $template_data ){
			foreach( $template_data as $key=>$val ){
				$objPage->list_data[$key] = $val;
			}
		}

		//-- HTMLテンプレートを使用する場合は、HTMLソースを生成してBODYへ挿入
		if ( $objPage->list_data["mail_method"] == 3) {
			$objTemplate = new LC_HTMLtemplate;
			$objTemplate->list_data = lfGetHtmlTemplateData($_POST['template_id']);
			$objSiteInfo = new SC_SiteInfo();
			$objTemplate->arrInfo = $objSiteInfo->data;
			//メール担当写真の表示
			$objUpFile = new SC_UploadFile(IMAGE_TEMP_URL, IMAGE_SAVE_URL);
			$objUpFile->addFile("メール担当写真", 'charge_image', array('jpg'), IMAGE_SIZE, true, SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
			$objUpFile->setDBFileList($objTemplate->list_data);
			$objTemplate->arrFile = $objUpFile->getFormFileList(IMAGE_TEMP_URL, IMAGE_SAVE_URL);
			$objMakeTemplate = new SC_AdminView();
			$objMakeTemplate->assignobj($objTemplate);		
			$objPage->list_data["body"] = $objMakeTemplate->fetch("mail/html_template.tpl");
		}
	}
	break;
/*
	regist_confirm:「入力内容を確認」
	regist_back:「テンプレート設定画面へ戻る」
	regist_complete:「登録」
*/	
case 'regist_confirm':
case 'regist_back':
case 'regist_complete':
	//-- 入力値コンバート
	$arrCheckColumn = array_merge( $arrSearchColumn, $arrRegistColumn );
	$objPage->list_data = lfConvertParam($_POST, $arrCheckColumn);
	
	//現在時刻の取得
	$objPage->arrNowDate = lfGetNowDate();

	//-- 入力エラーのチェック
	$objPage->arrErr = lfErrorCheck($objPage->list_data, 1);
	$objPage->tpl_mainpage = 'mail/input.tpl';
	$objPage->arrHidden = lfGetHidden($objPage->list_data); // hidden要素作成
	
	//-- 検索開始
	if ( ! is_array($objPage->arrErr)) {
			$objPage->list_data['name'] = sfManualEscape($objPage->list_data['name']);
		if ( $_POST['mode'] == 'regist_confirm'){
			$objPage->tpl_mainpage = 'mail/input_confirm.tpl';
		} else if( $_POST['mode'] == 'regist_complete' ){
			lfRegistData($objPage->list_data);
            //メルマガ機能がオンになっているかどうかの判定
			if(MELMAGA_SEND == true) {
                //予約配信モードの分岐
				if(MELMAGA_BATCH_MODE) {
					header("Location: " . URL_DIR . "admin/mail/history.php");
				} else {	
					header("Location: " . URL_DIR . "admin/mail/sendmail.php?mode=now");
				}
				exit;
			} else {
				sfErrorHeader(">> 本サイトではメルマガ配信は行えません。");
			}
		}
	}
	break;
default:
	$objPage->list_data['mail_type'] = 1;
	break;
}

// 配信時間の年を、「現在年~現在年＋１」の範囲に設定
for ($year=date("Y"); $year<=date("Y") + 1;$year++){
	$arrYear[$year] = $year;
}

$objPage->arrBlaynEngine = lfGetBlayn();

$objPage->arrYear = $arrYear;

$objPage->arrCustomerOrderId = lfGetCustomerOrderId($_POST['buy_product_code']);

$objPage->arrCatList = sfGetCategoryList();

$objPage->arrCampaignList = lfGetCampaignList();

//----　ページ表示
$objView->assignobj($objPage);
$objView->display(MAIN_FRAME);

//-------------------------------------------------------------------------------------------------------------------------------

// 商品コードで検索された場合にヒットした受注番号を取得する。
function lfGetCustomerOrderId($keyword) {
	if($keyword != "") {
		$col = "dtb_order.customer_id, dtb_order.order_id";
		$from = "dtb_order LEFT JOIN dtb_order_detail USING(order_id)";
		$where = "product_code LIKE ? AND del_flg = 0";
		$val = sfManualEscape($keyword);
		$arrVal[] = "%$val%";
		$objQuery = new SC_Query();
		$objQuery->setgroupby("customer_id, order_id");
		$arrRet = $objQuery->select($col, $from, $where, $arrVal);
		$arrCustomerOrderId = sfArrKeyValues($arrRet, "customer_id", "order_id");
	}
	return $arrCustomerOrderId;	
}

function lfMakeCsvData( $send_id ){
		
	global $conn;

	$arrTitle  = array(	 'name01','email');
				
	$sql = "SELECT name01,email FROM dtb_send_customer WHERE send_id = ? ORDER BY email";
	$result = $conn->getAll($sql, array($send_id) );
	
	if ( $result ){
		$return = lfGetCSVData( $result, $arrTitle);
	}
	return $return;	
}

//---- CSV出力用データ取得
function lfGetCSVData( $array, $arrayIndex){	
	
	for ($i=0; $i<count($array); $i++){
		
		for ($j=0; $j<count($array[$i]); $j++ ){
			if ( $j > 0 ) $return .= ",";
			$return .= "\"";			
			if ( $arrayIndex ){
				$return .= mb_ereg_replace("<","＜",mb_ereg_replace( "\"","\"\"",$array[$i][$arrayIndex[$j]] )) ."\"";	
			} else {
				$return .= mb_ereg_replace("<","＜",mb_ereg_replace( "\"","\"\"",$array[$i][$j] )) ."\"";
			}
		}
		$return .= "\n";			
	}
	return $return;
}

//現在時刻の取得（配信時間デフォルト値）
function lfGetNowDate(){
	$nowdate = date("Y/n/j/G/i");
	list($year, $month, $day, $hour, $minute) = split("[/]", $nowdate);
	$arrNowDate = array( 'year' => $year, 'month' => $month, 'day' => $day, 'hour' => $hour, 'minutes' => $minute);
	foreach ($arrNowDate as $key => $val){
		switch ($key){
			case 'minutes':
			$val = ereg_replace('^[0]','', $val);
			if ($val < 30){
			$list_date[$key] = '30';
			}else{
			$list_date[$key] = '00';
			}
			break;
			case 'year':
			case 'month':
			case 'day':
			$list_date[$key] = $val;
			break;
		}
	}
		if ($arrNowDate['minutes'] < 30){
			$list_date['hour'] = $hour;
		}else{
			$list_date['hour'] = $hour + 1;
		}
	return $list_date;
}

// 配信内容と配信リストを書き込む
function lfRegistData($arrData){
	
	global $conn;
	global $arrSearchColumn;
	
	$objQuery = new SC_Query();
	$objSelect = new SC_CustomerList( lfConvertParam($arrData, $arrSearchColumn), "magazine" );
	
	$search_data = $conn->getAll($objSelect->getListMailMagazine(lfGetIsMobile($_POST['mail_type'])), $objSelect->arrVal);
	$dataCnt = count($search_data);
	$dtb_send_history = array();
	
    if(DB_TYPE == "pgsql"){
	   $dtb_send_history["send_id"] = $objQuery->nextval('dtb_send_history', 'send_id');
    }
    
    $dtb_send_history["mail_method"] = $arrData['mail_method'];
	$dtb_send_history["subject"] = $arrData['subject'];
	$dtb_send_history["body"] = $arrData['body'];
	if(MELMAGA_BATCH_MODE) {
		//インストール先のサーバーでCRONが有効であるなら指定された時間にメールを送る
        $dtb_send_history["start_date"] = $arrData['send_year'] ."/".$arrData['send_month']."/".$arrData['send_day']." ".$arrData['send_hour'].":".$arrData['send_minutes'];
	} else {
		//CRONが無効であればリアルタイムに送信する
        $dtb_send_history["start_date"] = "now()";
	}
	$dtb_send_history["creator_id"] = $_SESSION['member_id'];
	$dtb_send_history["send_count"] = $dataCnt;
	$arrData['body'] = "";
	$dtb_send_history["search_data"] = serialize($arrData);
	$dtb_send_history["update_date"] = "now()";
	$dtb_send_history["create_date"] = "now()";
   
    //ハッシュdtb_send_historyをデータベースdtb_send_historyに挿入
    $objQuery->insert("dtb_send_history", $dtb_send_history );
    if(DB_TYPE == "mysql"){
        $dtb_send_history["send_id"] = $objQuery->nextval('dtb_send_history','send_id');
    }
    
	if ( is_array( $search_data ) ){
        foreach( $search_data as $line ){
            
            $dtb_send_customer = array();
			$dtb_send_customer["customer_id"] = $line["customer_id"];
			$dtb_send_customer["send_id"] = $dtb_send_history["send_id"];
			$dtb_send_customer["email"] = $line["email"];
			$dtb_send_customer["name"] = $line["name01"] . " " . $line["name02"];            
            $conn->autoExecute("dtb_send_customer", $dtb_send_customer );					
            
        }	
	}
}

// キャンペーン一覧
function lfGetCampaignList() {
	
	global $objQuery;
	
	$sql = "SELECT campaign_id, campaign_name FROM dtb_campaign ORDER BY update_date DESC";
	$arrResult = $objQuery->getall($sql);

	foreach($arrResult as $arrVal) {
		$arrCampaign[$arrVal['campaign_id']] = $arrVal['campaign_name'];
	}
	return $arrCampaign;
}

function lfGetIsMobile($mail_type) {
	// 検索結果の取得			
	$is_mobile = false;
	switch($mail_type) {
		case 1:
			$is_mobile = false;
			break;
		case 2:
			$is_mobile = true;		
			break;
		default:
			$is_mobile = false;
			break;
	}
	
	return $is_mobile;
}

// ブレインエンジンが設定済みか確認
function lfGetBlayn() {
    
    global $objQuery;
    
    $arrRet[now_version] = $objQuery->count("dtb_module", "now_version = (SELECT now_version FROM dtb_module WHERE main_php='blayn/blayn.php')");
    $arrRet[blayn_ip] = $objQuery->count("dtb_blayn");
    return $arrRet;
}
?>