<?php

include './core.php';

// ### überprüfen, ob der User diese Liste sehen und ändern darf
if( !isset( $user ) || !$user->can_access( 'questitems_authorisieren' ) ) access_error();

// SQL Befehl zum holen von Items aus der Datenbank
$select1 = "
SELECT d.name AS authorisierender_gm_n, t3 . * 
FROM (
  SELECT c.name AS gm_n, t2 . * 
  FROM (
    SELECT b.name AS empfaenger_n, t1 . * 
    FROM (
      SELECT a.`name` AS lastchangedby_n, `quest_items`. * 
      FROM server.quest_items 
      LEFT JOIN server.accounts AS a ON quest_items.lastchangedby = a.accountid";
$select2 = "
    ) AS t1
    LEFT JOIN server.chars AS b ON t1.empfaenger = b.charid
  ) AS t2
  LEFT JOIN server.accounts AS c ON t2.gm = c.accountid
) AS t3
LEFT JOIN server.accounts AS d ON t3.authorisierender_gm = d.accountid";

// ----------------------------------------------- Item zum Bearbeiten ausgewählt
if( isset( $_REQUEST['cat_edit'] ) )
{
	$Smarty->assign( 'sort', $_REQUEST['sort'] );
	$ID = intval( $_REQUEST['cat_edit'] );
	$Smarty->assign( 'ID', $ID );

	$Smarty->assign( 'authorisieren', $user->can_access('questitems_authorisieren') ); // ### darf der User Authorisieren

	$qitems = $MySQL->fetch_complete( $select1." WHERE quest_items.questitemid = ".$ID.$select2 );
	$Smarty->assign( 'row_count', count( $qitems ) );
	$Smarty->assign( 'qitems', $qitems );
	
	$page['content'] = $Smarty->fetch( 'quest_items_edit.tpl' );
}
else
{
	// ------------------------------------------- Item speichern
	// INSERT, version
	if( isset( $_REQUEST['Save'] ) )
	{
	    $qu_str = "UPDATE server.quest_items SET";
		$qu_str .= ", lastchanged = NOW(), lastchangedby = '".$accountid."'";
		if( isset( $_REQUEST['erklaerung'] ) )
			{ $qu_str .= " `erklaerung`=\"".$_REQUEST['erklaerung']."\""; }
		if( isset( $_REQUEST['authorisieren'] ) )
		{
			$qu_str .= ", `authorisierender_gm` = '".$accountid."'";
			$qu_str .= ", `authorisierung_zeitpunkt` = NOW()";
		}
		if( isset( $_REQUEST['kommentar'] ) )
			{ $qu_str .= ", `kommentar`=\"".$_REQUEST['kommentar']."\""; }
		$qu_str .= " WHERE `questitemid`  = ".intval( $_REQUEST['questitemid'] );
		$MySQL->query( $qu_str );
	    $Smarty->assign( 'saved', true );
	}

	// ------------------------------------------- Item Löschen
	if( isset( $_REQUEST['delete_item'] ) )
	{
		$MySQL->query( "DELETE FROM server.quest_items WHERE `questitemid`  = ".
			intval( $_REQUEST['delete_item'] ) );
	
	    $Smarty->assign( 'deleted', true );
	}
	
	// ------------------------------------------- Liste aller Items aufbauen
	if( isset( $_REQUEST['sort'] ) ) // --- Sortierreinfolge
	{	$last_sort = $_REQUEST['sort']; }
	else
	{
		$last_sort = "name";
	}
	
	$Smarty->assign( 's_name', "name" );
	$Smarty->assign( 's_get', "Get" );
	$Smarty->assign( 's_gm', "GM" );
	$Smarty->assign( 's_time', "time" );
	$Smarty->assign( 's_author', "-author" );

	$Smarty->assign( 'last_sort', $last_sort );
    switch ($last_sort)
	{
	  case "name":
	    $sort = "name";
	    $Smarty->assign( 'sort_text', "aufsteigend nach Name" );
	    $Smarty->assign( 's_name', "-name" );
		break;
	  case "-name":
		$sort = "`name` DESC";
	    $Smarty->assign( 'sort_text', "absteigend nach Name" );
	    $Smarty->assign( 's_name', "name" );
		break;
	  case "Get":
		$sort = "`personGet_n`";
	    $Smarty->assign( 'sort_text', "aufsteigend nach Spieler" );
	    $Smarty->assign( 's_get', "-Get" );
		break;
	  case "-Get":
		$sort = "`personGet_n` DESC";
	    $Smarty->assign( 'sort_text', "absteigend nach Spieler" );
		break;
	  case "GM":
		$sort = "`GM_n`";
	    $Smarty->assign( 'sort_text', "aufsteigend nach Ausgebendem GM" );
	    $Smarty->assign( 's_gm', "-GM" );
		break;
	  case "-GM":
		$sort = "`GM_n` DESC";
	    $Smarty->assign( 'sort_text', "absteigend nach Ausgebendem GM" );
		break;
	  case "time":
		$sort = "`timestamp`";
	    $Smarty->assign( 'sort_text', "aufsteigend nach Ausgebezeitpunkt" );
	    $Smarty->assign( 's_time', "-time" );
		break;
	  case "-time":
		$sort = "`timestamp` DESC";
	    $Smarty->assign( 'sort_text', "absteigend nach Ausgebezeitpunkt" );
		break;
	  case "author":
		$sort = "`authorizeGM` DESC";
	    $Smarty->assign( 'sort_text', "ob genehmigt" );
		break;
	  case "-author":
		$sort = "`authorizeGM`";
	    $Smarty->assign( 'sort_text', "ob nicht genehmigt" );
	    $Smarty->assign( 's_author', "author" );
		break;
	}

    // --- Liste aus der Datenbank holen	
	$qitems = $MySQL->fetch_complete( $select1.$select2." ORDER BY $sort" );
	$Smarty->assign( 'row_count', count( $qitems ) );
	$Smarty->assign( 'qitems', $qitems );
	
	$page['content'] = $Smarty->fetch( 'quest_items.tpl' );
}
$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>