<?php
///////////////////////////////////////////////////////////////////////////////////////
// PHPizabi 0.844b C1 [ALICIA]                               http://www.phpizabi.net //
///////////////////////////////////////////////////////////////////////////////////////
// Please read the license.txt file before using / modifying this software           //
// Original author:         Claude Desjardins, R - feedback@realitymedias.com        //
// Last modification date:  August 26th 2006                                         //
// Version:                 PHPizabi 0.844b C1                                       //
//                                                                                   //
// (C) 2005, 2006 Real!ty Medias / PHPizabi - All rights reserved                    //
///////////////////////////////////////////////////////////////////////////////////////

	/* Check Structure Availability */
	if (!defined("CORE_STRAP")) die("Out of structure call");
	
	// TEMPLATE HANDLING ////////////////////////////////////////////////////////////////// FINAL //
	/*
		Initialize the template engine and
		load the template file. Get objects,
		convert self user.
	*/
	$tpl = new template;
	$tpl -> Load("create");
	$tpl -> GetObjects();
	
	
	if (isset($_POST["Submit"], $_POST["topic"], $_POST["body"]) and $_POST["topic"] != "" and $_POST["body"] != "") {

		if (isset($_POST["opponent"]) and $_POST["opponent"] != "") {

			$opponent = myF(myQ("SELECT `id` FROM `[x]users` WHERE LCASE(`username`) = '".strtolower($_POST["opponent"])."'"));
			if ($opponent["id"] > 0) {
			
				myQ("
					INSERT INTO `[x]fight`
					(`user`,`opponent`,`date`,`duration`,`topic`,`body`,`votes`)
					VALUES (
						'".me("id")."',
						'{$opponent["id"]}',
						'".time()."',
						'{$_POST["duration"]}',
						'{$_POST["topic"]}',
						'{$_POST["body"]}',
						'".pk(array(me("id"),$opponent["id"]))."'
					)
				");
				
				$mail_replace = array(
					"{me}" => me("username"), 
					"{url}" => "http://".$_SERVER['HTTP_HOST'].str_replace("/index.php", NULL, $_SERVER['PHP_SELF'])."?L=fight.request&id=".mysql_insert_id()
				);
				
				myQ("
					INSERT INTO `[x]messages`
					(`to`,`from`,`subject`,`body`,`box`,`date`)
					VALUES (
						'{$opponent["id"]}',
						'".me("id")."',
						'".strtr($GLOBALS["OBJ"]["mail_subject"], $mail_replace)."',
						'".strtr($GLOBALS["OBJ"]["mail_body"], $mail_replace)."',
						'{$CONF["MAILS_INBOX_NAME"]}',
						'".time()."'
					)
				");

				$tpl -> Zone("fight_form", "success");
				reload(2, "?L=fight.index");

			}
			
			else {
				
				$tpl -> Zone("fight_form", "form");
				$error_message = $GLOBALS["OBJ"]["no_user"];
				
			}
		
		}
		
		elseif (!isset($_POST["opponent"]) or $_POST["opponent"] == "") {
			
			myQ("
				INSERT INTO `[x]fight`
				(`user`,`date`,`duration`,`topic`,`body`,`votes`)
				VALUES (
					'".me("id")."',
					'".time()."',
					'{$_POST["duration"]}',
					'{$_POST["topic"]}',
					'{$_POST["body"]}',
					'".pk(array(me("id")))."'
				)
			");
			
			$tpl -> Zone("fight_form", "success");
			reload(2, "?L=fight.index");
		}
	}
	
	else $tpl -> Zone("fight_form", "form");
	
	$tpl -> AssignArray(array(
		"error.message" => (isset($error_message)?$error_message:NULL),
		"post.topic" => (isset($_POST["topic"]) ? $_POST["topic"] : NULL),
		"post.body" => (isset($_POST["body"]) ? $_POST["body"] : NULL),
	));
	
	
	$tpl -> Flush();
	
?>