<?php

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


// TODO: Zugriffsrechte prüfen
if(!$user->can_access('viewchars')) access_error();

if(isset($_REQUEST['startindex'])===false) {
	$startindex = 0;
} else {
	$startindex = $_REQUEST['startindex'];
}

// Zusatzfunktion für Elinweth
if(isset($_REQUEST['mv'])) $Smarty->assign('mv', true);
if($user->can_access('activation') && isset($_REQUEST['mvc']) && $_REQUEST['mvc'] == "true") {
	// Masseneintrag soll stattfinden
	Print_R($_REQUEST);
	die();
}

$op1 = 'charid';
$op1v = '';
$op2 = 'charname';
$op2v = '';
$op3 = 'accountid';
$op3v = '';
$op4 = 'accountname';
$op4v = '';
$testserver = 0;
$deleted = 0;
$chartype = '';
$volk = -1;
$geschlecht = -1;
$aktiviert = 1;
$sort = 'charname';
$hatProfession = -1;
$records = 50;
$testserver = 0;

if(isset($_REQUEST['action']) AND $_REQUEST['action'] == 'query' AND isset($_REQUEST['Maske_leeren']) === false) {
	// sql-Query zusammenbasteln

	if(isset($_REQUEST['op1'])===true) $op1 = $_REQUEST['op1'];
	if(isset($_REQUEST['op1v'])===true) $op1v = $_REQUEST['op1v'];
	if(isset($_REQUEST['op2'])===true) $op2 = $_REQUEST['op2'];
	if(isset($_REQUEST['op2v'])===true) $op2v = $_REQUEST['op2v'];
	if(isset($_REQUEST['op3'])===true) $op3 = $_REQUEST['op3'];
	if(isset($_REQUEST['op3v'])===true) $op3v = $_REQUEST['op3v'];
	if(isset($_REQUEST['op4'])===true) $op4 = $_REQUEST['op4'];
	if(isset($_REQUEST['op4v'])===true) $op4v = $_REQUEST['op4v'];
	if(isset($_REQUEST['testserver'])===true) $testserver = $_REQUEST['testserver'];
	if(isset($_REQUEST['accesslevel'])===true) $accesslevel = $_REQUEST['accesslevel'];
	if(isset($_REQUEST['chartype'])===true) $chartype = $_REQUEST['chartype'];
	if(isset($_REQUEST['volk'])===true) $volk = $_REQUEST['volk'];
	if(isset($_REQUEST['geschlecht'])===true) $geschlecht = $_REQUEST['geschlecht'];
	if(isset($_REQUEST['aktiviert'])===true) $aktiviert = $_REQUEST['aktiviert'];
	if(isset($_REQUEST['hatProfession'])===true) $hatProfession = $_REQUEST['hatProfession'];
	if(isset($_REQUEST['sort'])===true) $sort  = $_REQUEST['sort'];
	if(isset($_REQUEST['records'])===true) $records = intval($_REQUEST['records']);
	
	switch ($records) {
		case 25:
		case 50:
		case 100:
		case 250:
			$limit = sprintf("LIMIT %d, %d ", intval($startindex), intval($records));
			break;
		default:
			$limit = sprintf("LIMIT %d, %d ", intval($startindex),2);
	}

	$where = '';
	for($i=1;$i<=4;$i++) {
		if(isset($_REQUEST['op'.$i.'v']) && strlen($_REQUEST['op'.$i.'v'])>0) {
			if($_REQUEST['op'.$i] == "charid") {
				$where.= sprintf("charid = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "charname") {
				$where.= sprintf("name LIKE '%s' AND ", me_escape($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "accountname") {
				list($accountid) = $MySQL->query_first(sprintf("SELECT accountid FROM server.accounts WHERE name LIKE '%s'", me_escape($_REQUEST['op'.$i.'v'])));
				$where.= sprintf("accountID IN (SELECT accountid FROM server.accounts WHERE name LIKE '%s') AND ", me_escape($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "accountid") {
				$where.= sprintf("accountID = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "level") {
				$where.= sprintf("level = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "leben") {
				$where.= sprintf("leben = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "alter") {
				$where.= sprintf("rp_alter = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "dyn_profil") {
				$where.= sprintf("dyn_profil LIKE '%s' AND ", me_escape($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "fix_profil") {
				$where.= sprintf("fix_profil LIKE '%s' AND ", me_escape($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "charstory") {
				$where.= sprintf("charstory LIKE '%s' AND ", me_escape($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "prof_1") {
				$where.= sprintf("profession_1 = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "prof_1_s") {
				$where.= sprintf("professionsstufe_1 = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "prof_2") {
				$where.= sprintf("profession_2 = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "prof_2_s") {
				$where.= sprintf("professionsstufe_2 = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "prof_3") {
				$where.= sprintf("profession_3 = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "prof_3_s") {
				$where.= sprintf("professionsstufe_3 = %d AND ", intval($_REQUEST['op'.$i.'v']));
			} elseif($_REQUEST['op'.$i] == "blocked") {
				$where.= "block = 1 AND ";
			} elseif($_REQUEST['op'.$i] == "jailed") {
				$where.= "jailed = 1 AND ";
			} elseif($_REQUEST['op'.$i] == "lc_from") {
				// $where.= "lastchanged >= '".mktime(."' AND ";
			} elseif($_REQUEST['op'.$i] == "lc_to") {
				
			} elseif($_REQUEST['op'.$i] == "ll_from") {
				
			} elseif($_REQUEST['op'.$i] == "ll_to") {
				
			} elseif($_REQUEST['op'.$i] == "aktiviert_id") {
				$where.= sprintf("aktiviert_von = %d AND ", intval($_REQUEST['op'.$i]));
			} elseif($_REQUEST['op'.$i] == "aktiviert_name") {
				list($temp) = $MySQL->query_first(sprintf("SELECT COUNT(*) FROM server.accounts WHERE name LIKE '%s'", me_escape($_REQUEST['op'.$i.'v'])));
				if($temp > 0) $msg = "Die Suche nach Freischaltern mit '".$_REQUEST['op'.$i.'v']."' war nicht eindeutig und hat $temp Treffer ergeben. Bitte prüfen!";
				$where.= sprintf("aktiviert_von IN (SELECT accountid FROM server.accounts WHERE name LIKE '%s') AND ", me_escape($_REQUEST['op'.$i.'v']));
			}	
		}
	}
	


	switch($geschlecht) {
		case 'm':
			$where.= "geschlecht = 'm' AND ";
			break;
		case 'w':
			$where.= "geschlecht = 'w' AND ";
			break;
	}
	
	if($volk != -1) $where.= sprintf("volk = %d AND ", intval($volk));
	
	switch($aktiviert) {
		case 0:
			$where.= "aktiviert = 0 AND ";
			break;
		case 9:
			$where.= "aktiviert = 9 AND ";
			break;
		case 2:
			$where.= "aktiviert = 2 AND ";
			break;
		case 3:
			$where.= "aktiviert = 3 AND ";
			break;
		case 1:
			$where.= "aktiviert = 1 AND ";
			break;
	}
	
	switch($chartype) {
		case 'staff':
			$where.="chartype = 'staff' AND ";
			break;
		case 'quest':
			if($user->can_access('questcharadministration')) {
				$where.="chartype = 'quest' AND ";
			}
			break;
		case 'player':
			$where.="chartype = 'player' AND ";
			break;
	}
	
	if($hatProfession != -1) {
		$where.= sprintf("(profession_1 = %d OR profession_2 = %d OR profession_3 = %d) AND ", intval($hatProfession), intval($hatProfession), intval($hatProfession));
	}
	
	// bestimmte Chars aus der Liste ausfiltern
	if($user->can_access('questcharadministration')===false) {
		// Benutzer darf keine Questchars sehen, außer seinen eigenen
		$where.=sprintf("(chartype != 'quest' OR accountid != %d) AND ",$user->ID);
	}
	
	
	if($testserver==1) {
		$where.="testserver=1 ";
	} elseif($testserver==2) {
		$where.="testserver=2 ";
	} else {
		$where.="testserver=0 ";
	}

	
	
	$additional = '';
		$order_by = "ORDER BY ";
	// Sortierung
	if($sort == "charid") {
		$order_by.="charid,";
	} elseif($sort == "charname") {
		$order_by.="name,";
	} elseif($sort == "accountname") {
		$order_by.="accountname,";
	} elseif($sort == "accountid") {
		$order_by.="accountid,";
	} elseif($sort == "level") {
		$order_by.="level,";
	} elseif($sort == "leben") {
		$order_by.="leben,";
	} elseif($sort == "alter") {
		$order_by.="rp_alter,";
	} elseif($sort == "prof_1") {
		$order_by.="profession_1,";
	} elseif($sort == "prof_1_s") {
		$order_by.="professionsstufe_1,";
	} elseif($sort == "prof_2") {
		$order_by.="profession_2,";
	} elseif($sort == "prof_2_s") {
		$order_by.="professionsstufe_2,";
	} elseif($sort == "prof_3") {
		$order_by.="profession_3 = %d,";
	} elseif($sort == "prof_3_s") {
		$order_by.="professionsstufe_3,";
	} elseif($sort == "blocked") {
		$order_by.="block,";
	} elseif($sort == "jailed") {
		$order_by.="jailed,";
	} elseif($sort == "lc") {
		$order_by.="lastchange,";
	} elseif($sort == "ll") {
		// wird im Moment nicht vom Server gespeichert; Suche über die Logs zu aufwendig
	} elseif($sort == "aktiviert_id") {
		$order_by.="aktiviert_von,";
	} elseif($sort == "aktiviert_name") {
		// deaktiviert wegen MySQL-Überlastung; seltsam
		//$order_by.="aktiviertname,";
	} elseif($sort == "volk") {
		$order_by.="volk,";
	} elseif($sort == "geschlecht") {
		$order_by.="geschlecht,";
	} elseif($sort == "profstufen") {
		// na geht so nicht
		//$additional = ", MAX(professionsstufe_1,professionsstufe_2,professionsstufe_3) as profmax";
		//$order_by.="profmax,";
	} elseif($sort == "vergausb") {
		$order_by.="vergebene_ausbilderpunkte DESC,";
	} elseif($sort == "verfausb") {
		$order_by.="verfuegbare_ausbilderpunkte DESC,";
	} elseif($sort == "xp") {
		$order_by.="xp,";
	} elseif($sort == "fp") {
		$order_by.="forschungspunkte,";
	}
	$order_by.=" name";
	
	//$sql = "SELECT *, (SELECT name FROM server.accounts WHERE accountID = server.chars.accountID) as accountname, (SELECT name FROM server.accounts WHERE aktiviert_von = server.chars.accountID) as aktiviertname FROM server.chars WHERE $where $order_by $limit";
	$sql = "SELECT SQL_CALC_FOUND_ROWS *, (SELECT name FROM server.accounts WHERE accountID = server.chars.accountID) as accountname$additional FROM server.chars WHERE $where $order_by $limit";
	if(isset($_REQUEST['debug']) && $user->rank >= 5) echo $sql;
	$chars = $MySQL->fetch_complete($sql);
	list($count) = $MySQL->query_first("SELECT FOUND_ROWS()");
	$Smarty->assign('chars', $chars);
	$Smarty->assign('count', $count);	
	
	$Smarty->assign('canActivate', $user->can_access('activation'));
	$Smarty->assign('canQuestchars', $user->can_access('questcharadministration'));
	$Smarty->assign('canAdministrate', $user->can_access('charadministration'));
	
	$browse = '';
	if($count > $records && $records >0) {
		// generiere Blätterfunktion	
		for($i=1;$i<=$count/$records;$i++) {
			$pos = strpos($_SERVER['QUERY_STRING'], '&startindex');
			if($pos != false) {
				$querystring = substr($_SERVER['QUERY_STRING'],0,$pos);
			} else {
				$querystring = $_SERVER['QUERY_STRING'];
			}
			$browse .= " <a href=\"?".$querystring."&startindex=".($i-1)*$records."\">$i</a>&nbsp;";
		}
		$Smarty->assign('browse', $browse);
	}
}

$Smarty->assign('op1', $op1);
$Smarty->assign('op1v', $op1v);
$Smarty->assign('op2', $op2);
$Smarty->assign('op2v', $op2v);
$Smarty->assign('op3', $op3);
$Smarty->assign('op3v', $op3v);
$Smarty->assign('op4', $op4);
$Smarty->assign('op4v', $op4v);
$Smarty->assign('testserver', $testserver);
$Smarty->assign('chartype', $chartype);
$Smarty->assign('volk', $volk);
$Smarty->assign('geschlecht', $geschlecht);
$Smarty->assign('aktiviert', $aktiviert);
$Smarty->assign('records', $records);
$Smarty->assign('hatProfession', $hatProfession);
$Smarty->assign('sort', $sort);

if(isset($msg)) $Smarty->assign('msg', $msg);


$options = array("charid" => "Charakterid", "charname" => "Charaktername", "accountname" => "Accountname",
	"accountid" => "Accountid", "level" => "Level", "leben" => "Leben", "alter" => "Alter",
	"dyn_profil" => "Dyn. Profil", "fix_profil" => "Fix. Profil", "charstory" => "Charaktergeschichte",
	"prof_1" => "Profession I (id)", "prof_1_s" => "Stufe Profession I", 
	"prof_2" => "Profession II (id)", "prof_2_s" => "Stufe Profession II", 
	"prof_3" => "Profession III (id)", "prof_3_s" => "Stufe Profession III", 
	"blocked" => "Blocked (-)", "jailed" => "Jailed (-)",
//	"lc_from" => "Letzte Änderung von", "lc_to" => "Letzte Änderung bis",
//	"ll_from" => "Letzter Login von", "ll_to" => "Letzter Login bis",
	"aktiviert_id" => "aktiviert von (id)", "aktiviert_name" => "aktiviert von (Name)");
$Smarty->assign('options', $options);

// Völkerliste
$voelker = $MySQL->fetch_complete("SELECT ID, Bezeichnung FROM server.voelker ORDER BY Rasse, Bezeichnung" );
$Smarty->assign('voelker', array_merge(array(array(0 => '-1', 1 => 'egal')), $voelker));

// Professionsliste
$professionen = $MySQL->fetch_complete("SELECT id, concat(bezeichnung, ' (', id, ')') FROM server.professionen WHERE id != 0 ORDER BY Bezeichnung" );
$Smarty->assign('professionen', array_merge(array(array(0 => '-1', 1 => 'egal')), $professionen));

$sortDropdown = array("charid" => "Charakterid", "charname" => "Charaktername", "accountname" => "Accountname",
	"accountid" => "Accountid", "level" => "Level", "leben" => "Leben", "alter" => "Alter",
	"prof_1" => "Profession I (id)", "prof_1_s" => "Stufe Profession I", 
	"prof_2" => "Profession II (id)", "prof_2_s" => "Stufe Profession II", 
	"prof_3" => "Profession III (id)", "prof_3_s" => "Stufe Profession III", 
	"blocked" => "Blocked (-)", "jailed" => "Jailed (-)",
	"lc" => "Letzte Änderung",
// "ll" => "Letzter Login",
	"aktiviert_id" => "aktiviert von (id)",
//	"aktiviert_name" => "aktiviert von (Name)",
	"volk" => "Volk", "geschlecht" => "Geschlecht", 
	//"profstufen" => "Professionsstufen",
	"vergausb" => "vergebene Ausb.punkte", "verfausb" => "verfügbare Ausb.punkte",
	"xp" => "Erfahrungspunkte", "fp" => "Forschungspunkte");
$Smarty->assign('sortDropdown', $sortDropdown);

// Charakterliste anzeigen
$Smarty->assign('page_title', "Charsuchfunktion");
$Smarty->assign('page_title_long', "Charsuchfunktion");
$Smarty->assign('page_title_site', "Charsuchfunktion");
$page['content'] = $Smarty->fetch('char_list.tpl');

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

	


?>