<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'enginequests' ) ) access_error();

$lists = loadlists();

if(isset($_REQUEST['new']) || isset($_REQUEST['edit'])) {
	$herstellungsids = $MySQL->fetch_complete("SELECT her_herstellungsid, name_interface FROM server.herstellung ORDER BY `name_interface` ASC");
	$Smarty->assign('herstellungsids', $herstellungsids);
}

if( isset( $_REQUEST['new'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'list_id', 'herstellungsid', 'amount', 'nimm' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	    else {
	    	$res = $MySQL->query_first("SELECT COUNT(*) FROM server.questbausteine_herstellung WHERE questbausteine_id=".$_REQUEST['list_id']." AND herstellungsid=".$_REQUEST['herstellungsid']);
	    	if($res['COUNT(*)'] > 0) {
	    		$form_errors[] = form_error( 'duplicate_qbaustein_herstellung' );
	    	}
	    }
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'list_id', 'herstellungsid', 'amount', 'nimm' );
			
			$page['content'] = $Smarty->fetch( 'qbausteine_new.tpl' );
	
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("INSERT INTO server.questbausteine_herstellung
	        		(questbausteine_id, herstellungsid, amount, nimm)
	        	VALUES
	        		(".$_REQUEST['list_id'].",
	        		".$_REQUEST['herstellungsid'].",
	        		'".$_REQUEST['amount']."',
					'".$_REQUEST['nimm']."')");
	    }
	}
	
	$page['content'] = $Smarty->fetch( 'qbausteine_new.tpl' );
	
    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
	die();
}

if( isset( $_REQUEST['newlist'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'name', 'aktion', 'trigger', 'trigger_satz' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'name', 'vorgaenger', 'aktion', 'trigger', 'trigger_satz', 'vorgaenger_nicht_erfuellt_satz', 'gib_items_nicht_vorhanden_satz' );
			
			$page['content'] = $Smarty->fetch( 'qbausteine_newlist.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
			$vorgaenger = empty($_REQUEST['vorgaenger']) ? 'NULL' : $_REQUEST['vorgaenger'];
			$vorgaenger_nicht_erfuellt_satz = empty($_REQUEST['vorgaenger_nicht_erfuellt_satz']) ? 'NULL' : "'".$_REQUEST['vorgaenger_nicht_erfuellt_satz']."'";
			$gib_items_nicht_vorhanden_satz = empty($_REQUEST['gib_items_nicht_vorhanden_satz']) ? 'NULL' : "'".$_REQUEST['gib_items_nicht_vorhanden_satz']."'";
			
	        $MySQL->query("INSERT INTO server.questbausteine
	        		(name, vorgaenger, aktion, `trigger`, `trigger_satz`, vorgaenger_nicht_erfuellt_satz, gib_items_nicht_vorhanden_satz)
	        	VALUES
	        		('".$_REQUEST['name']."',
	        		".$vorgaenger.",
					'".$_REQUEST['aktion']."',
					'".$_REQUEST['trigger']."',
					'".$_REQUEST['trigger_satz']."',
					".$vorgaenger_nicht_erfuellt_satz.",
					".$gib_items_nicht_vorhanden_satz.")");
	        echo mysql_error();
	        $lists = loadlists();
	    }
	} else {
		$page['content'] = $Smarty->fetch( 'qbausteine_newlist.tpl' );
		
	    $Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

if( isset( $_REQUEST['del'] ) )
{
    $MySQL->query( "DELETE FROM server.questbausteine_herstellung WHERE herstellungsid = ".intval( $_REQUEST['del'] )." AND questbausteine_id = ".intval( $_REQUEST['list'] ) );
}

if( isset( $_REQUEST['dellist'] ) )
{
    $MySQL->query( "DELETE FROM server.questbausteine_herstellung WHERE questbausteine_id = ".intval( $_REQUEST['dellist'] ) );	
	$MySQL->query( "DELETE FROM server.questbausteine WHERE id = ".intval( $_REQUEST['dellist'] ) );
	$lists = loadlists();
}

if( isset( $_REQUEST['edit'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'list_id', 'herstellungsid', 'amount', 'nimm' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'list_id', 'herstellungsid', 'amount', 'nimm' );
	    	
	    	$page['content'] = $Smarty->fetch( 'qbausteine_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("UPDATE server.questbausteine_herstellung
	        	SET
	        		questbausteine_id='".$_REQUEST['list_id']."',
	        		herstellungsid='".$_REQUEST['herstellungsid']."',
	        		amount='".$_REQUEST['amount']."',
	        		nimm='".$_REQUEST['nimm']."'
	        	WHERE 
	        		 herstellungsid = ".intval( $_REQUEST['edit'] )."
	        		 AND questbausteine_id = ".intval( $_REQUEST['list'] ));
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( "SELECT * FROM server.questbausteine_herstellung WHERE herstellungsid = ".intval( $_REQUEST['edit'] )." AND questbausteine_id = ".intval( $_REQUEST['list'] ) );
		$formvalues['list'] = $_REQUEST['list'];
		$formvalues['edit'] = $_REQUEST['edit'];
		$formvalues['list_id'] = $formvalues['questbausteine_id'];
		$formvalues['herstellungsid'] = $formvalues['herstellungsid'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'qbausteine_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

if( isset( $_REQUEST['editlist'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'name', 'aktion', 'trigger', 'trigger_satz' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'name', 'vorgaenger', 'aktion', 'trigger', 'trigger_satz', 'vorgaenger_nicht_erfuellt_satz', 'gib_items_nicht_vorhanden_satz' );
	    	
	    	$page['content'] = $Smarty->fetch( 'qbausteine_editlist.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
			$vorgaenger = empty($_REQUEST['vorgaenger']) ? 'NULL' : $_REQUEST['vorgaenger'];
			$vorgaenger_nicht_erfuellt_satz = empty($_REQUEST['vorgaenger_nicht_erfuellt_satz']) ? 'NULL' : "'".$_REQUEST['vorgaenger_nicht_erfuellt_satz']."'";
			$gib_items_nicht_vorhanden_satz = empty($_REQUEST['gib_items_nicht_vorhanden_satz']) ? 'NULL' : "'".$_REQUEST['gib_items_nicht_vorhanden_satz']."'";
							
	        $MySQL->query("UPDATE server.questbausteine
	        	SET
	        		name='".$_REQUEST['name']."',
	        		vorgaenger=".$vorgaenger.",
					aktion='".$_REQUEST['aktion']."',
					`trigger`='".$_REQUEST['trigger']."',
					`trigger_satz`='".$_REQUEST['trigger_satz']."',
					vorgaenger_nicht_erfuellt_satz=".$vorgaenger_nicht_erfuellt_satz.",
					gib_items_nicht_vorhanden_satz=".$gib_items_nicht_vorhanden_satz."
	        		
	        	WHERE 
	        		 id = ".intval( $_REQUEST['editlist'] ));
	        
	        $lists = loadlists();
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( "SELECT * FROM server.questbausteine WHERE id = ".intval( $_REQUEST['editlist'] ) );
		$formvalues['editlist'] = $_REQUEST['editlist'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'qbausteine_editlist.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

$list_id = $lists[0][0];
if(isset($_REQUEST['list_id'])) {
	$list_id = intval($_REQUEST['list_id']);
	$formvalues = array('list_id' => $list_id);
	$Smarty->assign('formvalues', $formvalues);
}

if(!empty($list_id)) {
	$entries = $MySQL->fetch_complete( "
		SELECT 
			server.questbausteine_herstellung.*, 
			server.herstellung.name_interface AS name
		FROM server.questbausteine_herstellung
		JOIN server.herstellung ON(server.questbausteine_herstellung.herstellungsid=server.herstellung.her_herstellungsid)
		WHERE questbausteine_id=".$list_id."
		ORDER BY amount DESC" );
}

$Smarty->assign('entries', $entries);

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

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

function loadlists() {
	global $MySQL, $Smarty;
	
	$lists = $MySQL->fetch_complete("SELECT id, name FROM server.questbausteine ORDER BY `name` ASC");
	$Smarty->assign('lists', $lists);
	
	return $lists;
}

?>