<?php
// wcf imports
require_once(WCF_DIR.'lib/form/RacingLeagueForm.class.php');

/**
 * Seite zum definieren der Platz/Punkte-Regeln
 * 
 * @author	Markus Gerdelmann
 * @copyright	2008 MDMAN
 * @license GNU
 * @package	de.mdman.racing.league
 */

class RacingLeaguePointsForm extends RacingLeagueForm {
 
	public $templateName = 'racingLeaguePoints';
	public $saisonID = 0;
	public $new = 0;
	public $edit = 0;
	public $ID = 0;
	public $pointsEdit = '';
	public $pointsID = 0;
	public $points = array();
	public $point = '';
	public $place = 0;
	public $anz = 0;	

	public $redirect = 'RacingLeaguePoints';
	
	/**
	 * @see Form::readParameters()
	 */
	public function readParameters() {
		parent::readParameters();

		if (isset($_REQUEST["saisonID"]))		$this->saisonID = intval($_REQUEST["saisonID"]);	
		if (isset($_REQUEST["new"]))		$this->new = intval($_REQUEST["new"]);
		if (isset($_REQUEST["edit"]))		$this->edit = intval($_REQUEST["edit"]);
		if (isset($_REQUEST["pointsID"]))		$this->pointsID = intval($_REQUEST["pointsID"]);		
	}
	
	/**
	 * @see Form::readFormParameters()
	 */
	public function readFormParameters() {
		parent::readFormParameters();
		
		if (isset($_POST["point"]))		$this->point = intval($_POST["point"]);
		if (isset($_POST["place"]))		$this->place = intval($_POST["place"]);
	}
	
	/**
	 * @see Form::validate()
	 */
	public function validate() {
		parent::validate();

		// point bei NEW
		if ($this->new == 1) {
			if (empty($this->point)) {
				$this->new = '1';
				throw new UserInputException('point');
			}
		}

		// point bei EDIT
		if ($this->edit == 1) {
			if (empty($this->point)) {
				$this->edit = '1';
				throw new UserInputException('point');
			}
		}		
	}

	/**
	 * @see Form::readData()
	 */
	public function readData() {
		parent::readData();

		// Hier wird die Anzahl der vorhandenen Renntage ermittelt
		if ($this->new == 1) {

			$sql = "SELECT *
					FROM wcf".WCF_N."_racing_points
					WHERE saisonID = '".$this->activeSaisonID."'
					";
			$res = WCF::getDB()->sendQuery($sql);
			$rows = WCF::getDB()->countRows($res);
			$this->anz = intval($rows) + 1;
		}

		// Hier wird eine Platz/Punkte-Regel geladen die editiert werden soll
		if ($this->edit == 1) {

			$sql = "SELECT * 
					FROM wcf".WCF_N."_racing_points
					WHERE pointsID = '".$this->pointsID."'
					";
		
			$res = WCF::getDB()->sendQuery($sql);
		
			while ($row = WCF::getDB()->fetchArray($res)) {
				$this->place = $row["place"];
				$this->pointsEdit = $row["points"];
			}
			
			$sql = "SELECT * 
					FROM wcf".WCF_N."_racing_points
					WHERE saisonID = '".$this->activeSaisonID."'
					";
			$res = WCF::getDB()->sendQuery($sql);
			$rows = WCF::getDB()->countRows($res);
			$this->anz = intval($rows) + 1;
		}
		
		//Hier werden alle Points geladen
		if ($this->activeSaisonID != 0) {
			$sql = "SELECT *
					FROM wcf".WCF_N."_racing_points
					WHERE saisonID = ".$this->activeSaisonID."
					ORDER BY place ASC
					";

			$res = WCF::getDB()->sendQuery($sql);
			while ($row = WCF::getDB()->fetchArray($res)) {
				$this->points[] = $row;
			}			
		}	
	}
	
	/**
	 * @see Form::save()
	 */
	public function save() {
		parent::save();
		
		// Hier wird eine neue Platz/Punkte-Regel gespeichert
		if ($this->point && $this->activeSaisonID != 0 && $this->new == 1) {
			
			$sql = "INSERT INTO wcf".WCF_N."_racing_points (saisonID, pointsID, place, points)
           			VALUES ('".escapeString($this->activeSaisonID)."', '', '".escapeString($this->place)."', '".escapeString($this->point)."')
	          	  	";
			$res = WCF::getDB()->sendQuery($sql);

			$row = 0;
			$row = WCF::getDB()->getAffectedRows($res);
			$this->new = 0;
			if ($row > 0) {				
				WCF::getTPL()->append('userMessages', WCF::getTPL()->fetch('racingLeagueNewSuccess'));
			}
			else {
				WCF::getTPL()->append('userMessages', WCF::getTPL()->fetch('racingLeagueNewError'));
			}
		}

		// Hier wird eine Platz/Punkte-Regel bearbeitet
		if ($this->place && $this->activeSaisonID != 0 && $this->edit == 1 && $this->pointsID != 0) {
		
			$sql = "UPDATE wcf".WCF_N."_racing_points 
					SET	points = '".escapeString($this->point)."',
						place = '".escapeString($this->place)."'
					WHERE pointsID = '".escapeString($this->pointsID)."'
					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,
			'new' => $this->new,
			'edit' => $this->edit,
			'place' => $this->place,
			'pointsEdit' => $this->pointsEdit,
			'pointsID' => $this->pointsID,
			'points' => $this->points,
			'point' => $this->point,			
			'anz' => $this->anz,
						
			'redirect' => $this->redirect
			));
	}
	
	/**
	 * @see Page::show()
	 */
	public function show() {
		
		// check permission
		WCF::getUser()->checkPermission('admin.racing.league.caneditpoints');
		require_once(WCF_DIR.'lib/page/util/menu/HeaderMenu.class.php');
		HeaderMenu::setActiveMenuItem('wcf.header.menu.racing.league');
		parent::show();		
    }
}
?>