<?php
add_shortcode('LG_Beitrag', 'lg_beitrag'); //deprecated
add_shortcode('LG_Beitrag_Title', 'lg_beitrag_title'); //deprecated

add_shortcode('LG_Bericht', 'lg_beitrag'); //neu ab V1.6.13
add_shortcode('LG_Bericht_Titel', 'lg_beitrag_title'); //neu ab V1.6.13


function lg_beitrag_title()
{
	$title = '';
	if (get_query_var('bericht')) {
		$post_id = get_query_var('bericht');

		if ('lgkalender_termin' == get_post_type($post_id)) {
			$bpshow = get_option('lg_kalender_settings_bpshow', false);
			$termin_date = date("d.m.Y", strtotime(get_post_meta($post_id, 'termindate', true)));
			$title = get_the_title($post_id);
			if ((strpos($title, '(BP)') !== false) and (!$bpshow)) {
				$title = $title;
			} else {
				$title = $title . ' vom ' . $termin_date;
			}

			if ((('draft' == get_post_status($post_id)) || ('pending' == get_post_status($post_id)))) {
				$termin_entwurf = '';
				if (is_user_logged_in()) {
					if ('draft' == get_post_status($post_id)) {
						$termin_entwurf = 'Entwurf';
					} else {
						$termin_entwurf = 'Warte auf Freigabe';
					}
					$title = '**' . $termin_entwurf . '** ' . $title;
					return $title;
				} else {
					if ('publish' == get_post_status($post_id)) {
						return $title;
					}
				}
			}
		}
	}
	return ($title);
}

function lg_beitrag()
{
	global $wpdb;
	$lg_content = '
<div id="lgkalender">
<table cellspacing="0" cellpadding="0">
';
	$post_id = '';
	if (get_query_var('bericht')) {
		$post_id = get_query_var('bericht');

		if ((get_query_var('setbericht')) && (is_user_logged_in())) {
			//$lg_content.='Check1 = true'; //for Debug
			if (get_query_var('setbericht') == 'on') {
				update_post_meta($post_id, 'terminfreigabe', 'on');
				//$lg_content.='Check2 = true';	//for Debug
			} else {
				update_post_meta($post_id, 'terminfreigabe', 'off');
			}
		}
	}
	if ('lgkalender_termin' == get_post_type($post_id)) {
		$title = get_the_title($post_id);
		$CB = '';
		/*
		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';
		}
		if (strstr($title, '(SJG)')) {
			$CB = 'SJG';
		}
		if (strstr($title, '(VStP)')) {
			$CB = 'VStP';
		}
		*/
		$CB = getPruefung($title);
		/*
		$termin_pl='Prüfungsleitung';
		$termin_po='Prüfungsobmann';
		$termin_r2='Richter';
		$termin_r3='Richter';
		$termin_ra='Richteranwärter';
		if ($CB=='WT'){
			$termin_pl='Wesenstest-Leitung';
			$termin_po='WS-Obmann';
			$termin_r2='WS-Richter';
			$termin_r3='WS-Richter';
			$termin_ra='WS-Richteranwärter';
		}
		if ($CB=='SJG'){
			$termin_pl='';
			$termin_po='Richter';
			$termin_r2='Richter';
			$termin_r3='Richter';
			$termin_ra='';
		}
		if ($CB=='ZS'){
			$termin_pl='Zuchtschau-Leitung';
			$termin_po='Zuchtschau-Richter';
			$termin_r2='Ringschreiber';
			$termin_r3='';
			$termin_ra='ZS-Richteranwärter';
		}
		$termin_pl = getPLTEXT($CB);
		$termin_po = getPOTEXT($CB);
		$termin_r2 = getR2TEXT($CB);
		$termin_r3 = getR3TEXT($CB);
		$termin_ra = getRATEXT($CB);
		*/
		$termin_ort = stripslashes(get_post_meta($post_id, 'terminort', true));
		$termin_freigabe = get_post_meta($post_id, 'terminfreigabe', true);
		$termin_meldeschein = get_post_meta($post_id, 'terminmeldeschein', true);
		$termin_date = get_post_meta($post_id, 'termindate', true);
		$termin_area = get_post_meta($post_id, 'terminarea', true);
		$image_url = esc_attr(get_post_meta($post_id, 'imagePath', true));
		$image_url2 = esc_attr(get_post_meta($post_id, 'imagePath2', true));
		$image_url3 = esc_attr(get_post_meta($post_id, 'imagePath3', true));
		$image_url4 = esc_attr(get_post_meta($post_id, 'imagePath4', true));
		$termin_text4 = get_post_meta($post_id, 'termintext4', true);
		$image_url5 = esc_attr(get_post_meta($post_id, 'imagePath5', true));
		$termin_text5 = get_post_meta($post_id, 'termintext5', true);
		$termin_bericht = nl2br(get_post_meta($post_id, 'terminbericht', true));
		$termin_date = date('d.m.Y', strtotime($termin_date));
		$title = get_the_title() . ' ' . $termin_ort . ' vom ' . $termin_date;
		$termin_bilderanzahl = get_post_meta($post_id, 'terminbilderanzahl', true);
		if ($termin_bilderanzahl > '') {
		} else {
			$termin_bilderanzahl = '0';
		}
		if (($termin_freigabe == 'on') || (is_user_logged_in())) {
			$post_list = getMeldungen_ID($post_id);
			$lg_content .= '
							<tr style="border: none;">
								<td width="96%"><a href="javascript:history.back()"><small>zurück zur Übersicht</small></a><td>
							</tr><tr><td><table>';

			if ($termin_meldeschein == 'on') {
				$richterliste = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "lgmeldungenrichter WHERE postid IN (" . $post_list . ")");
				$lg_content .= '<tr><td width="180px">' . getPLTEXT($CB) . '<br>' . getPOTEXT($CB) . '<br>' . getR2TEXT($CB).'<br>' . getR3TEXT($CB).'<br>' . getRATEXT($CB);
				/*
				$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->r3);
				if (isset($richter->nachname)) {
					$lg_content .= '<br>' . getR3TEXT($CB);
				}
				$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->ra);
				if (isset($richter->nachname)) {
					$lg_content .= '<br>' . getRATEXT($CB);
				}
				*/
				$lg_content .= '</td><td>';


				
				//$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->pl);
				if ($CB <> 'SJG') {
					/*
					if (isset($richter->vorname)) {
						$vorname = $richter->vorname;
					} else {
						$vorname = '';
					}
					if (isset($richter->nachname)) {
						$nachname = $richter->nachname;
					} else {
						$nachname = '';
					}
					$lg_content .= stripslashes($vorname) . ' ' . stripslashes($nachname) . '<br>';
					*/

					$lg_content .= getPLName($post_id,false,false,$CB) . '<br>';
				} else {
					$lg_content .= '<br>';
				}
				/*
				$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->po);
				if (isset($richter->vorname)) {
					$vorname = $richter->vorname;
				} else {
					$vorname = '';
				}
				if (isset($richter->nachname)) {
					$nachname = $richter->nachname;
				} else {
					$nachname = '';
				}
				$lg_content .= stripslashes($vorname) . ' ' . stripslashes($nachname) . '<br>';
				*/
				$lg_content .= getPOName($post_id,false,false,$CB) . '<br>';

				/*
				$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->r2);
				if (isset($richter->vorname)) {
					$vorname = $richter->vorname;
				} else {
					$vorname = '';
				}
				if (isset($richter->nachname)) {
					$nachname = $richter->nachname;
				} else {
					$nachname = '';
				}
				$lg_content .= stripslashes($vorname) . ' ' . stripslashes($nachname);
				*/
				$lg_content .= getR2Name($post_id,false,false,$CB) . '<br>';

				/*
				$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->r3);
				if (isset($richter->vorname)) {
					$vorname = $richter->vorname;
				} else {
					$vorname = '';
				}
				if (isset($richter->nachname)) {
					$nachname = $richter->nachname;
				} else {
					$nachname = '';
				}
				if ($nachname > '') {
					$lg_content .= '<br>' . stripslashes($vorname) . ' ' . stripslashes($nachname);
				}
				*/
				$text = getR3Name($post_id,false,false,$CB);
				if ($text > ''){
					$lg_content .= getR3Name($post_id,false,false,$CB) . '<br>';
				}else{
					$lg_content .= '-<br>';
				}

				/*
				$richter = $wpdb->get_row("SELECT * from " . $wpdb->prefix . "lgrichter where id like " . $richterliste->ra);
				if (isset($richter->vorname)) {
					$vorname = $richter->vorname;
				} else {
					$vorname = '';
				}
				if (isset($richter->nachname)) {
					$nachname = $richter->nachname;
				} else {
					$nachname = '';
				}
				if ($nachname > '') {
					$lg_content .= '<br>' . stripslashes($vorname) . ' ' . stripslashes($nachname);
				}
				*/
				$text = getRAName($post_id,false,false,$CB);
				if ($text > ''){
					$lg_content .= getRAName($post_id,false,false,$CB) . '<br>';
				}else{
					$lg_content .= '-<br>';
				}
				$lg_content .= '	</td></tr>';
			}

			if (is_user_logged_in()) {
				$pagebericht = get_option('lg_kalender_settings_pagebericht', '');
				$post_url = get_page_link($pagebericht) . '?bericht=' . $post_id;
				$lg_content .= '<tr><td colspan 2>';
				$user = wp_get_current_user();
				$allowed_roles = array(get_option('lg_kalender_settings_allowMeldungEdit', ''));
				if (array_intersect($allowed_roles, $user->roles)) {
					if ($termin_freigabe == 'on') {
						$lg_content .= '<a href="' . $post_url . '&setbericht=off" class="button">Bericht nicht veröffentlichen</a>';
					} else {
						$lg_content .= '<a href="' . $post_url . '&setbericht=on" class="button">Bericht veröffentlichen</a>';
					}
				}
				$lg_content .= '</td></tr>';
			}
			$lg_content .= '</table></td></tr><tr>
								<td>
									<table>';
			if (($CB == 'SJG') || ($CB == 'WT')) {
				$lg_content .= '<tr><th>Nr.</th><th>Name Hund</th><th>R/H</th><th>ZB-Nr.</th><th>Wurftag</th><th></th></tr>';
			}
			if (($CB == 'ZS')) {
				$lg_content .= '<tr><th>Nr.</th><th>Name Hund</th><th>R/H</th><th>ZB-Nr.</th><th>Wurftag</th><th>FW</th><th>WH</th><th>BU</th><th>Haar</th><th>Gebiss</th></tr>';
			}
			if (($CB == 'JP') || ($CB == 'ZP')) {
				$lg_content .= '<tr><th>Nr.</th><th>Name Hund</th><th>R/H</th><th>ZB-Nr.</th><th>Wurftag</th><th>Punkte</th><th>Preis</th><th>Laut</th></tr>';
			}
			if (($CB == 'GP') || ($CB == 'BP')) {
				$lg_content .= '<tr><th>Nr.</th><th>Name Hund</th><th>R/H</th><th>ZB-Nr.</th><th>Wurftag</th><th>Punkte</th><th>Preis</th></tr>';
			}
			if ($CB == 'VStP') {
				$lg_content .= '<tr><th>Nr.</th><th>Name Hund</th><th>R/H</th><th>ZB-Nr.</th><th>Wurftag</th><th>Punkte</th><th>Ergebnis</th><th>Laut</th></tr>';
			}

			$meldungen = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "lgmeldungen where postid IN (" . $post_list . ") ORDER BY sortierung,id");
			if ($CB == 'ZS') {
				$meldungen = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "lgmeldungen where postid IN (" . $post_list . ") ORDER BY sortierung,hund_rh DESC,hund_zbnr");
			}
			$i = 0;
			$preis = array(
				"0" => "--",
				"1" => "I.",
				"2" => "II.",
				"3" => "III.",
				"4" => "n.b.",
				"5" => "nicht erschienen",
				"7" => "zurückgestellt",
				"8" => "bestanden",
			);
			$laut = array(
				"0" => "--",
				"1" => "spl",
				"2" => "sil",
				"3" => "fl",
				"4" => "lt",
				"5" => "wdl",
				"6" => "stumm",
				"7" => "frgl.",
			);
			$formwert = array(
				"0" => "--",
				"1" => "v",
				"2" => "sg",
				"3" => "g",
				"4" => "disq.",
				"5" => "nicht erschienen",
			);
			foreach ($meldungen as $meldung) {
				if ($meldung->preis != '5') {
					if ((($CB == 'SJG') && ($meldung->preis == '8')) || ($CB <> 'SJG')) {
						if ((($meldung->preis != '5') && ($CB <> 'ZS')) || (($CB == 'ZS') && ($meldung->fw != '5'))) {
							$i++;
							$lg_content .= "<tr><td>" . $i;
							$lg_content .= "</td><td>" . stripslashes($meldung->hund_name);
							$lg_content .= "</td><td>" . $meldung->hund_rh;
							$lg_content .= "</td><td>" . $meldung->hund_zbnr;
							$lg_content .= "</td><td>" . $meldung->hund_wurftag;
							if (($CB == 'SJG') || ($CB == 'WT')) {
								$lg_content .= "</td><td>" . $preis[$meldung->preis];
							}
							if (($CB == 'JP') || ($CB == 'ZP') || ($CB == 'VStP')) {
								$lg_content .= "</td><td>" . $meldung->punkte;
								$lg_content .= "</td><td>" . $preis[$meldung->preis];
								$lg_content .= "</td><td>" . $laut[$meldung->laut];
							}
							if (($CB == 'GP') || ($CB == 'BP')) {
								$lg_content .= "</td><td>" . $meldung->punkte;
								$lg_content .= "</td><td>" . $preis[$meldung->preis];
							}
							if (($CB == 'ZS')) {
								$lg_content .= "</td><td>" . $formwert[$meldung->fw];
								$lg_content .= "</td><td>" . $meldung->wh;
								$lg_content .= "</td><td>" . $meldung->bu;
								$lg_content .= "</td><td>" . $meldung->haar;
								$lg_content .= "</td><td>" . stripslashes($meldung->gebiss);
							}
							$lg_content .= "</td></tr>";
						}
					}
				}
			}
			$lg_content .= '
									</table>
								</td>
							</tr>
							';
		}
		for ($i = 1; $i < ($termin_bilderanzahl + 1); $i++) {
			$parameter_path = 'imagePath' . $i;
			$parameter_text = 'termintext' . $i;
			$parameter_smaller = 'imagesmall' . $i;
			$image_url = esc_attr(get_post_meta($post_id, $parameter_path, true));
			$termin_text = get_post_meta($post_id, $parameter_text, true);
			$termin_imagesmall = get_post_meta($post_id, $parameter_smaller, true);
			if ($termin_imagesmall == 'on') {
				$width = '55%';
			} else {
				$width = '90%';
			}
			$lg_content .= '
							<tr style="border: none;">
								<td><p align="center"><img src="' . $image_url . '" alt="Prüfungsergebnis" width="' . $width . '"></td>
							</tr>
							<tr style="border: none;">
								<td><p align="center"><small>' . $termin_text . '</small></td>
							</tr>
							';
		}
		if ($termin_bericht > '') {
			$lg_content .= '
							<tr style="border: none;">
								<td><table><tr><td width="25%"></td><td><small>' . $termin_bericht . '</small></td><td width="25%"></td></tr></table></td>
							</tr>
							';
		}
	} else {
		$lg_content .= '
							<tr style="border: none;">
								<td>Kein Bericht hinterlegt.' . $post_id . '</td>
							</tr>
							';
	}
	$lg_content .= '
<tr style="border: none;">
	<td><a href="javascript:history.back()"><small>zurück zur Übersicht</small></a></td>
</tr>
</table>
</div>';
	if (((is_user_logged_in()) and (('draft' == get_post_status($post_id)) || ('pending' == get_post_status($post_id)))) || ('publish' == get_post_status($post_id))) {
		return $lg_content;
	} else {
		return '';
	}
}
