Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_league.php
<?php
/**
 * Defines the OFFL_League 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");
require_once($DOC_ROOT . "/lib/classes/offl_rosterplayer.php");
/**
 * The OFFL_League class provides an interface to the users and teams involved in an OFFL league.
 *
 * This class is being revised for the next OFFL release to incorporate the new {@link OFFL_RosterPlayer} class.
 *
 * @package offl
 */
class OFFL_League extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var array
	 */
	var $_user_id_array = array();
	/**
	 * @var array
	 */
	var $_fflteam_id_array = array();
	/**
	 * @var array
	 */
	var $_division_id_array = array();
	/**
	 * @var string
	 */
	var $_league_name = "";
	/**
	 * @var string
	 */
	var $_league_abbv = "";
	/**
	 * @var string
	 */
	var $_league_motto = "";
	/**
	 * @var string
	 */
	var $_commish_email = "";
	/**
	 * @var integer Integral boolean (1/0)
	 */
	var $_public = 1;

	/**
	 * constructor, populates if optional parameter $league_id is set
	 *
	 * @param integer $league_id FFL League ID number to populate object with
	 * @see populate()
	 */
	function OFFL_League($league_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(!is_null($league_id))
		{
			$this->_league_id = $league_id;
			$this->populate();
		}
	}

	/**
	 * Pulls relevant league info from the database into member variables
	 *
	 * populate() is only useful if {@link $_league_id} has been set in advance.  If not, an error is logged.
	 */
	function populate()
	{
		if(is_null($this->_league_id)) 
		{
			$this->_emsg = "\$_league_id is not set.  Cannot populate OFFL_League object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT * FROM leagues WHERE league_id=" . $this->_league_id;
		$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.";
			error_log ($this->_emsg);
			return FALSE;
		}
//		$this->_user_id_array = explode(",", mysql_result($result,0,"user_ids"));
//		$this->_fflteam_id_array = explode(",", mysql_result($result,0,"fflteam_ids"));
		$this->_league_name = htmlspecialchars(stripslashes(mysql_result($result, 0, "league_name")));
		$this->_league_abbv = htmlspecialchars(stripslashes(mysql_result($result, 0, "league_abbv")));
		$this->_league_motto = htmlspecialchars(stripslashes(mysql_result($result, 0, "league_motto")));
		$this->_commish_email = htmlspecialchars(stripslashes(mysql_result($result, 0, "commish_email")));
		$this->_public = mysql_result($result, 0, "public");
		mysql_free_result($result);

		$sql = "SELECT fflteam_id, user_id FROM fflteams WHERE league_id=" . $this->_league_id;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			$this->_user_id_array[] = mysql_result($result,$i,"user_id");
			$this->_fflteam_id_array[] = mysql_result($result,$i,"fflteam_id");
		}
		mysql_free_result($result);

		$sql = "SELECT division_id FROM divisions WHERE league_id=". $this->_league_id;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{	$this->_division_id_array[] = mysql_result($result,$i,"division_id");	}
		mysql_free_result($result);

		return TRUE;
	}

	/**
	 * Saves league info to the database
	 *
	 * 
	 */
	function save()
	{
		if(is_null($this->_league_id))
		{
			// insert
			$sql = "INSERT INTO leagues (league_name, league_abbv, league_motto, commish_email, public) VALUES ('" . mysql_escape_string($this->_league_name) . "', '" . mysql_escape_string($this->_league_abbv) . "', '" . mysql_escape_string($this->_league_motto) . "', '" . mysql_escape_string($this->_commish_email) . "', " . $this->_public . ")";
		}
		else
		{
			// update
			$sql = "UPDATE leagues SET league_name='" . mysql_escape_string($this->_league_name) . "', league_abbv='" . mysql_escape_string($this->_league_abbv) . "', league_motto='" . mysql_escape_string($this->_league_motto) . "', commish_email='" . mysql_escape_string($this->_commish_email) . "', public=" . $this->_public . " WHERE league_id=" . $this->_league_id;
		}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		// populate the UID
		if (is_null($this->_league_id))
		{	$this->_league_id = mysql_insert_id();	}

		return $result;
	}

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

	/**
	 * @return array
	 */
	function getUserIDs($year = NULL)
	{
		if (isset($year) && ($year < getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			if ($pastLeague->_found)
			{	return $pastLeague->getUserIDs();	}
		}
		return $this->_user_id_array;
	}

	/**
	 * @return array
	 */
	function getFFLTeamIDs($year = NULL)
	{
		if (isset($year) && ($year < getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			if ($pastLeague->_found)
			{	return $pastLeague->getFFLTeamIDs();	}
		}
		return $this->_fflteam_id_array;
	}

	/**
	 * @param string $league_name
	 */
	function setLeagueName($league_name)
	{
		$this->_league_name = $league_name;
	}

	/**
	 * @return string
	 */
	function getLeagueName()
	{
		return $this->_league_name;
	}

	/**
	 * @param string $league_abbv
	 */
	function setLeagueAbbv($league_abbv)
	{
		$this->_league_abbv = $league_abbv;
	}

	/**
	 * @return string
	 */
	function getLeagueAbbv()
	{
		return $this->_league_abbv;
	}

	/**
	 * @param string $league_motto
	 */
	function setLeagueMotto($league_motto)
	{
		$this->_league_motto = $league_motto;
	}

	/**
	 * @return string
	 */
	function getLeagueMotto()
	{
		return $this->_league_motto;
	}

	/**
	 * @param string $commish_email
	 */
	function setCommishEmail($commish_email)
	{
		$this->_commish_email = $commish_email;
	}

	/**
	 * @return string
	 */
	function getCommishEmail()
	{
		return $this->_commish_email;
	}

	/**
	 * @param boolean
	 */
	function setPublic($public)
	{
		if ($public)
		{	$this->_public = 1;	 }
		else
		{	$this->_public = 0;	 }
	}

	/**
	 * @return integer Integral boolean (1/0)
	 */
	function getPublic()
	{
		return $this->_public;
	}

	/**
	 * Returns number of teams in this league
	 * @return integer
	 */
	function getNumberOfFFLTeams($year = NULL)
	{
		if (isset($year) && ($year < getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			if ($pastLeague->_found)
			{	return count($pastLeague->getFFLTeamIDs());	}
		}
		return sizeof($this->_fflteam_id_array);
	}

	/**
	 * Returns {@link OFFL_FFLTeam} objects
	 *
	 * If parameter is passed TRUE, returns all teams all leagues.  Otherwise returns all teams this league.
	 *
	 * @param boolean $all_leagues Optional: set TRUE for all teams regardless of league.
	 * @return array Array of {@link OFFL_FFLTeam FFL teams}
	 */
	function getAllFFLTeams($all_leagues = FALSE, $year = NULL)
	{
		if (!$all_leagues && isset($year) && ($year < getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			if ($pastLeague->getFound())
			{
				$div_ids = $pastLeague->getDivisionIDs();
				foreach ($div_ids as $div_id)
				{
					$tm_ids = $pastLeague->getFFLTeamIDs($div_id);	
					foreach ($tm_ids as $tm_id)
					{
						$team = new OFFL_FFLTeam($tm_id);
						$team->setDivisionID($div_id);
						$team->setLeagueID($this->_league_id);
						$retArr[] = $team;
					}
				}
				return $retArr;
			}
		}

		$retArr = array();
		if (!$all_leagues)
		{
			foreach ($this->_fflteam_id_array as $fflteam_id)
			{
				$retArr[] = new OFFL_FFLTeam($fflteam_id);
			}
			return $retArr;
		}
		$sql = "SELECT fflteam_id FROM fflteams ORDER BY fflteam_city, fflteam_name";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			$team = new OFFL_FFLTeam(mysql_result($result, $i, "fflteam_id"));
			$retArr[] = $team;
		}
		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Returns number of divisions in this league
	 * @return integer
	 */
	function getNumberOfDivisions($year = NULL)
	{
		if (isset($year) && ($year < getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			if ($pastLeague->_found)
			{	return count($pastLeague->getDivisionIDs());	}
		}

		return sizeof($this->_division_id_array);
	}

	/**
	 * Returns {@link OFFL_Division} objects
	 *
	 * If parameter is passed TRUE, returns all divisions in all leagues.  Otherwise returns all divisions in this league.
	 *
	 * @param boolean $all_leagues Optional: set TRUE for all divisions regardless of league.
	 * @return array Array of {@link OFFL_Division divisions}
	 */
	function getAllDivisions($all_leagues = FALSE, $year = NULL)
	{
		$retArr = array();

		if (!$all_leagues && isset($year) && ($year < getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			if ($pastLeague->_found)
			{
				$div_ids = $pastLeague->getDivisionIDs();	
				foreach ($div_ids as $div_id)
				{
					$div = new OFFL_Division($div_id);
					$div->setLeagueID($this->_league_id);
					$retArr[] = $div;
				}
				return $retArr;
			}
		}

		if (!$all_leagues)
		{
			foreach ($this->_division_id_array as $division_id)
			{
				$retArr[] = new OFFL_Division($division_id);
			}
			return $retArr;
		}
		$sql = "SELECT division_id FROM divisions";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			$division = new OFFL_Division(mysql_result($result, $i, "division_id"));
			$retArr[] = $division;
		}
		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Tests whether a given {@link OFFL_FFLTeam}, {@link OFFL_User}, or {@link OFFL_Division} is in the current league
	 *
	 * @param integer $id {@link OFFL_FFLTeam::$_fflteam_id fflteam_id}, {@link OFFL_User::$_user_id user_id}, or {@link OFFL_Division::$_division_id division_id} as appropriate
	 * @param string $select set to "FFLTEAM" to check if a team is in a league, "USER" to see if a user owns a team in a league, or "DIVISION" to see if a division is in a league.
	 * @param integer $year Optional: if not getThisYear, checks (@link OFFL_pastLeague} for info
	 * @return bool TRUE: Team/User/Division found , FALSE: Team/User/Division not found or $parameter(s) invalid
	 */
	function inLeague($id, $select, $year=NULL)
	{
		if (isset($year) && ($year != getThisYear()))
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
		}

		if (isset($pastLeague) && $pastLeague->_found) // old league is archived, refer there
		{
			switch ($select)
			{
				case "FFLTEAM":
					if (in_array($id, $pastLeague->getFFLTeamIDs()))
					{	return TRUE;	}
					return FALSE;
					break;
				case "USER":
					if (in_array($id, $pastLeague->getUserIDs()))
					{	return TRUE;	}
					return FALSE;
					break;
				case "DIVISION":
					if (in_array($id, $$pastLeague->getDivisionIDs()))
					{	return TRUE;	}
					return FALSE;
					break;
				default:
					return FALSE;
			}
		}

		switch ($select)
		{
			case "FFLTEAM":
				if (in_array($id, $this->_fflteam_id_array))
				{	return TRUE;	}
				return FALSE;
				break;
			case "USER":
				if (in_array($id, $this->_user_id_array))
				{	return TRUE;	}
				return FALSE;
				break;
			case "DIVISION":
				if (in_array($id, $this->_division_id_array))
				{	return TRUE;	}
				return FALSE;
				break;
			default:
				return FALSE;
		}
	}

	/**
	 * Returns all registered OFFL_Leagues
	 *
	 * @param string $retType Optional: set "ID" to return league_ids only, default returns OFFL_League objects
	 * @return array Array of integers or {@link OFFL_League} objects
	 */
	function getAllLeagues($retType="LEAGUE")
	{
		$retArr = array();
		$sql = "SELECT league_id FROM leagues";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			if ($retType == "ID")
			{	$retArr[] = mysql_result($result, $i, "league_id");	 	}
			else
			{
				$league = new OFFL_League(mysql_result($result, $i, "league_id"));
				$retArr[] = $league;
			}
		}
		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Looks up players cross-referenced with various criteria.
	 *
	 * This function combines the functionality of lots of old (and mostly redundant) functions.  New parameters are a bit
	 * heavy but I think the overall streamlined code is worthwhile.
	 * Replaced:
	 * <ul><li>getAllActivePlayers</li><li>getAllActiveFreeAgentPlayers</li><li>getAllPlayers</li><li>getAllPlayersByPosition</li></ul>
	 *
	 * @param integer $roster Optional: Default 0 returns all players.  1 returns only players on rosters.  2 returns only players not on rosters (players on waivers and free agents).  Combine with $waiver_status to get only free agents.
	 * @param integer $waiver_status Optional: Default 0 returns all players.  1 returns only players on waivers.  2 returns only players not on waivers (free agents and players on rosters).  Combine with $roster to get only free agents.
	 * @param integer $position_id Optional: Default 0 returns all players.  Otherwise returns only players with given {@link $_position_id}.
	 * @param string $orderby Optional: Default "name" sorts by name (lname, fname).  Otherwise, sorts by specified column, and then name.
	 * @param boolean $inactive Optional: Default FALSE returns only active players.  If TRUE, returns all inactive (as well as active) players.
	 * @return array Returns an array of {@link OFFL_Player} objects
	 */
	function getPlayers($roster=0, $waiver_status=0, $position_id=0, $inactive=FALSE, $orderby="name")
	{
		$player_id_array = array();
		$retArr = array();
		$sql = "SELECT player_id FROM players";
		if ($position_id > 0)
		{
			$sql .= " WHERE position_id=" . $position_id;
			if (!$inactive)
			{	$sql .= " AND active=1";	}
		}
		elseif (!$inactive)
		{	$sql .= " WHERE active=1";	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			$player_id_array[] = mysql_result($result, $i, "player_id");
		}
		mysql_free_result($result);

		switch ($roster)
		{
			case 1:
				$players_on_rosters = array();
				$this->SQLQuery("SELECT player_id FROM `rosterplayers` WHERE league_id=" . $this->_league_id);
				$num_players = $this->SQLNumRows();
				for ($i = 0; $i < $num_players; $i++)
				{	$players_on_rosters[] = $this->SQLResult($i, "player_id");	}
				
//				$teams = $this->getAllFFLTeams();
//				$players_on_rosters = array();
//				foreach ($teams as $team)
//				{	$players_on_rosters = array_merge($players_on_rosters, $team->getRosterIDs(TRUE));	}

				$player_id_array = array_intersect($player_id_array, $players_on_rosters);
				unset ($players_on_rosters);

				break;
			case 2:
				$players_on_rosters = array();
				$this->SQLQuery("SELECT player_id FROM `rosterplayers` WHERE league_id=" . $this->_league_id);
				$num_players = $this->SQLNumRows();
				for ($i = 0; $i < $num_players; $i++)
				{	$players_on_rosters[] = $this->SQLResult($i, "player_id");	}

//				$teams = $this->getAllFFLTeams();
//				$players_on_rosters = array();
//				foreach ($teams as $team)
//				{	$players_on_rosters = array_merge($players_on_rosters, $team->getRosterIDs(TRUE));	}

				$player_id_array = array_diff($player_id_array, $players_on_rosters);
				unset ($players_on_rosters);

				break;
		}
		switch ($waiver_status)
		{
			case 1:
				$ws = new OFFL_WaiverPlayer();
				$waiver_player_ids = array();
				$waiver_players = $ws->getAllWaiverPlayers($this->_league_id);
				foreach ($waiver_players as $wplayer)
				{	$waiver_player_ids[] = $wplayer->getPlayerID();	}
				unset ($waiver_players);

				$player_id_array = array_intersect($player_id_array, $waiver_player_ids);
				unset ($waiver_player_ids);

				break;
			case 2:
				$ws = new OFFL_WaiverPlayer();
				$waiver_player_ids = array();
				$waiver_players = $ws->getAllWaiverPlayers($this->_league_id);
				foreach ($waiver_players as $wplayer)
				{	$waiver_player_ids[] = $wplayer->getPlayerID();	}
				unset ($waiver_players);

				$player_id_array = array_diff($player_id_array, $waiver_player_ids);
				unset ($waiver_player_ids);

				break;
		}

		switch ($orderby)
		{
			case "position":
				$orderby_sql = "position_abbv, ";
				break;
			case "team":
				$orderby_sql = "nflteam_abbv, ";
				break;
			default:
				$orderby_sql = "";
		}
		$orderby_sql .= "lname, fname";

		$sql = "SELECT * FROM `players` LEFT JOIN positions " .
			"ON players.position_id=positions.position_id " .
			"LEFT JOIN nflteams " .
			"ON players.nflteam_id=nflteams.nflteam_id";
		if ($position_id > 0)
		{
			$sql .= " WHERE players.position_id=" . $position_id;
			if (!$inactive)
			{	$sql .= " AND active=1";	}
		}
		elseif (!$inactive)
		{	$sql .= " WHERE active=1";	}
		$sql .= " ORDER BY " . $orderby_sql;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
	    while ($row = mysql_fetch_assoc($result))
		{
			if( in_array($row["player_id"], $player_id_array) )
			{
				$player = new OFFL_Player();
				$player->populateBySQLRow($row, $this->_league_id);
				$retArr[] = $player;
			}
		}

//		foreach ($player_id_array as $player_id)
//		{	$retArr[] = new OFFL_Player($player_id, $this->_league_id);	}

		return $retArr;
	}

} // end OFFL_League class

?>
Return current item: Online Fantasy Football League