<?php

include './core.php';
include './lib/parse.class.php';

$voelker = $MySQL->fetch_complete("SELECT id, bezeichnung FROM server.voelker");
$professionen = $MySQL->fetch_complete("SELECT id, bezeichnung FROM server.professionen WHERE id > 0 ORDER BY bezeichnung");
$Smarty->assign('voelker', $voelker);
$Smarty->assign('professionen', $professionen);

if(isset($_REQUEST['csadmin'])) {
	if( !isset( $user ) || !$user->can_access( 'gamesys' ) ) access_error();
	
	$MySQL->query("START TRANSACTION;");
	
	if(isset($_REQUEST['save'])) {
		$MySQL->query("TRUNCATE TABLE server.anmeldestop");
		
		foreach($professionen as $prof) {
			foreach($voelker as $volk) {
				if(isset($_REQUEST['cs_'.$prof['id'].'_'.$volk['id']])) {
                $MySQL->query(sprintf("INSERT INTO server.anmeldestop (volk, profession, lastchanged, lastchangedby) VALUES (%d,%d, NOW(), %d)", intval($volk['id']), intval($prof['id']), $user->ID));
				}
			}
		}
	}
	
	$MySQL->query("COMMIT;");
	
	$combs = $MySQL->fetch_complete("SELECT * FROM server.anmeldestop");
	$combs_ = array();
		
	foreach($combs as $comb) {
		$combs_[$comb['volk']][$comb['profession']] = true;
	}
	
	$Smarty->assign('combs', $combs_);

	$page['content'] = $Smarty->fetch( 'charstop_admin.tpl' );
} else {
	if( !isset( $user )) access_error();
	
	$combs = $MySQL->fetch_complete("SELECT * FROM server.anmeldestop");
	$combs_ = array();
	foreach($combs as $comb) {
		$combs_[$comb['volk']][$comb['profession']] = true;
	}
	$Smarty->assign('combs', $combs_);

	$page['content'] = $Smarty->fetch( 'charstop.tpl' );
}

$Smarty->assign( 'page', $page );
$Smarty->display( 'main_light.tpl' );

?>