<?php
//Script zum herunterladen eines Meldescheines vom Terminkalender
$title = get_the_title();
$post_id = get_the_ID();
$data['file'] = get_option('lg_kalender_settings_pfad1', true);
$data['flattern'] = get_option('lg_kalender_settings_pfad1_flattern', false);
$url = 'https://nienhausen.com/mypdftk';
$array = array('(JP)', '(BP)', '(ZP)', '(GP)', '(WT)', '(ZS)');
$pruefung = false;
for ($x = 0; $x < count($array); $x++) {
	if (strstr($title, $array[$x])) {
		$pruefung = true;
	}
}
/*
if (strstr($title, '(JP)')) {
	$CB = 'JP';
}
if (strstr($title, '(BP)')) {
	$CB = 'BP';
}
if (strstr($title, '(ZP)')) {
	$CB = 'ZP';
}
if (strstr($title, '(WT)')) {
	$CB = 'WT';
}
if (strstr($title, '(ZS)')) {
	$CB = 'ZS';
}
if (strstr($title, '(GP)')) {
	$CB = 'GP';
}
*/
$CB = getPruefung($title);
if (strstr($CB, 'SJG')) {
	//$CB = 'SJG';
	$data['file'] = get_option('lg_kalender_settings_pfad2', true);
	$data['flattern'] = get_option('lg_kalender_settings_pfad2_flattern', false);
}
if ((strstr($CB, 'VStP'))||(strstr($CB, 'VSwP'))) {
	//$CB = 'VStP';
	$data['file'] = get_option('lg_kalender_settings_pfad3', true);
	$data['flattern'] = get_option('lg_kalender_settings_pfad3_flattern', false);
}
$meldeschin_counter = intval(get_post_meta($post_id, 'terminmeldescheincounter', true));
$data['postid'] = $post_id;
$data['Meldestelle_Mail'] = get_option('lg_kalender_settings_email', true);
$data['newfile'] = 'Meldeschein_' . $CB . '_' . get_post_meta($post_id, 'terminort', true) . '_' . date('d.m.Y', strtotime(get_post_meta($post_id, 'termindate', true))) . '.pdf';
$data['POrt'] = get_post_meta($post_id, 'terminort', true);
$data['PDatum'] = date('d.m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
$data['Pruefung'] = $CB;
if ($data['flattern'] == '1') {
	$data['flattern'] = 'true';
} else {
	$data['flattern'] = 'false';
}
if (($CB <> 'VStP')&&($CB <> 'VSwP')) {
	$data['Landesgruppe'] = get_option('lg_kalender_settings_name', true);
	$data['Datum_Ort'] = get_post_meta($post_id, 'terminort', true) . ', ' . date('d.m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
	$data['Ort'] = get_post_meta($post_id, 'terminort', true);
	$data['Datum'] = date('d.m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
	if ($CB == 'BP') {
		$data['newfile'] = 'Meldeschein_' . $CB . '_' . date('m.Y', strtotime(get_post_meta($post_id, 'termindate', true))) . '.pdf';
		$data['POrt'] = '';
		$data['PDatum'] = '00.' . date('m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
		$data['Datum_Ort'] = '                                                               , 00.' . date('m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
		$data['Ort'] = '';
		$data['Datum'] = '00.' . date('m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
	}
	$data['CheckBox1'] = $CB;
	if (isset($_COOKIE['doglist'])) {
		$cjson = $_COOKIE['doglist'];
		$cjson = str_replace('\\"', '"', $cjson);
		$cjson = str_replace('\\\\', '\\', $cjson);
		$cjson = iconv("UTF-8", "Windows-1252", $cjson);
		$cjson = str_replace("\\\\", "\\", $cjson);
		$mydog = json_decode($cjson, true);
		$d1_name = stripslashes($mydog["d1_name"]);
		$d1_wtag = $mydog["d1_wtag"];
		$d1_sex = $mydog["d1_sex"];
		$d1_zbnr = $mydog["d1_zbnr"];
		$d1_chip = $mydog["d1_chip"];
		$d1_vater = stripslashes($mydog["d1_vater"]);
		$d1_mutter = stripslashes($mydog["d1_mutter"]);
		$d2_name = stripslashes($mydog["d2_name"]);
		$d2_wtag = $mydog["d2_wtag"];
		$d2_sex = $mydog["d2_sex"];
		$d2_zbnr = $mydog["d2_zbnr"];
		$d2_chip = $mydog["d2_chip"];
		$d2_vater = stripslashes($mydog["d2_vater"]);
		$d2_mutter = stripslashes($mydog["d2_mutter"]);
		$d3_name = stripslashes($mydog["d3_name"]);
		$d3_wtag = $mydog["d3_wtag"];
		$d3_sex = $mydog["d3_sex"];
		$d3_zbnr = $mydog["d3_zbnr"];
		$d3_chip = $mydog["d3_chip"];
		$d3_vater = stripslashes($mydog["d3_vater"]);
		$d3_mutter = stripslashes($mydog["d3_mutter"]);
		$daktiv = $mydog["daktiv"];
		if ($daktiv == 'dog1') {
			$data['Name_Hund'] = $d1_name;
			$data['ZBNR'] = $d1_zbnr;
			$data['W_Tag'] = $d1_wtag;
			$data['Chip_Nr'] = $d1_chip;
			$data['Mutter'] = $d1_mutter;
			$data['Vater'] = $d1_vater;
			if ($d1_name > '') {
				if ($d1_sex == 'on') {
					$data['CheckBox4'] = 'male';
					$data['Hund_RH'] = 'R';
				} else {
					$data['CheckBox4'] = 'female';
					$data['Hund_RH'] = 'H';
				}
			}
		}
		if ($daktiv == 'dog2') {
			$data['Name_Hund'] = $d2_name;
			$data['ZBNR'] = $d2_zbnr;
			$data['W_Tag'] = $d2_wtag;
			$data['Chip_Nr'] = $d2_chip;
			$data['Mutter'] = $d2_mutter;
			$data['Vater'] = $d2_vater;
			if ($d2_name > '') {
				if ($d2_sex == 'on') {
					$data['CheckBox4'] = 'male';
					$data['Hund_RH'] = 'R';
				} else {
					$data['CheckBox4'] = 'female';
					$data['Hund_RH'] = 'H';
				}
			}
		}
		if ($daktiv == 'dog3') {
			$data['Name_Hund'] = $d3_name;
			$data['ZBNR'] = $d3_zbnr;
			$data['W_Tag'] = $d3_wtag;
			$data['Chip_Nr'] = $d3_chip;
			$data['Mutter'] = $d3_mutter;
			$data['Vater'] = $d3_vater;
			if ($d3_name > '') {
				if ($d3_sex == 'on') {
					$data['CheckBox4'] = 'male';
					$data['Hund_RH'] = 'R';
				} else {
					$data['CheckBox4'] = 'female';
					$data['Hund_RH'] = 'H';
				}
			}
		}
	}
} else { //ab hier JGHV Meldeschein
	$data['flattern'] = 'false';
	if (isset($_COOKIE['doglist'])) {
		$cjson = $_COOKIE['doglist'];
		$cjson = str_replace('\\"', '"', $cjson);
		$cjson = str_replace('\\\\', '\\', $cjson);
		$cjson = iconv("UTF-8", "Windows-1252", $cjson);
		$cjson = str_replace("\\\\", "\\", $cjson);
		$mydog = json_decode($cjson, true);
		$d1_name = stripslashes($mydog["d1_name"]);
		$d1_wtag = $mydog["d1_wtag"];
		$d1_sex = $mydog["d1_sex"];
		$d1_zbnr = $mydog["d1_zbnr"];
		$d1_chip = $mydog["d1_chip"];
		$d1_vater = stripslashes($mydog["d1_vater"]);
		$d1_mutter = stripslashes($mydog["d1_mutter"]);
		$d2_name = stripslashes($mydog["d2_name"]);
		$d2_wtag = $mydog["d2_wtag"];
		$d2_sex = $mydog["d2_sex"];
		$d2_zbnr = $mydog["d2_zbnr"];
		$d2_chip = $mydog["d2_chip"];
		$d2_vater = stripslashes($mydog["d2_vater"]);
		$d2_mutter = stripslashes($mydog["d2_mutter"]);
		$d3_name = stripslashes($mydog["d3_name"]);
		$d3_wtag = $mydog["d3_wtag"];
		$d3_sex = $mydog["d3_sex"];
		$d3_zbnr = $mydog["d3_zbnr"];
		$d3_chip = $mydog["d3_chip"];
		$d3_vater = stripslashes($mydog["d3_vater"]);
		$d3_mutter = stripslashes($mydog["d3_mutter"]);
		$daktiv = $mydog["daktiv"];
	}
	if ($CB == 'VStP'){
	$data['Pruefart'] = 'On7';
	$data['BvomFührer'] = 'On';
	}
	if ($CB == 'VSwP'){
		$data['Pruefart'] = 'On5';
		$data['20Std'] = 'On';
		}
	$data['Verein'] = 'Parson Russell Terrier Club Deutschland';
	$data['EDVNr'] = '3305';
	$data['Prüfungsort'] = get_post_meta($post_id, 'terminort', true);
	$data['Prüfungsdatum'] = date('d.m.Y', strtotime(get_post_meta($post_id, 'termindate', true)));
	$data['Rasse'] = 'PRT';
	if ($daktiv == 'dog1') {
		$data['NamedesHundes'] = $d1_name;
		$data['ZBNr'] = $d1_zbnr;
		$data['gew'] = $d1_wtag;
		$chip = $d1_chip;
		$data['Mutter'] = $d1_mutter;
		$data['Vater'] = $d1_vater;
		if ($d1_name > '') {
			if ($d1_sex == 'on') {
				$data['Geschlecht'] = 'Rüde';
			} else {
				$data['Geschlecht'] = 'Hündin';
			}
		}
	}
	if ($daktiv == 'dog2') {
		$data['NamedesHundes'] = $d2_name;
		$data['ZBNr'] = $d2_zbnr;
		$data['gew'] = $d2_wtag;
		$chip = $d2_chip;
		$data['Mutter'] = $d2_mutter;
		$data['Vater'] = $d2_vater;
		if ($d2_name > '') {
			if ($d2_sex == 'on') {
				$data['Geschlecht'] = 'Rüde';
			} else {
				$data['Geschlecht'] = 'Hündin';
			}
		}
	}
	if ($daktiv == 'dog3') {
		$data['NamedesHundes'] = $d3_name;
		$data['ZBNr'] = $d3_zbnr;
		$data['gew'] = $d3_wtag;
		$chip = $d3_chip;
		$data['Mutter'] = $d3_mutter;
		$data['Vater'] = $d3_vater;
		if ($d3_name > '') {
			if ($d3_sex == 'on') {
				$data['Geschlecht'] = 'Rüde';
			} else {
				$data['Geschlecht'] = 'Hündin';
			}
		}
	}
	for ($i = 0; $i < strlen($chip); $i++) {
		$var = 'Text1' . strval($i);
		$data[$var] = $chip[$i];
	}
}
$options = array(
	'http' => array(
		'header' => "Content-type: application/x-www-form-urlencoded\r\n",
		'method' => 'POST',
		'content' => http_build_query($data)
	)
);
$context = stream_context_create($options);
$url = file_get_contents($url);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */
}
update_post_meta($post_id, 'terminmeldescheincounter', strval($meldeschin_counter + 1));
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=" . $data['newfile']);
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
var_dump($result);
