Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_division.php
<?php
/**
 * Defines the {@link OFFL_Division} 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");
/**
 * Defines interfaces for info related to Fantasy League divisions.
 *
 * @package offl
 */
class OFFL_Division extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_division_id = NULL;
	/**
	 * @var string
	 */
	var $_division_desc = "";
	/**
	 * @var integer
	 */
	var $_league_id = NULL;

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

		if(isset($division_id))
		{
			$this->_division_id = $division_id;
			$this->populate();
		}
	}
	
	/**
	 * Yanks info from database
	 *
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(empty($this->_division_id))
		{
			$this->_emsg = "\$_division_id is not set.	Cannot populate OFFL_Division object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "select * from divisions where division_id=" . $this->_division_id;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No division records found for division_id $this->_division_id.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_division_desc = htmlspecialchars(stripslashes(mysql_result($result,0,"division_desc")));
		$this->_league_id = mysql_result($result,0,"league_id");
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves info into database.  Determines UPDATE or INSERT automagically.
	 */
	function save ()
	{
		if(is_null($this->_division_id))
		{
			// insert
			$sql = "INSERT INTO divisions (division_desc, league_id) VALUES ('" . mysql_escape_string($this->_division_desc) . "', " . $this->_league_id . ")";
		}
		else
		{
			// update
			$sql = "UPDATE divisions SET division_desc='" . mysql_escape_string($this->_division_desc) . "', league_id=" . $this->_league_id . " WHERE division_id=" . $this->_division_id;
		}

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

	/**
	 * @return integer
	 */
	function getDivisionID()
	{
		return $this->_division_id;
	}

	/**
	 * @param string $division_desc
	 */
	function setDivisionDesc($division_desc)
	{
		$this->_division_desc = $division_desc; 
	}

	/**
	 * @return string
	 */
	function getDivisionDesc()
	{
		return $this->_division_desc;
	}

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

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

	/**
	 * Returns division objects, optionally only from a single league
	 *
	 * @param integer $league_id Optional: if set returns divisions for that {@link OFFL_League league} only
	 * @return array Returns an array of {@link OFFL_Division} objects
	 */
	function getAllDivisions($league_id = NULL, $year = NULL)
	{
		$retArr = array();

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

		$sql = "SELECT division_id FROM divisions";
		if (isset($league_id))
		{	$sql .= " WHERE league_id=" . $league_id;	}
		$sql .= " ORDER BY division_id";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

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

		return $retArr;
	}

	/**
	 * Returns FFL teams in a given division
	 * @param integer $division_id Optional: if not set, assumes this OFFL_Division, otherwise set to desired OFFL_Division
	 * @return array Returns an array of {@link OFFL_FFLTeam} objects
	 */
	function getAllDivisionTeams($division_id=NULL, $year=NULL, $league_id=NULL)
	{
		$retArr = array();

		if (isset($division_id) && isset($year) && ($year < getThisYear()) && isset($league_id))
		{
			$pastLeague = new OFFL_PastLeague($league_id, $year);
			if ($pastLeague->_found)
			{
				$tm_ids = $pastLeague->getFFLTeamIDs($division_id);	
				foreach ($tm_ids as $tm_id)
				{
					$team = new OFFL_FFLTeam($tm_id);
					$team->setDivisionID($division_id);
					$team->setLeagueID($league_id);
					$retArr[] = $team;
				}
				return $retArr;
			}
		}

		$sql = "SELECT fflteam_id FROM fflteams WHERE division_id=";
		if (is_null($division_id))
		{	$sql .= $this->_division_id;	}
		else
		{	$sql .= $division_id;	}
		$sql .= " 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"));
			array_push($retArr, $team);
		}
	
		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Deletes a given division.
	 *
	 * This function will not alter division settings of member {@link OFFL_FFLTeam} objects; that must be done manually.
	 * @param integer $division_id Optional: if not set, assumes this OFFL_Division, otherwise set to desired OFFL_Division
	 */
	function deleteDivision($division_id = NULL)
	{
		$sql = "DELETE FROM divisions WHERE division_id=";
		if (is_null($division_id))
		{	$sql .= $this->_division_id;	}
		else
		{	$sql .= $division_id;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return TRUE;
	}
}
?>
Return current item: Online Fantasy Football League