Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_draftpick.php
<?php
/**
 * Defines the {@link OFFL_DraftPick} class.
 *
 * @package offl
 * @author Stephen Rochelle <hide@address.com>
 */

if (strtr(__FILE__, "\\", "/") == $_SERVER["SCRIPT_FILENAME"])
{	die ("Cannot access file directly!");	}

require_once($DOC_ROOT . "/lib/classes/offl_dbobject.php");
require_once($DOC_ROOT . "/lib/classes/offl_fflteam.php");
require_once($DOC_ROOT . "/lib/classes/offl_player.php");
/**
 * Defines interfaces for individual draft picks.
 *
 * This class is primarily used by the {@link OFFL_Draft} class methods.  I need to mess with the save()
 * function to work with the new (and still unfinished) roster storage method.  Also, roster validation
 * is needed during draft process.
 *
 * I've also cut back drastically on the info recorded in this class.  I want to push towards native
 * lookup rather than all these left joins.
 *
 * @package offl
 */
class OFFL_DraftPick extends OFFL_DBObject {
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var integer
	 */
	var $_year = NULL;
	/**
	 * @var integer
	 */
	var $_round_no = NULL;
	/**
	 * @var integer
	 */
	var $_pick_no = NULL;
	/**
	 * @var integer
	 */
	var $_fflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_fromfflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_fromfromfflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_player_id = NULL;

	/**
	 * Constructor
	 *
	 * Either all parameters should be left alone (for a blank draft pick) or all four should be given.
	 *
	 * @param integer $league_id Optional: If set with others, loads given draft pick from database
	 * @param integer $year Optional: If set with others, loads given draft pick from database
	 * @param integer $round_no Optional: If set with others, loads given draft pick from database
	 * @param integer $pick_no Optional: If set with others, loads given draft pick from database
	 */
	function OFFL_DraftPick ($league_id = NULL, $year = NULL, $round_no = NULL, $pick_no = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(!is_null($league_id) && !is_null($year) && !is_null($round_no) && !is_null($pick_no))
		{
			$this->_league_id = $league_id;
			$this->_year = $year;
			$this->_round_no = $round_no;
			$this->_pick_no = $pick_no;
			$this->populate();
		}
	}

	/**
	 * Yanks info from database, given a mysql result row
	 *
	 * @param array $row From {@link mysql_fetch_row() mysql_fetch_row()}
	 */
	function populateFromResult($row)
	{
	// this function is called by Draft objects

		// $row is an associative array from mysql_fetch_assoc()
		$this->_league_id = $row["league_id"];
		$this->_year = $row["year"];
		$this->_round_no = $row["round_no"];
		$this->_pick_no = $row["pick_no"];
		$this->_fflteam_id = $row["fflteam_id"];
		$this->_fromfflteam_id = $row["fromfflteam_id"];
		$this->_fromfromfflteam_id = $row["fromfromfflteam_id"];
		$this->_player_id = $row["player_id"];
	}
	
	/**
	 * Yanks info from database
	 *
	 * Has unusual if check.  Ought to figure out why (I don't remember).
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(is_null($this->_league_id) || is_null($this->_year) || is_null($this->_round_no) || is_null($this->_pick_no))
		{
			$this->_emsg = "Parameters are not set.  Cannot populate OFFL_DraftPick object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT * FROM drafts WHERE league_id=" . $this->_league_id . " AND year=" . $this->_year . " AND round_no=" . $this->_round_no . " AND pick_no=" . $this->_pick_no;

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No draft pick record found for these constraints.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_fflteam_id = mysql_result($result,0,"fflteam_id");
		$this->_fromfflteam_id = mysql_result($result,0,"fromfflteam_id");
		$this->_fromfromfflteam_id = mysql_result($result,0,"fromfromfflteam_id");
		$this->_player_id = mysql_result($result,0,"player_id");
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves info into database.
	 *
	 * I think I should revise the parameter syntax here.  Also updates OFFL_Player status, which must be revised.
	 *
	 * Draft picks should only be added at creation time (and left blank until draft conducted)
	 * @param string $mode Should be set to "add" for adding a new DraftPick or "edit" for making/changing a player selection.
	 * @return boolean FALSE if $mode incorrectly set, otherwise TRUE
	 */
	function save ($mode)
	{
		if(is_null($this->_fromfflteam_id) || ($this->_fromfflteam_id == ""))
		{	$this->_fromfflteam_id = "NULL";	}
		if(is_null($this->_fromfromfflteam_id) || ($this->_fromfromfflteam_id == ""))
		{	$this->_fromfromfflteam_id = "NULL";	}
		if(!is_null($this->_player_id))
		{
			$team = new OFFL_FFLTeam($this->_fflteam_id);
			$team->addPlayer($this->_player_id);
			$team->save();
		}
		if(is_null($this->_player_id) || ($this->_player_id == ""))
		{	$this->_player_id = "NULL";	}

		if($mode == "add")
		{
			// insert
			$sql = "INSERT INTO drafts (league_id, year, round_no, pick_no, fflteam_id, fromfflteam_id, fromfromfflteam_id, player_id) VALUES (" . $this->_league_id . ", " . $this->_year . ", " . $this->_round_no . ", " . $this->_pick_no . ", " . $this->_fflteam_id . ", " . $this->_fromfflteam_id . ", " . $this->_fromfromfflteam_id . ", " . $this->_player_id . ")";
		}
		elseif($mode == "edit")
		{
			// update
			$sql = "UPDATE drafts SET fflteam_id=" . $this->_fflteam_id . ", fromfflteam_id=" . $this->_fromfflteam_id . ", fromfromfflteam_id=" . $this->_fromfromfflteam_id . ", player_id=" . $this->_player_id . " WHERE league_id=" . $this->_league_id . " AND year=" . $this->_year . " AND round_no=" . $this->_round_no . " AND pick_no=" . $this->_pick_no;
		}
		else {	return FALSE;	}

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		return $result;
	}

	function deleteDraftPick()
	{
		if(is_null($this->_league_id) || is_null($this->_year) || is_null($this->_round_no) || is_null($this->_pick_no))
		{
			$this->_emsg = "parameters are not set.  Cannot delete OFFL_DraftPick object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "DELETE FROM drafts WHERE league_id=" . $this->_league_id . " AND year=" . $this->_year . " AND round_no=" . $this->_round_no . " AND pick_no=" . $this->_pick_no;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		if (mysql_affected_rows($this->_conn) > 0)
		{	return TRUE;	}
		return FALSE;
	}

	/**
	 * @return integer
	 */
	function getLeagueID()
	{
		return $this->_league_id;
	}

	/**
	 * @return integer
	 */
	function getYear()
	{
		return $this->_year;
	}

	/**
	 * @return integer
	 */
	function getRoundNo()
	{
		return $this->_round_no;
	}

	/**
	 * @return integer
	 */
	function getPickNo()
	{
		return $this->_pick_no;
	}

	/**
	 * @param integer $fflteam_id
	 */
	function setFFLTeamID($fflteam_id)
	{
		$this->_fflteam_id = $fflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getFFLTeamID()
	{
		return $this->_fflteam_id;
	}

	/**
	 * @param integer $fromfflteam_id
	 */
	function setFromFFLTeamID($fromfflteam_id)
	{
		$this->_fromfflteam_id = $fromfflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getFromFFLTeamID()
	{
		return $this->_fromfflteam_id;
	}

	/**
	 * @param integer $fromfromfflteam_id
	 */
	function setFromFromFFLTeamID($fromfromfflteam_id)
	{
		$this->_fromfromfflteam_id = $fromfromfflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getFromFromFFLTeamID()
	{
		return $this->_fromfromfflteam_id;
	}

	/**
	 * @param integer $player_id
	 */
	function setPlayerID($player_id)
	{
		$this->_player_id = $player_id; 
	}

	/**
	 * @return integer
	 */
	function getPlayerID()
	{
		return $this->_player_id;
	}

}
?>
Return current item: Online Fantasy Football League