<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	require('__define-pdf.php');

	setcookie("abook_print_sender", $_POST['差出人'], time() + LOGIN_COOKIE_EXPIRE, "/");
	setcookie("abook_print_sender_name", $_POST['差出人名'], time() + LOGIN_COOKIE_EXPIRE, "/");

	$chks = $_POST['check'];
	if (!$chks) {
		error_exit('1件以上、選択してください。', True, False);
	}
	$chk_id = '';
	foreach($chks as $chk){
		if ($chk_id <> '') $chk_id .= ',';
		$chk_id .= $chk;
	}
	if (isset($_POST['Maps'])) {
		if ($_POST['option'] == '世界遺産') {
			header("Location: maps-abook.php?opt=wh&id=" . $chk_id);
		} else {
			header("Location: maps-abook.php?id=" . $chk_id);
		}
	}
	$con = my_mysql_connect(_DB_SCHEMA);
	$sql = "select * from v_abook where id_abook in (".$chk_id.')';
	if ($_POST['sortOrder'] <> '') {
		$sql .= ' order by '.$_POST['sortOrder'];
	}
	$rs = my_mysql_query($sql);
	$row = mysql_num_rows($rs);
	if ($row == 0) {
		error_exit('該当するレコードがみつかりません。', True);
	}
	if (isset($_POST['PDF'])) {
		pdf_cretate_all($rs, PDF_CREATE_FILE, PDF_SOURCE_FILE);
	} elseif (isset($_POST['Email'])) {
		email_all($rs);
//	} elseif (isset($_POST['Maps'])) {
//		maps_all_OLD($rs);
	}
	$con = mysql_close($con);
?>
<?php
function maps_all_OLD($rs) {
	$address = '';
	while ($rec=mysql_fetch_array($rs)) {
		if ($rec['c_address1'] <> '') {
			if ($address <> '') $address .= ',';
			$address .= $rec['c_address1'];
		}
	}
	$url = "../tools/google-maps-earth-marker.php?addr=".urlencode($address);
	header("Location: $url");
}
?>
<?php
function email_all($rs) {
	$mailto = '';
	while ($rec=mysql_fetch_array($rs)) {
		if ($rec['c_email'] <> '') {
			if ($mailto <> '') $mailto .= ',';
			$mailto .= $rec['c_name1'].$rec['c_name2'].'様<'.$rec['c_email'].'>';
		}
	}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>メール送信先</title>
<style type="text/css">
body {
	margin: 10px;
	font-size: 84%;
}
a { color: blue; }
a.a_cancel_back {
	color: #ff8000;
}
</style>
</head>
<body>
<p><a href="mailto:<?= urlencode($mailto) ?>"><?= str_replace(',', '<br>', my_htmlspecialchars($mailto)) ?></a></p>
</body>
</html>
<?php
}
?>
<?php
function pdf_cretate_all($rs, $pdf_out_file, $pdf_src_file) {
	$pdf_foder = ATTACH_FILE_FOLDER_abook.$_SESSION['current_id'];
	if (!myfile_is_dir($pdf_foder)) {
		myfile_mkdir($pdf_foder, '0777', true);
	}
	$pdf_out_path = $pdf_foder.'/'.$pdf_out_file;
	$pdf = new my_class_pdf("P", "mm", "Postcard", array(MINCHO, GOTHIC));
	while ($rec=mysql_fetch_array($rs)) {
		pdf_cretate($pdf, $pdf_src_file, $rec['c_name1'], $rec['c_name2'], $rec['c_zip1'], $rec['c_zip2'], $rec['c_address1'], $rec['c_address2'], $rec['c_renmei']);
	}
	$pdf->output($pdf_out_path, "real", 'F');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>宛名書き葉書(PDF文書)</title>
<style type="text/css">
body {
	margin: 10px;
}
a { color: blue; }
a.a_cancel_back {
	font-size: 84%;
	color: #ff8000;
}
</style>
</head>
<body>
<p><a href="<?= $pdf_out_path ?>">宛名書き葉書(PDF文書)</a></p>
</body>
</html>
<?php
}
?>
<?php
function pdf_cretate($pdf, $pdf_src_file, $name1, $name2, $zip1, $zip2, $addr1, $addr2, $renmei) {
	$pdf->add_page(HAGAKI_FONT, $pdf_src_file);

	// 水平 $x, $y, $size, $text, $max_width=0, $align="left", $space=0, $style="", $family=""
	$pdf->text_horizontal(HAGAKI_ZIP_X, HAGAKI_ZIP_Y, HAGAKI_ZIP_SIZE, $zip1, 0, "left", 4.3, '', GOTHIC);
	$pdf->text_horizontal(HAGAKI_ZIP_X+22.5, HAGAKI_ZIP_Y, HAGAKI_ZIP_SIZE, $zip2, 0, "left", 4.3, '', GOTHIC);

	// 垂直 $x, $y, $size, $text, $max_height=0, $valign="top", $space=0, $style="", $family=""
	list($size) = $pdf->text_vertical(HAGAKI_ADDR_X, HAGAKI_ADDR_Y, HAGAKI_ADDR_SIZE, sjis($addr1), 100);
	if ($addr2 <> '') {
		$pdf->text_vertical(HAGAKI_ADDR_X-6, HAGAKI_ADDR_Y+20, $size, sjis($addr2), 80, "top");
	}
	if ($renmei <> '') {
		$name_x = HAGAKI_NAME_X+4;
	} else {
		$name_x = HAGAKI_NAME_X;
	}
	list($size, $y) = $pdf->text_vertical($name_x, HAGAKI_NAME_Y, HAGAKI_NAME_SIZE, sjis($name1.' '.$name2.'様'), 100, "middle", 2, "B");
	if ($renmei <> '') {
		$pdf->text_vertical($name_x-12, HAGAKI_RENMEI_Y, $size, sjis($renmei.'様'), 50, "top", 2, "B");
	}
	if ($_POST['差出人'] == '印刷') {
		$con = my_mysql_connect(_DB_ACCOUNT_SCHEMA);
		$sql = "select * from m_account where id_account = " . $_SESSION['current_id'];
		$rs = my_mysql_query($sql);
		$rec = mysql_fetch_array($rs);
		$home_address2 = $rec['c_home_address2'];
		if ($home_address2 <> '') {
			$address_x = HAGAKI_S_ADDR_X+2;
		} else {
			$address_x = HAGAKI_S_ADDR_X;
		}
		if ($rec['c_home_address'] <> '') {
			list($size) = $pdf->text_vertical($address_x, HAGAKI_S_ADDR_Y, HAGAKI_S_ADDR_SIZE, sjis($rec['c_home_address']), 76);
		}
		if ($home_address2 <> '') {
			$pdf->text_vertical($address_x-4, HAGAKI_S_ADDR_Y+20, $size, sjis($rec['c_home_address2']), 56);
		}
		if ($_POST['差出人名'] <> '') {
			$pdf->text_vertical(HAGAKI_S_NAME_X, HAGAKI_S_NAME_Y, HAGAKI_S_NAME_SIZE, sjis($_POST['差出人名']), 60);
		} elseif ($rec['c_fullname'] <> '') {
			$pdf->text_vertical(HAGAKI_S_NAME_X, HAGAKI_S_NAME_Y, HAGAKI_S_NAME_SIZE, sjis($rec['c_fullname']), 60);
		}
		if ($rec['c_zip1'] <> '') {
			$pdf->text_horizontal(HAGAKI_S_ZIP_X, HAGAKI_S_ZIP_Y, HAGAKI_S_ZIP_SIZE, $rec['c_zip1'], 0, "left", 2.2, '', GOTHIC);
			$pdf->text_horizontal(HAGAKI_S_ZIP_X+15, HAGAKI_S_ZIP_Y, HAGAKI_S_ZIP_SIZE, $rec['c_zip2'], 0, "left", 2.2, '', GOTHIC);
		}
	}
}
function sjis($str) {
	return mb_convert_encoding($str, 'SJIS-win', 'UTF-8');
}
?>
