Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_pastleague.php
<?php
/**
 * Defines the {@link OFFL_PastLeague} 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 league and division listings.
 *
 * @package offl
 */
class OFFL_PastLeague extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var integer
	 */
	var $_year = NULL;
	/**
	 *	@var array
	 */
	var $_division_id_array = array();
	/**
	 *	@var array
	 */
	var $_fflteam_id_array = array();
	/**
	 *	@var array
	 */
	var $_fflteam_id_by_division_id_array = array();
	/**
	 *	@var array
	 */
	var $_user_id_array = array();
	/**
	 * @var boolean checks if a league has been found by lookup
	 */
	var $_found = FALSE;

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

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

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No league records found for league_id " . $this->_league_id . " and year " . $this->_year . ".";
//			error_log ($this->_emsg);
			return FALSE;
		}
		$division_id_string = htmlspecialchars(stripslashes(mysql_result($result,0,"division_id_array")));
		$fflteam_string = htmlspecialchars(stripslashes(mysql_result($result,0,"fflteam_id_array")));
		$user_id_string = htmlspecialchars(stripslashes(mysql_result($result,0,"user_id_array")));

		$division_id_array = array_diff(explode(",", $division_id_string), array(""));
		$fflteam_array = array_diff(explode("|", $fflteam_string), array(""));
		$user_id_array = array_diff(explode(",", $user_id_string), array(""));

		foreach ($division_id_array as $division_id)
		{
			$division_id = trim($division_id);
			if ($division_id != "")
			{	
				$this->_division_id_array[] = $division_id;
				$this->_fflteam_id_by_division_id_array[$division_id] = array();
			}
		}
		$fflteam_tmp_array = array();
		foreach ($fflteam_array as $fflteam)
		{
			$fflteam = trim($fflteam);
			if ($fflteam != "")
			{	$fflteam_tmp_array[] = $fflteam;	 }
		}
		foreach($this->_division_id_array as $i=>$division_id)
		{
			$fflteam_array = array_diff(explode(",", $fflteam_tmp_array[$i]), array(""));
			foreach ($fflteam_array as $fflteam)
			{
				$fflteam = trim($fflteam);
				if ($fflteam != "")
				{	
					$this->_fflteam_id_array[] = $fflteam;
					$this->_fflteam_id_by_division_id_array[$division_id][] = $fflteam;
				}
			}
		}
		foreach ($user_id_array as $user_id)
		{
			$user_id = trim($user_id);
			if ($user_id != "")
			{	$this->_user_id_array[] = $user_id;	 }
		}

		mysql_free_result($result);

		return TRUE;
	}

	/**
	 * Saves info into database.  Determines UPDATE or INSERT automagically.
	 */
	function save()
	{
		$division_id_string = "";
		$fflteam_id_string = "";
		$fflteam_id_temp_string = "";
		$user_id_string = "";

		if (count($this->_division_id_array))
		{	$division_id_string = implode(",", $this->_division_id_array);	}
		if (count($this->_fflteam_id_by_division_id_array))
		{
			foreach ($this->_fflteam_id_by_division_id_array as $divisionlist)
			{
				$fflteam_id_temp_string[] = implode(",", $divisionlist);
			}
			$fflteam_id_string = implode("|", $fflteam_id_temp_string);
		}
		if (count($this->_user_id_array))
		{	$user_id_string = implode(",", $this->_user_id_array);	}
		
		$sql = "";
		// insert
		mysql_query("DELETE FROM pastleagues WHERE league_id=" . $this->_league_id . " AND year=" . $this->_year);

		$sql = "INSERT INTO pastleagues (league_id, year, division_id_array, fflteam_id_array, user_id_array) VALUES (" . $this->_league_id . ", " . $this->_year . ", '" . mysql_escape_string($division_id_string) . "', '" . mysql_escape_string($fflteam_id_string) . "', '" . mysql_escape_string($user_id_string) . "')";

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

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

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

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

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

	function getDivisionIDs()
	{
		return $this->_division_id_array;
	}

	function getFFLTeamIDs($division_id = NULL)
	{
		if (isset($division_id))
		{	return $this->_fflteam_id_by_division_id_array[$division_id];	}
		return $this->_fflteam_id_array;
	}

	function getUserIDs()
	{
		return $this->_user_id_array;
	}
	
	function getFound()
	{
		return $this->_found;
	}

	function storeCurrentLeague($league_id, $year=NULL)
	{
		$this->_league_id = $league_id;
		if (isset($year))
		{	$this->_year = $year;	}
		else
		{	$this->_year = getThisYear();	}

		$league = new OFFL_League($league_id);
		$divisions = $league->getAllDivisions();
		foreach ($divisions as $division)
		{
			$this->_division_id_array[] = $division->getDivisionID();
			
			$teams = $division->getAllDivisionTeams();
			foreach ($teams as $team)
			{
				$this->_fflteam_id_by_division_id_array[$division->getDivisionID()][] = $team->getFFLTeamID();
				$this->_fflteam_id_array[] = $team->getFFLTeamID();
				if (!in_array($team->getUserID(), $this->_user_id_array))
				{	$this->_user_id_array[] = $team->getUserID();	}
			}
		}
		$this->save();
	}

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