<?php

require_once('./core.php');
if( !isset( $user )) access_error();

$charid = intval($_REQUEST['charid']);
$char = $MySQL->query_first(sprintf("SELECT * FROM server.chars WHERE charid = %d", $charid));

if(($char['accountid'] != $user->ID && $user->can_access('charadministration') == false) || ($user->can_access('questcharadministration')==false && $char['chartype'] == "quest")) {
	error('Zugriff verweigert!', true);
}
$Smarty->assign('canAdministrate', $user->can_access('charadministration'));

if(isset($char['name'])===false) {
	error('Char wurde nicht gefunden', true);
}	

require_once('lib/char.cls.php');
try {
	$char = new Char((int)$_REQUEST['charid'], (int)$char['accountid'], false, (bool)$char['testserver'], true);
	
} catch(IException $e) {
	// TODO
	echo $e->getNotice();
	die();
}

//===========================

		
		// ========================== 1:1 aus char.php; TODO irgendwann mal besser =======================
		// hole Charakterdaten aus dem Objekt		
		$reflection = new ReflectionClass($char);
		$defaults = $reflection->getdefaultProperties();
		
		foreach($defaults as $prop_name => $propvalue) {
			$char_out[$prop_name] = $char->get($prop_name);
		}
	
		// Werte mit Text-Informationen ergänzen
		list ($char_out['volk']) = $MySQL->query_first(sprintf("SELECT bezeichnung FROM server.voelker WHERE id = %d", $char_out['volk']));
		
		// Erstelle Liste für Professionen
		$professionsstufe = array("Anfänger", "Lehrling", "Geselle", "Meister", "Großmeister");
		$char_out['professionen'] = '';
		for($i=1;$i<=3;$i++) {
			if(isset($char_out['profession_'.$i]['id']) AND $char_out['profession_'.$i]['stufe']>0) {
				list ($profession) = $MySQL->query_first(sprintf("SELECT bezeichnung FROM server.professionen WHERE id = %d", $char_out['profession_'.$i]['id']));
				$char_out['professionen'] .= $profession." (".$professionsstufe[$char_out['profession_'.$i]['stufe']].")<br />";
			} else break;
		}
		
		
		// Werte für Statlimits einfügen
		foreach($char_out['stats'] as $stat => $value) {
			$stats_out[$stat]['bezeichnung'] = $stat;
			$stats_out[$stat]['min'] = $char_out['min_stats'][$stat];
			$stats_out[$stat]['wert'] = $value;
			$stats_out[$stat]['max'] = $char_out['max_stats'][$stat];
			
			$stats_out[$stat]['a'] = 4*$stats_out[$stat]['min']; 
			$stats_out[$stat]['b'] = 4*($stats_out[$stat]['wert']-$stats_out[$stat]['min']); 
			$stats_out[$stat]['c'] = 4*($stats_out[$stat]['max']-$stats_out[$stat]['wert']);
			$stats_out[$stat]['d'] = 4*(100-$stats_out[$stat]['max']);
		}
		
		// Werte für Skilllimits einfügen
		foreach($char_out['skills'] as $skill => $value) {
			$skills_out[$skill]['bezeichnung'] = $skill;
			$skills_out[$skill]['min'] = $char_out['min_skills'][$skill];
			$skills_out[$skill]['wert'] = $value;
			$skills_out[$skill]['max'] = $char_out['max_skills'][$skill];
			
			$skills_out[$skill]['a'] = 20*$skills_out[$skill]['min']; 
			$skills_out[$skill]['b'] = 20*($skills_out[$skill]['wert']-$skills_out[$skill]['min']); 
			$skills_out[$skill]['c'] = 20*($skills_out[$skill]['max']-$skills_out[$skill]['wert']);
			$skills_out[$skill]['d'] = 20*(20-$skills_out[$skill]['max']);
		}
		
		// ENDE====================== 1:1 aus char.php; TODO irgendwann mal besser ===================ENDE
		
		// Hübsche Bildchen für Charstorytheft-Detector
		if($char_out['cst'] > 5) {
			for($i=1;$i<=$char_out['cst'];$i++) {
				$char_out['cst_images'] .= "<img src=\"images/error.png\" width=\"16\" height=\"16\" title=\"Wahrscheinlich Geklaut\" alt=\"OK\" />";
			}
		}
		elseif($char_out['cst'] > 1) {
			for($i=1;$i<=$char_out['cst'];$i++) {
				$char_out['cst_images'] .= "<img src=\"images/warning.png\" width=\"16\" height=\"16\" title=\"Vermutlich Geklaut\" alt=\"OK\" />";
			}
		}
		else {
			for($i=0;$i<=$char_out['cst'];$i++) {
				$char_out['cst_images'] = "<img src=\"images/ok.png\" width=\"16\" height=\"16\" title=\"Vermutlich in Ordnung\" alt=\"OK\" />";
			}
		}
		
		// Accountname Charbesitzer holen
		list($char_out['account_name']) = $MySQL->query_first(sprintf("SELECT name FROM server.accounts WHERE accountid =%d", $char_out['accountid']));
		
		// Accountname Zurücksteller
		if($char_out['aktiviert_von']>0)
			list($char_out['zuruecksteller']) = $MySQL->query_first(sprintf("SELECT name FROM server.accounts WHERE accountid =%d", $char_out['aktiviert_von']));
		
		
		// Fälle holen
		list($char_out['filed']) =  $MySQL->query_first(sprintf("SELECT COUNT(*) FROM server.cases WHERE accountid  =%d AND case_type='Activation'", $char_out['accountid']));
		
		$Smarty->assign('page_title', "Charfreischaltung &raquo; Char freischalten &raquo; ".$char_out['name']);
		$Smarty->assign('page_title_long', "Charfreischaltung &raquo; Char freischalten &raquo; ".$char_out['name']);
		$Smarty->assign('page_title_site', "Char freischalten '".$char_out['name']."'");
		$Smarty->assign('clength', strlen($char_out['charstory']));
		
		$Smarty->assign('char', $char_out);
		$Smarty->assign('stats', $stats_out);
		$Smarty->assign('skills', $skills_out);
		$Smarty->assign('clength', strlen($char_out['charstory']));
		
		if(isset($_REQUEST['aktivierungskommentar'])) $Smarty->assign('aktivierungskommentar', $_REQUEST['aktivierungskommentar']);
		if(isset($_REQUEST['short_report'])) $Smarty->assign('short_report', $_REQUEST['short_report']);
		
		$page['content'] = $Smarty->fetch('char_display.tpl');


//===========================
$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

/*
// ========================== 1:1 aus char.php; TODO irgendwann mal besser =======================
// hole Charakterdaten aus dem Objekt		
$reflection = new ReflectionClass($char);
$defaults = $reflection->getdefaultProperties();

foreach($defaults as $prop_name => $propvalue) {
	$char_out[$prop_name] = $char->get($prop_name);
}

// Werte mit Text-Informationen ergänzen
list ($char_out['volk']) = $MySQL->query_first(sprintf("SELECT bezeichnung FROM server.voelker WHERE id = %d", $char_out['volk']));

// Erstelle Liste für Professionen
$professionsstufe = array("Anfänger", "Lehrling", "Geselle", "Meister", "Großmeister");
$char_out['professionen'] = '';
for($i=1;$i<=3;$i++) {
	if(isset($char_out['profession_'.$i]['id']) AND $char_out['profession_'.$i]['stufe']>0) {
		list ($profession) = $MySQL->query_first(sprintf("SELECT bezeichnung FROM server.professionen WHERE id = %d", $char_out['profession_'.$i]['id']));
		$char_out['professionen'] .= $profession." (".$professionsstufe[$char_out['profession_'.$i]['stufe']].")<br />";
	} else break;
}


// Werte für Statlimits einfügen
foreach($char_out['stats'] as $stat => $value) {
	$stats_out[$stat]['bezeichnung'] = $stat;
	$stats_out[$stat]['min'] = $char_out['min_stats'][$stat];
	$stats_out[$stat]['wert'] = $value;
	$stats_out[$stat]['max'] = $char_out['max_stats'][$stat];
	
	$stats_out[$stat]['a'] = 4*$stats_out[$stat]['min']; 
	$stats_out[$stat]['b'] = 4*($stats_out[$stat]['wert']-$stats_out[$stat]['min']); 
	$stats_out[$stat]['c'] = 4*($stats_out[$stat]['max']-$stats_out[$stat]['wert']);
	$stats_out[$stat]['d'] = 4*(100-$stats_out[$stat]['max']);
}

// Werte für Skilllimits einfügen
foreach($char_out['skills'] as $skill => $value) {
	$skills_out[$skill]['bezeichnung'] = $skill;
	$skills_out[$skill]['min'] = $char_out['min_skills'][$skill];
	$skills_out[$skill]['wert'] = $value;
	$skills_out[$skill]['max'] = $char_out['max_skills'][$skill];
	
	$skills_out[$skill]['a'] = 20*$skills_out[$skill]['min']; 
	$skills_out[$skill]['b'] = 20*($skills_out[$skill]['wert']-$skills_out[$skill]['min']); 
	$skills_out[$skill]['c'] = 20*($skills_out[$skill]['max']-$skills_out[$skill]['wert']);
	$skills_out[$skill]['d'] = 20*(20-$skills_out[$skill]['max']);
}

// ENDE====================== 1:1 aus char.php; TODO irgendwann mal besser ===================ENDE

// Accountname Charbesitzer holen
list($char_out['account_name']) = $MySQL->query_first(sprintf("SELECT name FROM server.accounts WHERE accountid =%d", $char_out['accountid']));

$Smarty->assign('page_title', "Char ansehen &raquo; ".$char_out['name']);
$Smarty->assign('page_title_long', "Char ansehen &raquo; ".$char_out['name']);
$Smarty->assign('page_title_site', "Char ansehen '".$char_out['name']."'");
$Smarty->assign('clength', strlen($char_out['charstory']));

$Smarty->assign('char', $char_out);
$Smarty->assign('stats', $stats_out);
$Smarty->assign('skills', $skills_out);

$Smarty->assign('viewchar', true);
$page['content'] = $Smarty->fetch('char_display.tpl');
$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );
*/
?>