Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_waiverplayer.php
<?php
/**
 * Contains the implementation of the {@link OFFL_WaiverPlayer} 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 which players are on waivers (and for how long) in particular league.
 *
 * @package offl
 */
class OFFL_WaiverPlayer extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_player_id = NULL;
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var string SQL DATETIME format, GMT
	 */
	var $_waiver_deadline = NULL;

	/**
	 * Constructor
	 *
	 * Both parameters should be set together or not at all
	 * @param integer $player_id Optional: If set, loads waiver from database
	 * @param integer $league_id Optional: If set, loads waiver from database
	 * @see populate()
	 */
	function OFFL_WaiverPlayer ($player_id = NULL, $league_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

	    if(isset($player_id) && isset($league_id))
		{
			$this->_player_id = $player_id;
			$this->_league_id = $league_id;
			$this->populate();
		}
	}

	/**
	 * Pulls info from database.  {@link $_player_id} and {@link $_league_id} must be set.
	 *
	 * @return boolean TRUE if successful, FALSE otherwise.
	 */
	function populate ()
	{
		if(is_null($this->_player_id) || is_null($this->_league_id))
		{
			$this->_emsg = "Control values are not set.  Cannot populate OFFL_WaiverPlayer object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT waiver_deadline FROM waiverplayers WHERE player_id=" . $this->_player_id . " AND 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 waiver/player records found for player_id " . $this->_player_id . " and league_id " . $this->_league_id . ".";
// Error disabled, as this may be used as a boolean test.
//	        error_log ($this->_emsg);
			return FALSE;
		}
		$this->_waiver_deadline = mysql_result($result,0,"waiver_deadline");
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves the transaction to the database
	 *
	 * @return boolean TRUE if successful, FALSE otherwise
	 */
	function save ()
	{
		if (is_null($this->_player_id) || is_null($this->_league_id))
		{	return FALSE;	}
		
		$sql = "INSERT INTO waiverplayers (player_id, league_id, waiver_deadline) VALUES (" . $this->_player_id . ", " . $this->_league_id . ", '" . $this->_waiver_deadline . "')";

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

	/**
	 * Deletes this player/waiver note
	 *
	 * {@link $_player_id} and {@link $_league_id} must be set
	 *
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function deleteWaiverPlayer()
	{
		if (isset($this->_player_id) && isset($this->_league_id))
		{
			$sql = "DELETE FROM waiverplayers WHERE player_id=" . $this->_player_id . " AND league_id=" . $this->_league_id;
			$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
			return TRUE;
		}
		$this->_emsg = "Unable to delete waiver info: control values not defined.";
        error_log ($this->_emsg);
		return FALSE;
	}


	/**
	 * @param integer $player_id
	 */
	function setPlayerID($player_id)
	{
		$this->_player_id = $player_id;
	}

	/**
	 * @return integer
	 */
	function getPlayerID()
	{
		return $this->_player_id;
	}

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

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

	/**
	 * Sets internal value to {@link gmdate() gmdate} output in SQL DATETIME format
	 *
	 * The waiver wire time is automatically applied by this function.
	 *
	 * @param integer $deadline Timestamp, GMT
	 */
	function setWaiverDeadline($deadline)
	{
		$control = new OFFL_Control($this->_league_id);
		$ww_hours = $control->getValue("CONFIG_WW_HOURS");
		$deadline += 3600 * $ww_hours; // append ww_hours * min/hr * sec/min

		$this->_waiver_deadline = gmdate("Y-m-d H:i:s", $deadline);
	}

	/**
	 * Returns output from {@link gmmktime() gmmktime} corresponding to the SQL DATETIME format
	 * @return integer Timestamp, GMT
	 */
	function getWaiverDeadline()
	{
		if (is_null($this->_waiver_deadline))
		{	return $this->_waiver_deadline;	}
		// assemble DATETIME format into UNIX timestamp
		list($date, $time) = explode(" ", $this->_waiver_deadline);
		list($Y, $M, $D) = explode("-", $date);
		list($h, $i, $s) = explode(":", $time);
		$time = gmmktime($h, $i, $s, $M, $D, $Y);
		return $time;
	}

	/**
	 * Looks up all players on waivers, optionally for a single league
	 * @param integer $league_id Optional: if set, returns claims only for this league
	 * @return array Array of {@link OFFL_WaiverPlayer} objects
	 */
	function getAllWaiverPlayers($league_id=NULL)
	{
		$retArr = array();
		$sql = "SELECT player_id, league_id FROM waiverplayers WHERE waiver_deadline IS NOT NULL";
		if (isset($league_id))
		{	$sql .= " AND league_id=" . $league_id;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$w = new OFFL_WaiverPlayer(mysql_result($result,$i,"player_id"), mysql_result($result,$i,"league_id"));
			array_push($retArr, $w);
		}
		mysql_free_result($result);

		return $retArr;
	}

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