<?php
// wcf imports
require_once(WCF_DIR.'lib/form/RacingLeagueForm.class.php');

/**
 * Seite zum Erstellen, Editieren und Löschen von Renntagen
 * 
 * @author	Markus Gerdelmann
 * @copyright	2008 MDMAN
 * @license GNU
 * @package	de.mdman.racing.league
 */

class RacingLeagueRacesForm extends RacingLeagueForm {
 
	public $templateName = 'racingLeagueRaces';
	public $saisonID = 0;
	public $newRaces = 0;
	public $edit = 0;
	public $race = '';
	public $raceID = 0;
	public $racename = '';
	public $races = array();
	public $anz = 0;
	public $raceEditNo = 0;
	public $raceNo = 0;

	public $redirect = 'RacingLeagueRaces';
	
	/**
	 * @see Form::readParameters()
	 */
	public function readParameters() {
		parent::readParameters();

		if (isset($_REQUEST["saisonID"]))	$this->saisonID = intval($_REQUEST["saisonID"]);	
		if (isset($_REQUEST["newRaces"]))	$this->newRaces = intval($_REQUEST["newRaces"]);
		if (isset($_REQUEST["edit"]))		$this->edit = intval($_REQUEST["edit"]);
		if (isset($_REQUEST["raceID"]))		$this->raceID = intval($_REQUEST["raceID"]);
		if (isset($_REQUEST["raceNo"]))		$this->raceNo = intval($_REQUEST["raceNo"]);
	}
	
	/**
	 * @see Form::readFormParameters()
	 */
	public function readFormParameters() {
		parent::readFormParameters();
		
		if (isset($_POST["race"]))		$this->race = StringUtil::trim($_POST["race"]);
	}
	
	/**
	 * @see Form::validate()
	 */
	public function validate() {
		parent::validate();

		// race bei newRaces
		if ($this->newRaces == 1) {
			if (empty($this->race)) {
				$this->newRaces = '1';
				throw new UserInputException('race');
			}
		}
		// race bei EDIT
		if ($this->edit == 1) {
			if (empty($this->race)) {
				$this->edit = '1';
				throw new UserInputException('race');
			}
		}
		
		// raceNo
		if (empty($this->raceNo)) {
			throw new UserInputException('raceNo');
		}
	}

	/**
	 * @see Form::readData()
	 */
	public function readData() {
		parent::readData();

		// Hier wird die Anzahl der vorhandenen Renntage ermittelt
		if ($this->newRaces == 1) {

			$sql = "SELECT *
					FROM wcf".WCF_N."_racing_races
					WHERE saisonID = '".$this->activeSaisonID."'
					";
			$res = WCF::getDB()->sendQuery($sql);
			$rows = WCF::getDB()->countRows($res);
			$this->anz = intval($rows) + 1;
		}

		// Hier wird ein Rennen geladen das editiert werden soll
		if ($this->edit == 1) {

			$sql = "SELECT * 
					FROM wcf".WCF_N."_racing_races
					WHERE raceID = '".$this->raceID."'
					";
		
			$res = WCF::getDB()->sendQuery($sql);
		
			while ($row = WCF::getDB()->fetchArray($res)) {
				$this->racename = $row["racename"];
				$this->raceEditNo = $row["raceNo"];
			}
			
			$sql = "SELECT * 
					FROM wcf".WCF_N."_racing_races
					WHERE saisonID = '".$this->activeSaisonID."'
					";
			$res = WCF::getDB()->sendQuery($sql);
			$rows = WCF::getDB()->countRows($res);
			$this->anz = intval($rows) + 1;
		}
		
		//Hier werden alle Races geladen
		if ($this->activeSaisonID != 0) {
			$sql = "SELECT *
					FROM wcf".WCF_N."_racing_races
					WHERE saisonID = ".$this->activeSaisonID."
					ORDER BY raceNo ASC
					";

			$res = WCF::getDB()->sendQuery($sql);
			while ($row = WCF::getDB()->fetchArray($res)) {
				$this->races[] = $row;
			}			
		}	
	}
	
	/**
	 * @see Form::save()
	 */
	public function save() {
		parent::save();
		
		// Hier wird ein neuer Renntag gespeichert
		if ($this->race && $this->activeSaisonID != 0 && $this->newRaces == 1) {
			
			$sql = "INSERT INTO wcf".WCF_N."_racing_races (saisonID, raceID, raceNo, racename)
           			VALUES ('".escapeString($this->activeSaisonID)."', '', '".escapeString($this->raceNo)."', '".escapeString($this->race)."')
	          	  	";
			$res = WCF::getDB()->sendQuery($sql);

			$row = 0;
			$row = WCF::getDB()->getAffectedRows($res);
			$this->newRaces = 0;
			if ($row > 0) {				
				WCF::getTPL()->append('userMessages', WCF::getTPL()->fetch('racingLeagueNewSuccess'));
			}
			else {
				WCF::getTPL()->append('userMessages', WCF::getTPL()->fetch('racingLeagueNewError'));
			}
		}

		// Hier wird ein Renntag bearbeitet
		if ($this->race && $this->activeSaisonID != 0 && $this->edit == 1 && $this->raceID != 0) {
		
			$sql = "UPDATE wcf".WCF_N."_racing_races 
					SET	racename = '".escapeString($this->race)."',
						raceNo = '".escapeString($this->raceNo)."'
					WHERE raceID = '".escapeString($this->raceID)."'
					LIMIT 1
					";
			
			$res = WCF::getDB()->sendQuery($sql);

			$row = 0;
			$row = WCF::getDB()->getAffectedRows($res);
			$this->edit = 0;
			if ($row > 0) {				
				WCF::getTPL()->append('userMessages', WCF::getTPL()->fetch('racingLeagueEditSuccess'));
			}
			else {
				WCF::getTPL()->append('userMessages', WCF::getTPL()->fetch('racingLeagueEditError'));
			}
		}
	}
	
	/**
	 * @see Page::assignVariables();
	 */
	public function assignVariables() {
		parent::assignVariables();

		WCF::getTPL()->assign(array(
			'saisonID' => $this->saisonID,
			'newRaces' => $this->newRaces,
			'edit' => $this->edit,
			'race' => $this->race,
			'raceID' => $this->raceID,
			'racename' => $this->racename,
			'races' => $this->races,
			'anz' => $this->anz,
			'raceEditNo' => $this->raceEditNo,
			'redirect' => $this->redirect
			));
	}
	
	/**
	 * @see Page::show()
	 */
	public function show() {
		
		// check permission
		WCF::getUser()->checkPermission('admin.racing.league.caneditraces');
		require_once(WCF_DIR.'lib/page/util/menu/HeaderMenu.class.php');
		HeaderMenu::setActiveMenuItem('wcf.header.menu.racing.league');
		parent::show(); 		
    }
}
?>