Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_nflgame.php
<?php
/**
 * Defines the {@link OFFL_NFLGame} 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_nflteam.php");
/**
 * Defines interfaces for info related to the NFL schedule.
 *
 * @package offl
 */
class OFFL_NFLGame extends OFFL_DBObject {
	/**
	 * @var integer
	 */
	var $_nflgame_id = NULL;
	/**
	 * @var integer
	 */
	var $_year = NULL;
	/**
	 * @var integer
	 */
	var $_week = NULL;
	/**
	 * @var string SQL DATETIME format, GMT
	 */
	var $_gametime = NULL;
	/**
	 * @var integer
	 */
	var $_v_nflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_h_nflteam_id = NULL;
	/**
	 * @var string Read-only data
	 */
	var $_v_nflteam_city = NULL;
	/**
	 * @var string Read-only data
	 */
	var $_v_nflteam_name = NULL;
	/**
	 * @var string Read-only data
	 */
	var $_v_nflteam_abbv = NULL;
	/**
	 * @var string Read-only data
	 */
	var $_h_nflteam_city = NULL;
	/**
	 * @var string Read-only data
	 */
	var $_h_nflteam_name = NULL;
	/**
	 * @var string Read-only data
	 */
	var $_h_nflteam_abbv = NULL;

	/**
	 * Constructor
	 *
	 * @param integer $nflgame_id Optional: If set, loads given game from database
	 */
	function OFFL_NFLGame ($nflgame_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(isset($nflgame_id))
		{
			$this->_nflgame_id = $nflgame_id;
			$this->populate();
		}
	}
	
	/**
	 * Yanks info from database
	 *
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(empty($this->_nflgame_id))
		{
			$this->_emsg = "\$_game_id is not set.	Cannot populate Game object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Left outer join glory! w00t!
		$sql = "SELECT g.*, " . 
		"t1.nflteam_city AS v_nflteam_city, t1.nflteam_name AS v_nflteam_name, t1.nflteam_abbv AS v_nflteam_abbv, " . 
		"t2.nflteam_city AS h_nflteam_city, t2.nflteam_name AS h_nflteam_name, t2.nflteam_abbv AS h_nflteam_abbv " . 
		"FROM nflgames g " . 
		"LEFT JOIN nflteams t1 " . 
			"ON g.v_nflteam_id=t1.nflteam_id " . 
		"LEFT JOIN nflteams t2 " . 
			"ON g.h_nflteam_id=t2.nflteam_id " . 
		"where nflgame_id=" . $this->_nflgame_id;

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No NFL game records found for nflgame_id " . $this->_nflgame_id . ".";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_year = mysql_result($result,0,"year");
		$this->_week = mysql_result($result,0,"week");
		$this->_gametime = mysql_result($result,0,"gametime");
		$this->_v_nflteam_id = mysql_result($result,0,"v_nflteam_id");
		$this->_h_nflteam_id = mysql_result($result,0,"h_nflteam_id");
		$this->_v_nflteam_city = htmlspecialchars(stripslashes(mysql_result($result,0,"v_nflteam_city")));
		$this->_v_nflteam_name = htmlspecialchars(stripslashes(mysql_result($result,0,"v_nflteam_name")));
		$this->_v_nflteam_abbv = htmlspecialchars(stripslashes(mysql_result($result,0,"v_nflteam_abbv")));
		$this->_h_nflteam_city = htmlspecialchars(stripslashes(mysql_result($result,0,"h_nflteam_city")));
		$this->_h_nflteam_name = htmlspecialchars(stripslashes(mysql_result($result,0,"h_nflteam_name")));
		$this->_h_nflteam_abbv = htmlspecialchars(stripslashes(mysql_result($result,0,"h_nflteam_abbv")));
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves info into database.  Determines UPDATE or INSERT automagically.
	 */
	function save ()
	{
		if(is_null($this->_nflgame_id))
		{
			// insert
			$sql = "INSERT INTO nflgames (year, week, gametime, v_nflteam_id, h_nflteam_id) VALUES (" . $this->_year . ", " . $this->_week . ", '" . $this->_gametime . "', " . $this->_v_nflteam_id . ", " . $this->_h_nflteam_id . ")";
		}
		else
		{
			// update
			$sql = "UPDATE nflgames SET year=" . $this->_year . ", week=" . $this->_week . ", gametime='" . $this->_gametime . "', v_nflteam_id=" . $this->_v_nflteam_id . ", h_nflteam_id=" . $this->_h_nflteam_id . " WHERE nflgame_id=" . $this->_nflgame_id;
		}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

	/**
	 * @return integer
	 */
	function getNFLGameID()
	{
		return $this->_nflgame_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;
	}

	/**
	 * Sets the parameter's {@link gmdate() gmdate()} output to the SQL DATETIME format stored in the database
	 * @param integer Timestamp, GMT
	 */
	function setGametime($gametime)
	{
		if (is_null($gametime))
			$this->_gametime = NULL;
		else
			$this->_gametime = gmdate("Y-m-d H:i:s", $gametime);
	}

	/**
	 * Returns the {@link gmmktime() gmmktime()} output of the SQL DATETIME format stored in the database
	 * @return integer Timestamp, GMT
	 */
	function getGametime()
	{
		if (is_null($this->_gametime))
			return $this->_gametime;

		// assemble DATETIME format into UNIX timestamp
		list($date, $time) = explode(" ", $this->_gametime);
		list($Y, $M, $D) = explode("-", $date);
		list($h, $i, $s) = explode(":", $time);
		$time = gmmktime($h, $i, $s, $M, $D, $Y);
		return $time;
	}

	/**
	 * @param integer $h_nflteam_id
	 */
	function setHNFLTeamID($h_nflteam_id)
	{
		$this->_h_nflteam_id = $h_nflteam_id;
	}

	/**
	 * @return integer
	 */
	function getHNFLTeamID()
	{
		return $this->_h_nflteam_id;
	}

	/**
	 * @return string
	 */
	function getHNFLTeamCity()
	{
		return $this->_h_nflteam_city;
	}

	/**
	 * @return string
	 */
	function getHNFLTeamName()
	{
		return $this->_h_nflteam_name;
	}

	/**
	 * @return string
	 */
	function getHNFLTeamAbbv()
	{
		return $this->_h_nflteam_abbv;
	}

	/**
	 * @param integer $v_nflteam_id
	 */
	function setVNFLTeamID($v_nflteam_id)
	{
		$this->_v_nflteam_id = $v_nflteam_id;
	}

	/**
	 * @return integer
	 */
	function getVNFLTeamID()
	{
		return $this->_v_nflteam_id;
	}

	/**
	 * @return string
	 */
	function getVNFLTeamCity()
	{
		return $this->_v_nflteam_city;
	}

	/**
	 * @return string
	 */
	function getVNFLTeamName()
	{
		return $this->_v_nflteam_name;
	}

	/**
	 * @return string
	 */
	function getVNFLTeamAbbv()
	{
		return $this->_v_nflteam_abbv;
	}

	/**
	 * Returns OFFL_NFLGame objects, optionally by year or by year and week.  Default returns all games in database.
	 *
	 * @param integer $year Optional: if set, returns only games from the given year
	 * @param integer $week Optional: if set, returns only games from the given week
	 * @return array Array of OFFL_NFLGame objects fitting given criteria
	 */
	function getNFLGames($year = NULL, $week = NULL)
	{
		$retArr = array();
		$sql = "SELECT nflgame_id FROM nflgames";
		if(isset($year))
		{
			$sql .= " WHERE year=" . $year;
			if(isset($week))
			{	$sql .= " AND week=" . $week;	}
		}
		$sql .= " ORDER BY gametime, year, week";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$game = new OFFL_NFLGame(mysql_result($result,$i,"nflgame_id"));
			array_push($retArr, $game);
		}
	
		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Returns all years with registered NFL games
	 * @return array Array of integers
	 */
	function getNFLGameYears()
	{
		$retArr = array();
		$sql = "SELECT DISTINCT year FROM nflgames ORDER BY year";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
			array_push($retArr, mysql_result($result,$i,"year"));

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Returns all weeks from a given year with registered NFL games
	 * @param integer $year Weeks with registered games are returned for the year matching this parameter
	 * @return array Array of integers
	 */
	function getNFLGameWeeks($year)
	{
		$retArr = array();
		$sql = "SELECT DISTINCT week FROM nflgames WHERE year=" . $year . " ORDER BY week";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{	array_push($retArr, mysql_result($result,$i,"week"));	}

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Looks up the OFFL_NFLGame object matching a given year, week, and {@link OFFL_NFLTeam::$_nflteam_id nflteam_id}
	 * @param integer $year 
	 * @param integer $week
	 * @param integer $nflteam_id
	 * @return OFFL_NFLGame Returns relevant game if found, FALSE otherwise
	 */
	function getNFLTeamGame($year, $week, $nflteam_id)
	{
		$sql = "SELECT * FROM nflgames WHERE year=" . $year . " AND week=" . $week . " AND (h_nflteam_id=" . $nflteam_id . " OR v_nflteam_id=" . $nflteam_id . ")";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		if (mysql_num_rows($result) > 0)
		{	return new OFFL_NFLGame(mysql_result($result,0,"nflgame_id"));	}
		return FALSE;
	}

	/**
	 * Looks up the NFL opponent of a given year, week, and {@link OFFL_NFLTeam::$_nflteam_id nflteam_id}
	 * @param integer $year 
	 * @param integer $week
	 * @param integer $nflteam_id
	 * @return string If provided nflteam_id is the home team, returns output like "TEN".  If provided nflteam_id is the visiting team, returns output like "@ten".  Returns "Bye" if no game found.
	 */
	function getNFLTeamGameOpponent($year, $week, $nflteam_id)
	{
		$sql = "SELECT * FROM nflgames WHERE year=" . $year . " AND week=" . $week . " AND (h_nflteam_id=" . $nflteam_id . " OR v_nflteam_id=" . $nflteam_id . ")";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		if (mysql_num_rows($result)) // game found
		{
			if (mysql_result($result,0,"h_nflteam_id") == $nflteam_id)
			{
				$team = new OFFL_NFLTeam(mysql_result($result,0,"v_nflteam_id"));
				return $team->getNFLTeamAbbv();
			}
			$team = new OFFL_NFLTeam(mysql_result($result,0,"h_nflteam_id"));
			return "@" . strtolower($team->getNFLTeamAbbv());
		}
		return "Bye";
	}

	/**
	 * Supplement to {@link getNFLGames()}.  Returns bye teams for a given year and week.
	 * @param integer $year
	 * @param integer $week
	 * @return array Array of {@link OFFL_NFLTeam} objects
	 */
	function getByeTeams($year, $week)
	{
		$non_bye_teams = array();
		$bye_teams = array();

		$sql = "SELECT v_nflteam_id, h_nflteam_id FROM nflgames WHERE year=" . $year . " and week=" . $week;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$non_bye_teams[] = mysql_result($result, $i, "h_nflteam_id");
			$non_bye_teams[] = mysql_result($result, $i, "v_nflteam_id");
		}
		mysql_free_result($result);

		$lookup_team = new OFFL_NFLTeam();
		$NFLTeams = $lookup_team->getAllNFLTeams();
		foreach ($NFLTeams as $team)
		{
			if (!in_array($team->getNFLTeamID(), $non_bye_teams))
			{	$bye_teams[] = new OFFL_NFLTeam($team->getNFLTeamID());	}
		}

		return $bye_teams;
	}

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