Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_pastroster.php
<?php
/**
 * Defines the {@link OFFL_PastRoster} 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");
/**
 * Defines interfaces for archived {@link OFFL_FFLTeam} rosters.
 *
 * @package offl
 */
class OFFL_PastRoster extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_roster_id = NULL;
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var integer
	 */
	var $_year = NULL;
	/**
	 * @var integer
	 */
	var $_week = NULL;
	/**
	 * @var integer
	 */
	var $_fflteam_id = NULL;
	/**
	 * @var integer Array of integers
	 */
	var $_player_id_array = array();
	/**
	 * @var array Array of integral booleans (1/0)
	 */
	var $_starter_array = array();
	/**
	 * @var integer Array of integers
	 */
	var $_nflteam_id_array = array();

	/**
	 * Constructor
	 *
	 * Either all parameters should be left alone (for a blank roster record) or all three should be given.
	 *
	 * @param integer $year Optional: If set with others, loads given roster from database
	 * @param integer $week Optional: If set with others, loads given roster from database
	 * @param integer $fflteam_id Optional: If set with others, loads given roster from database
	 */
	function OFFL_PastRoster ($year = NULL, $week = NULL, $fflteam_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(isset($year) && isset($week) && isset($fflteam_id))
		{
			$this->_year = $year;
			$this->_week = $week;
			$this->_fflteam_id = $fflteam_id;
			$this->populate();
		}
	}
	
	/**
	 * Yanks info from database
	 *
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(is_null($this->_year) || is_null($this->_week) || is_null($this->_fflteam_id))
		{
			$this->_emsg = "Control values are not set.  Cannot populate PastRoster object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT * FROM pastrosters WHERE year=" . $this->_year . " AND week=" . $this->_week . " AND fflteam_id=" . $this->_fflteam_id;

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No roster records found for year " . $this->_year . ", week " . $this->_week . ", and fflteam_id " . $this->_fflteam_id . ".";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_roster_id = mysql_result($result,0,"roster_id");
		$this->_league_id = mysql_result($result,0,"league_id");
		$player_id_string = htmlspecialchars(stripslashes(mysql_result($result,0,"player_id_array")));
		$starter_string = htmlspecialchars(stripslashes(mysql_result($result,0,"starter_array")));
		$nflteam_id_string = htmlspecialchars(stripslashes(mysql_result($result,0,"nflteam_id_array")));

		$player_id_array = array_diff(explode(",", $player_id_string), array(""));
		$starter_array = array_diff(explode(",", $starter_string), array(""));
		$nflteam_id_array = array_diff(explode(",", $nflteam_id_string), array(""));

		foreach ($player_id_array as $player_id)
		{
			$player_id = trim($player_id);
			if ($player_id != "")
				$this->_player_id_array[] = $player_id;
		}
		foreach ($starter_array as $starter)
		{
			$starter = trim($starter);
			if ($starter != "")
				$this->_starter_array[] = $starter;
		}
		foreach ($nflteam_id_array as $nflteam_id)
		{
			$nflteam_id = trim($nflteam_id);
			if ($nflteam_id != "")
				$this->_nflteam_id_array[] = $nflteam_id;
		}

		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves info into database.  Determines UPDATE or INSERT automagically.
	 */
	function save ()
	{
		$player_id_string = "";
		$starter_string = "";
		$nflteam_id_string = "";
		if (!is_null($this->_player_id_array))
			$player_id_string = implode(",", $this->_player_id_array);
		if (!is_null($this->_starter_array))
			$starter_string = implode(",", $this->_starter_array);
		if (!is_null($this->_nflteam_id_array))
			$nflteam_id_string = implode(",", $this->_nflteam_id_array);
		if (is_null($this->_league_id))
		{	
			$team = new OFFL_FFLTeam($this->_fflteam_id);
			$this->_league_id = $team->getLeagueID();
		}
		
		$sql = "";
		if(empty($this->_roster_id))
		{
			// insert
			$sql = "INSERT INTO pastrosters (year, week, league_id, fflteam_id, player_id_array, starter_array, nflteam_id_array) VALUES (" . $this->_year . ", " . $this->_week . ", " . $this->_league_id . ", " . $this->_fflteam_id . ", '" . mysql_escape_string($player_id_string) . "', '" . mysql_escape_string($starter_string) . "', '" . mysql_escape_string($nflteam_id_string) . "')";
		}
		else
		{
			// update
			$sql = "UPDATE pastrosters SET league_id=" . $this->_league_id . ", player_id_array='" . mysql_escape_string($player_id_string) . "', starter_array='" . mysql_escape_string($starter_string) . "', nflteam_id_array='" . mysql_escape_string($nflteam_id_string) . "' WHERE year=" . $this->_year . " AND week=" . $this->_week . " AND fflteam_id=" . $this->_fflteam_id;
		}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

	/**
	 * @return integer
	 */
	function getRosterID()
	{
		return $this->_roster_id;
	}

	/**
	 * @param integer $year
	 */
	function setYear($year)
	{
		$this->_year = $year;
	}

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

	/**
	 * @param integer $week
	 */
	function setWeek($week)
	{
		$this->_week = $week;
	}

	/**
	 * @return integer
	 */
	function getWeek()
	{
		return $this->_week;
	}

	/**
	 * @param integer $league_id
	 */
	function setLeagueID($league_id)
	{
		$this->_league_id = $league_id;
	}

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

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

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

	/**
	 * @param array $player_id_array Array of integers
	 */
	function setPlayerIDArray($player_id_array)
	{
		$this->_player_id_array = $player_id_array;
	}

	/**
	 * Returns an array of {@link OFFL_Player::$_player_id Player IDs}
	 * @return array Array of integers
	 */
	function getPlayerIDArray()
	{
		return $this->_player_id_array;
	}

	/**
	 * Should have same index order as that passed to {@link setPlayerIDArray()}
	 * @param array $player_id_array Array of integral booleans (1/0)
	 */
	function setStarterArray($starter_array)
	{
		$this->_starter_array = $starter_array;
	}

	/**
	 * Returns an array of starter status for players in the same order provided by {@link getPlayerIDArray()}
	 * @return array Array of integral booleans (1/0)
	 */
	function getStarterArray()
	{
		return $this->_starter_array;
	}

	/**
	 * @param array $nflteam_id_array Array of integers
	 */
	function setNFLTeamIDArray($nflteam_id_array)
	{
		$this->_nflteam_id_array = $nflteam_id_array;
	}

	/**
	 * Returns an array of {@link OFFL_NFLTeam::$_nflteam_id Player IDs}
	 * @return array Array of integers
	 */
	function getNFLTeamIDArray()
	{
		return $this->_nflteam_id_array;
	}


} // end OFFL_PastRoster class
?>
Return current item: Online Fantasy Football League