<?php
add_shortcode('LG_Richter', 'lg_richter');


function lg_richter()
{
	global $wpdb;
	global $wp;
	$lg_content = '';
	$current_url = home_url(add_query_arg(array(), $wp->request));
	wp_enqueue_style("zz-shortcode-jscss-style");
	$css = '
		#lg_kalender_table_meldung {font-size:' . get_option('lg_kalender_settings_size_meldung', 'unset') . ';}
	';
	wp_add_inline_style('zz-shortcode-jscss-style', $css);

	if (get_query_var('insert')) {
		if ('new' == get_query_var('insert')) {
			$wpdb->insert(
				$wpdb->prefix . 'lgrichter',
				//Tabellenname 
				array(
					'mitgliedschaft' => '0'
				),
				array(
					'%s' //String-Wert (column1)

				)
			);
			echo ("<script>location.href = '" . $current_url . "'</script>");
			//wp_redirect($current_url);
			exit;
		}
	}
	if ((isset($_POST['richterid'])) || (isset($_GET['richterid']))) {
		if (isset($_POST['submit'])) {
			if ($_POST['submit'] == 'delete') {
				if (isset($_POST['richternummerid'])) {
					$wpdb->delete($wpdb->prefix . "lgrichternummer", array('id' => $_POST['richternummerid']));
				} else {
					$results = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "lgrichternummer where richterid like " . $_POST['richterid']);
					if (count($results) > 0) {
					} else {
						$wpdb->delete($wpdb->prefix . "lgrichter", array('id' => $_POST['richterid']));
						echo ("<script>location.href = '" . $current_url . "'</script>");
						//wp_redirect($current_url);
						exit;
					}
				}
			} elseif ($_POST['submit'] == 'neu') {
				$wpdb->insert(
					$wpdb->prefix . 'lgrichternummer',
					//Tabellenname 
					array(
						'richterid' => $_POST['richterid'],
						'bau' => '0',
						'sw' => '0',
						'wt' => '0',
						'zs' => '0',
						'ra' => '0',
						'norm' => '0'
					),
					array(
						'%s',
						'%s',
						'%s',
						'%s',
						'%s',
						'%s',
						'%s' //String-Wert (column1)

					)
				);
				//wp_redirect($current_url);
				//exit;
			} elseif ($_POST['submit'] == 'speichern') {
				if (isset($_POST['richternummerid'])) {
					if (isset($_POST['bau'])){
						$bau = $_POST['bau'];
					}else{
						$bau ='';
					}
					if (isset($_POST['sw'])){
						$sw = $_POST['sw'];
					}else{
						$sw ='';
					}
					if (isset($_POST['wt'])){
						$wt = $_POST['wt'];
					}else{
						$wt ='';
					}
					if (isset($_POST['zs'])){
						$zs = $_POST['zs'];
					}else{
						$zs ='';
					}
					if (isset($_POST['ra'])){
						$ra = $_POST['ra'];
					}else{
						$ra ='';
					}
					if (isset($_POST['norm'])){
						$norm = $_POST['norm'];
					}else{
						$norm ='';
					}
					$wpdb->update(
						$wpdb->prefix . 'lgrichternummer',
						//Tabellenname 
						array(
							'richternummer' => $_POST['richternummer'],
							'verein' => $_POST['verein'],
							'bau' => $bau,
							'sw' => $sw,
							'wt' => $wt,
							'zs' => $zs,
							'ra' => $ra,
							'norm' => $norm
						),
						array('id' => $_POST['richternummerid']),
						array(
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s' //String-Wert (column1)

						),
						array('%d')
					);
				} else {
					if(isset($_POST['mitgliedschaft'])){
						$mitgliedschaft=$_POST['mitgliedschaft'];
					}else{
						$mitgliedschaft='';
					}
					$wpdb->update(
						$wpdb->prefix . 'lgrichter',
						//Tabellenname 
						array(
							'nachname' => $_POST['nachname'],
							'vorname' => $_POST['vorname'],
							'strasse' => $_POST['strasse'],
							'plz' => $_POST['plz'],
							'ort' => $_POST['ort'],
							'telefon' => $_POST['telefon'],
							'mobil' => $_POST['mobil'],
							'email' => $_POST['email'],
							'iban' => $_POST['iban'],
							'bank' => $_POST['bank'],
							'mitgliedschaft' => $mitgliedschaft
						),
						array('id' => $_POST['richterid']),
						array(
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s',
							'%s' //String-Wert (column1)

						),
						array('%d')
					);
					//wp_redirect($current_url);
					//exit;
				}
			}
		} //else{
		if (isset($_POST['richterid'])) {
			$richterid = $_POST['richterid'];
		}
		if (isset($_GET['richterid'])) {
			$richterid = $_GET['richterid'];
		}
		$meldung = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "lgrichter where id like " . $richterid);
		$lg_content .= '<div id="lgkalender"><form method="post"><input type="hidden" name="richterid" value="' . $meldung->id . '"><table id="lg_kalender_table_meldung" cellspacing="0" cellpadding="0"><tr><th>Feld</th><th>Wert</th><th> </th></tr>';
		$lg_content .= '<tr><td>Name</td><td><input type="text" name="nachname" size="50" maxlength="100" value="' . stripslashes($meldung->nachname) . '"></td></tr>';
		$lg_content .= '<tr><td>Vorname</td><td><input type="text" name="vorname" size="50" maxlength="100" value="' . stripslashes($meldung->vorname) . '"></td></tr>';
		$lg_content .= '<tr><td>Strasse</td><td><input type="text" name="strasse" size="50" maxlength="100" value="' . stripslashes($meldung->strasse) . '"></td></tr>';
		$lg_content .= '<tr><td>PLZ</td><td><input type="text" name="plz" size="10" maxlength="10" value="' . $meldung->plz . '"></td></tr>';
		$lg_content .= '<tr><td>Ort</td><td><input type="text" name="ort" size="50" maxlength="100" value="' . stripslashes($meldung->ort) . '"></td></tr>';
		$lg_content .= '<tr><td>Telefon</td><td><input type="text" name="telefon" size="50" maxlength="50" value="' . $meldung->telefon . '"></td></tr>';
		$lg_content .= '<tr><td>Mobil</td><td><input type="text" name="mobil" size="50" maxlength="50" value="' . $meldung->mobil . '"></td></tr>';
		$lg_content .= '<tr><td>E-Mail Adresse</td><td><input type="text" name="email" size="50" maxlength="150" value="' . $meldung->email . '"></td></tr>';
		$lg_content .= '<tr><td>Mitgliedschaft im<br>PRTCD, DJT oder DFV</td><td><input type="checkbox" name="mitgliedschaft" value="1"' . checked($meldung->mitgliedschaft, 1, false) . '></td></tr>';
		$lg_content .= '<tr><td>Bank IBAN</td><td><input type="text" name="iban" pattern="^DE\d{2}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{2}|DE\d{20}$" size="50" maxlength="50" value="' . $meldung->iban . '"></td></tr>';
		$lg_content .= '<tr><td>Bank Name</td><td><input type="text" name="bank" size="50" maxlength="100" value="' . stripslashes($meldung->bank) . '"></td></tr>';
		$lg_content .= '<tr><td>';
		$user = wp_get_current_user();
		$allowed_roles = array(get_option('lg_kalender_settings_allowRichterNew', ''));
		if (array_intersect($allowed_roles, $user->roles)) {
			$lg_content .= '<input type="submit" name="submit" value="delete" onclick="return confirm(\'Richter löschen ?\');">';
		}
		$lg_content .= '</td><td><a href="' . $current_url . '" class="button">zurück zur Übersicht</a></td><td>';
		$user = wp_get_current_user();
		$allowed_roles = array(get_option('lg_kalender_settings_allowRichterEdit', ''));
		if (array_intersect($allowed_roles, $user->roles)) {
			$lg_content .= '<input type="submit" name="submit" value="speichern">';
		}
		$lg_content .= '</td></tr>';
		$lg_content .= '</table></form>';
		$meldungen = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "lgrichternummer where richterid like " . $richterid);
		$lg_content .= '<div id="lgkalender"><table id="lg_kalender_table_meldung" cellspacing="0" cellpadding="0"><tr><th>Richternummer</th><th>Verein</th><th>JGHV</th><th>Bau</th><th>SW</th><th>WT</th><th>ZS</th><th>RA</th><th></th><th>';
		$user = wp_get_current_user();
		$allowed_roles = array(get_option('lg_kalender_settings_allowRichterNummerEdit', ''));
		if (array_intersect($allowed_roles, $user->roles)) {
			$lg_content .= '<form method="post"><input type="hidden" name="richterid" value="' . $meldung->id . '"><input type="submit" name="submit" value="neu"></form>';
		}
		$lg_content .= '</th></tr>';
		$richterid = $meldung->id;
		foreach ($meldungen as $meldung) {
			$lg_content .= "<tr><form method='post'><input type='hidden' name='richternummerid' value='" . $meldung->id . "'><input type='hidden' name='richterid' value='" . $richterid . "'>";
			$lg_content .= "<td><input type='text' name='richternummer' size='12' maxlength='50' value='" . $meldung->richternummer . "'></td>";
			$lg_content .= '<td><input type="text" name="verein" size="20" maxlength="100" value="' . stripslashes($meldung->verein) . '"></td>';
			$lg_content .= '<td><input type="checkbox" name="norm" value="1"' . checked($meldung->norm, 1, false) . '></td>';
			$lg_content .= '<td><input type="checkbox" name="bau" value="1"' . checked($meldung->bau, 1, false) . '></td>';
			$lg_content .= '<td><input type="checkbox" name="sw" value="1"' . checked($meldung->sw, 1, false) . '></td>';
			$lg_content .= '<td><input type="checkbox" name="wt" value="1"' . checked($meldung->wt, 1, false) . '></td>';
			$lg_content .= '<td><input type="checkbox" name="zs" value="1"' . checked($meldung->zs, 1, false) . '></td>';
			$lg_content .= '<td><input type="checkbox" name="ra" value="1"' . checked($meldung->ra, 1, false) . '></td>';
			$user = wp_get_current_user();
			$allowed_roles = array(get_option('lg_kalender_settings_allowRichterNummerEdit', ''));
			if (array_intersect($allowed_roles, $user->roles)) {
				$lg_content .= "<td><input type='submit' name='submit' value='delete' onclick=\"return confirm('Richternummer löschen ?');\"></td><td><input type='submit' name='submit' value='speichern'></td>";
			}
			$lg_content .= "</form></tr>";
		}
		$lg_content .= "</table></div>";
		//}
	} else {

		$lg_content .= '<div class = "wrap"><div id="lgkalender"><table id="lg_kalender_table_richter" cellspacing="0" cellpadding="0"><tr><th id="lg_kalender_richter_cell1">Nr.</th><th id="lg_kalender_richter_cell2">Name</th><th id="lg_kalender_richter_cell5">Vorname</th><th id="lg_kalender_richter_cell6">Adresse</th><th id="lg_kalender_richter_cell7">Telefon</th><th id="lg_kalender_richter_cell8">Mobil</th><th id="lg_kalender_richter_cell9">J<br>G<br>H<br>v</th><th>B<br>a<br>u</th><th>S<br>w</th><th>W<br>T</th><th> </th></tr>';
		$meldungen = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "lgrichter ORDER BY nachname,vorname");

		//$zaehler = 0;
		$i = 0;
		foreach ($meldungen as $meldung) {
			$i++;
			$lg_content .= "<form method='post'><input type='hidden' name='richterid' value='" . $meldung->id . "'>";
			$lg_content .= "<tr><td>" . $i . "</td>";
			$lg_content .= "</td><td>" . stripslashes($meldung->nachname);
			$lg_content .= "</td><td>" . stripslashes($meldung->vorname);
			$lg_content .= "</td><td>" . stripslashes($meldung->strasse) . "<br>" . $meldung->plz . " " . stripslashes($meldung->ort);
			$lg_content .= "</td><td>" . $meldung->telefon;
			$lg_content .= "</td><td>" . $meldung->mobil;
			$meldungen_2 = $wpdb->get_row("SELECT SUM(bau) AS bau, SUM(wt) AS wt, SUM(norm) AS norm, SUM(sw) AS sw, SUM(ra) AS ra, SUM(zs) AS zs FROM " . $wpdb->prefix . "lgrichternummer WHERE richterid LIKE '" . $meldung->id . "'");
			$lg_content .= "</td><td>";
			if ($meldungen_2->norm > 0) {
				$lg_content .= 'x';
			}
			$lg_content .= "</td><td>";
			if ($meldungen_2->bau > 0) {
				$lg_content .= 'x';
			}
			$lg_content .= "</td><td>";
			if ($meldungen_2->sw > 0) {
				$lg_content .= 'x';
			}
			$lg_content .= "</td><td>";
			if ($meldungen_2->wt > 0) {
				$lg_content .= 'x';
			}
			//$lg_content.= "</td><td>".$meldung->hund_wurftag;
			//if ($meldung->zahlung == 'J'){
			//	$lg_content.="</td><td>X";
			//}else{
			//	$lg_content.="</td><td>O";
			//}
			$lg_content .= "</td><td><input type='submit' value='edit'>";
			$lg_content .= "</td></form></tr>";
		}
		$lg_content .= '
<tr>
<td colspan=10><p>';
		$user = wp_get_current_user();
		$allowed_roles = array(get_option('lg_kalender_settings_allowRichterNew', ''));
		if (array_intersect($allowed_roles, $user->roles)) {
			$lg_content .= '<a href="' . $current_url . '?insert=new" class="button">neu anlegen</a>';
		}
		$lg_content .= '</p></td>
<td><p><a href="' . $current_url . '?downloadpdf=richter" class="button" target="_blank">drucken</a></p></td>
</tr>
</table>
</div>
</div>';
	}
	if (is_user_logged_in()) {
		return $lg_content;
	} else {
		return '';
	}
}
