Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_player.php
<?php
/**
 * Defines the {@link OFFL_Player} class and associated sorting functions.
 *
 * @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_league.php");
require_once($DOC_ROOT . "/lib/classes/offl_fflteam.php");
require_once($DOC_ROOT . "/lib/classes/offl_nflteam.php");
/**
 * Class for referencing NFL players.
 * 
 * Internal members include league-specific info (such as associated FFL team) cross-referenced from elsewhere in the database.
 *
 * @package offl
 */
class OFFL_Player extends OFFL_DBObject
{
	// LEAGUE-GENERIC DATA
	/**
	 * @var integer
	 */
	var $_player_id = NULL;
	/**
	 * @var string
	 */
	var $_lname = NULL;
	/**
	 * @var string
	 */
	var $_fname = NULL;
	/**
	 * @var integer
	 */
	var $_nflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_position_id = NULL;
	/**
	 * @var string Read-only
	 */
	var $_position_name = NULL;
	/**
	 * @var string Read-only
	 */
	var $_position_abbv = NULL;
	/**
	 * @var string Read-only
	 */
	var $_nflteam_city = NULL;
	/**
	 * @var string Read-only
	 */
	var $_nflteam_name = NULL;
	/**
	 * @var string Read-only
	 */
	var $_nflteam_abbv = NULL;
	/**
	 * @var integer Treat as numeric boolean (0/1)
	 */
	var $_active = 0;
	/**
	 * @var string
	 */
	var $_nfl_profile = NULL;

	// LEAGUE-SPECIFIC DATA
	/**
	 * @var integer League-specific
	 */
	var $_fflteam_id = NULL;
	/**
	 * @var string League-specific
	 */
	var $_fflteam_city = NULL;
	/**
	 * @var string League-specific
	 */
	var $_fflteam_name = NULL;
	/**
	 * @var integer League-specific -- Treat as numeric boolean (0/1)
	 */
	var $_starter = 0;
	/**
	 * @var string League-specific -- Stored as GMT SQL DATETIME format (yy-mm-dd hh:mm:ss, or "Y-m-d H:i:s" for {@link gmdate() gmdate()})
	 */
	var $_waiver_status = NULL;
	/**
	 * @var integer League-specific -- Treat as numeric boolean (0/1)
	 */
	var $_tradelock = 0;

	/**
	 * Constructor, initializes member data based on parameters
	 *
	 * @param integer $player_id Optional: loads league-generic player info if set
	 * @param integer $league_id Optional: loads league-specific player info if set
	 * @see populate()
	 * @see populateLeagueSpecific()
	 */
	function OFFL_Player ($player_id = NULL, $league_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

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

	/**
	 * Pulls league-generic info from database
	 */
	function populate()
	{
		if(is_null($this->_player_id))
		{
			$this->_emsg = "\$_player_id is not set. Cannot populate OFFL_Player object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Left outer join glory! w00t!
		$sql = "SELECT * " .
		"FROM players " .
		"LEFT JOIN positions " .
		"ON players.position_id=positions.position_id " .
		"LEFT JOIN nflteams " .
		"ON players.nflteam_id=nflteams.nflteam_id " .
		"WHERE player_id='" . $this->_player_id . "'";

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No player records found for player_id $this->_player_id.";
			error_log ($this->_emsg);
			return FALSE;
		}

		$this->_lname = htmlspecialchars(stripslashes(mysql_result($result,0,"lname")));
		$this->_fname = htmlspecialchars(stripslashes(mysql_result($result,0,"fname")));
		$this->_active = mysql_result($result,0,"active");
		$this->_nflteam_id = mysql_result($result,0,"nflteam_id");
		$this->_position_id = mysql_result($result,0,"position_id");
		$this->_nfl_profile = htmlspecialchars(stripslashes(mysql_result($result,0,"nfl_profile")));
		$this->_nflteam_city = htmlspecialchars(stripslashes(mysql_result($result,0,"nflteam_city")));
		$this->_nflteam_name = htmlspecialchars(stripslashes(mysql_result($result,0,"nflteam_name")));
		$this->_nflteam_abbv = htmlspecialchars(stripslashes(mysql_result($result,0,"nflteam_abbv")));
		$this->_position_abbv = htmlspecialchars(stripslashes(mysql_result($result,0,"position_abbv")));
		$this->_position_name = htmlspecialchars(stripslashes(mysql_result($result,0,"position_name")));
		
		return TRUE;
	}

	/**
	 * Populates a player object based off a result row from the database
	 *
	 * @parameter array $row The result of a call to mysql_fetch_assoc()
	 * @parameter integer $league_id Optional: if set, populates league-specific info
	 */
	function populateBySQLRow($row, $league_id=NULL)
	{
		$this->_player_id = $row["player_id"];
		$this->_lname = htmlspecialchars(stripslashes($row["lname"]));
		$this->_fname = htmlspecialchars(stripslashes($row["fname"]));
		$this->_active = $row["active"];
		$this->_nflteam_id = $row["nflteam_id"];
		$this->_position_id = $row["position_id"];
		$this->_nfl_profile = htmlspecialchars(stripslashes($row["nfl_profile"]));
		$this->_nflteam_city = htmlspecialchars(stripslashes($row["nflteam_city"]));
		$this->_nflteam_name = htmlspecialchars(stripslashes($row["nflteam_name"]));
		$this->_nflteam_abbv = htmlspecialchars(stripslashes($row["nflteam_abbv"]));
		$this->_position_abbv = htmlspecialchars(stripslashes($row["position_abbv"]));
		$this->_position_name = htmlspecialchars(stripslashes($row["position_name"]));
		if (isset($league_id))
		{
			$this->populateLeagueSpecific($league_id);
		}
	}

	/**
	 * Pulls league-specific info (like Team / Waiver / Trade status, etc) from database
	 *
	 * gets FFL Team parameters as well as starter, waiver status, trade lock.
	 * NOTE -- WAIVERS / TRADES NOT DONE
	 * @param integer $league_id
	 * return boolean TRUE if found, FALSE if free agent
	 */
	function populateLeagueSpecific($league_id)
	{
		// get all FFL teams in $state->getLeagueID()
		// find player_id in FFL teams (if present)
		// set fflteam_id (0 if not found?)
		// set starter
		// pull FFLTeam info with fflteam_id if found
		// look up waiver / trade status by $state->getLeagueID()
		if ($league_id == 0)
		{	return FALSE;	}

		// NEW LOOKUP FUNCTION BEGINS HERE

		$RP = new OFFL_RosterPlayer($this->_player_id, $league_id);
		if ($RP === FALSE)
		{	return FALSE;	}
		$fflteam = new OFFL_FFLTeam($RP->getFFLTeamID());
		$this->_starter = $RP->getStarter();
		$this->_fflteam_id = $fflteam->getFFLTeamID();
		$this->_fflteam_city = $fflteam->getFFLTeamCity();
		$this->_fflteam_name = $fflteam->getFFLTeamName();

		// NOW OLD VERSION CONTINUES

		// look up waivers
		$wp = new OFFL_WaiverPlayer($this->_player_id, $league_id);
		$this->_waiver_status = $wp->getWaiverDeadline();
		unset($wp);
		// look up trades
		$tp = new OFFL_TradePlayer($this->_player_id, $league_id);
		$this->_tradelock = $tp->getTradelock();
		unset($tp);

		return TRUE;
	}

	/**
	 * Saves player info into database
	 *
	 * Determines SELECT vs INSERT based on state of {@link $_player_id}
	 */
	function save()
	{
		if (is_null($this->_player_id))
		{
			$sql = "INSERT INTO players (fname, lname, nfl_profile, nflteam_id, position_id, active) VALUES ('" . mysql_escape_string($this->_fname) . "', '" . mysql_escape_string($this->_lname) . "', '" . mysql_escape_string($this->_nfl_profile) . "', " . $this->_nflteam_id . ", " . $this->_position_id . ", " . $this->_active . ")";
		}
		else
		{
			$sql = "UPDATE players SET fname='" . mysql_escape_string($this->_fname) . "', lname='" . mysql_escape_string($this->_lname) . "', nfl_profile='" . mysql_escape_string($this->_nfl_profile) . "', nflteam_id=" . $this->_nflteam_id . ", position_id=" . $this->_position_id . ", active=" . $this->_active . ", WHERE player_id=" . $this->_player_id;
		}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

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

// ****************
// GENERIC READ/WRITE DATA
// ****************

	/**
	 * @param string $fname
	 */
	function setFirstName($fname)
	{
		$this->_fname = $fname;
	}

	/**
	 * @return string
	 */
	function getFirstName()
	{
		return $this->_fname;
	}

	/**
	 * @param string $lname
	 */
	function setLastName($lname)
	{
		$this->_lname = $lname;
	}

	/**
	 * @return string
	 */
	function getLastName()
	{
		return $this->_lname;
	}

	/**
	 * Returns formatted name string, order determined by parameter
	 *
	 * Default behavior is "Last, First", but may also be set to return "First Last".
	 * @param string $order Optional: set "FL" to get "First Last" output
	 * @return string Player name concatenation
	 */
	function getName($order="LF")
	{
		if (strlen($this->_fname) && strlen($this->_lname) && ($order == "LF"))
		{
			return trim($this->_lname . ", " . $this->_fname);
		}
		return trim($this->_fname . " " . $this->_lname);
	}
	
	/**
	 * @param string $nfl_profile
	 */
	function setNFLProfile($nfl_profile)
	{
		$this->_nfl_profile = $nfl_profile;
	}

	/**
	 * @return string
	 */
	function getNFLProfile()
	{
		return $this->_nfl_profile;
	}

	/**
	 * @param integer $nflteam_id
	 */
	function setNFLTeamID($nflteam_id)
	{
		$this->_nflteam_id = $nflteam_id;
	}

	/**
	 * @return integer
	 */
	function getNFLTeamID()
	{
		return $this->_nflteam_id;
	}

	/**
	 * @param integer $position_id
	 */
	function setPositionID($position_id)
	{
		$this->_position_id = $position_id;
	}

	/**
	 * @return integer
	 */
	function getPositionID()
	{
		return $this->_position_id;
	}

	/**
	 * @param boolean $active Uses integral truth values (1/0)
	 */
	function setActive($active)
	{
		if ($active)
		{	$this->_active = 1;	 }
		else
		{	$this->_active = 0;	 }
	}

	/**
	 * @return boolean Uses integral truth values (1/0)
	 */
	function getActive()
	{
		return $this->_active;
	}

// *************
// GENERIC READ-ONLY DATA
// *************

	/**
	 * @return string This is read-only data from this class.
	 */
	function getNFLTeamCity()
	{
		return $this->_nflteam_city;
	}

	/**
	 * @return string This is read-only data from this class.
	 */
	function getNFLTeamName()
	{
		return $this->_nflteam_name;
	}

	/**
	 * @return string This is read-only data from this class.
	 */
	function getNFLTeamAbbv()
	{
		return $this->_nflteam_abbv;
	}

	/**
	 * This function was formerly getPositionDesc().  Renamed for better consistency.
	 *
	 * @return string This is read-only data from this class.
	 */
	function getPositionName()
	{
		return $this->_position_name;
	}

	/**
	 * @return string This is read-only data from this class.
	 */
	function getPositionAbbv()
	{
		return $this->_position_abbv;
	}

	/**
	 * While this data is not saved to the OFFL_Player class, it's useful to place here for roster changes
	 * @param boolean $starter Uses integral truth values (1/0)
	 */
	function setStarter($starter)
	{
		if ($starter)
		{	$this->_starter = 1;	}
		else
		{	$this->_starter = 0;	}
	}

	/**
	 * @return boolean Uses integral truth values (1/0).
	 */
	function getStarter()
	{
		return $this->_starter;
	}

// ***************
// LEAGUE-SPECIFIC DATA (READ-ONLY)
// ***************

	/**
	 * @return integer This data is read-only from this class.
	 */
	function getFFLTeamID()
	{
		return $this->_fflteam_id;
	}

	/**
	 * @return string This data is read-only from this class.
	 */
	function getFFLTeamCity()
	{
		return $this->_fflteam_city;
	}

	/**
	 * @return string This data is read-only from this class.
	 */
	function getFFLTeamName()
	{
		return $this->_fflteam_name;
	}

	/**
	 * @return integer Data derived from {@link gmmktime() gmmktime()}.  This data is read-only from this class.
	 */
	function getWaiverStatus()
	{
		return $this->_waiver_status;
	}

	/**
	 * @return boolean Uses integral truth values (1/0).  This data is read-only from this class.
	 */
	function getTradelock()
	{
		return $this->_tradelock;
	}
	
	/**
	 * return string Full FFL team name with proper whitespace.
	 */
	function getFFLTeamFullName()
	{
		return trim($this->_fflteam_city . " " . $this->_fflteam_name);
	}

// ***************
// CLASS FUNCTIONALITY
// ***************

	/**
	 * Looks up and returns a player on the basis of profile.
	 *
	 * This function is used primarily by the stats aggregator.
	 * @param string $profile The full address (http to .html) of a player's NFL.com profile.
	 * @return OFFL_Player
	 */
	function getPlayerByNFLProfile($profile)
	{
		$sql = "select player_id from players where nfl_profile='" . $profile . "'";
		
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		if (mysql_num_rows($result) > 0)
			$player = new OFFL_Player(mysql_result($result, 0, "player_id"));
		else
			$player = FALSE;
		mysql_free_result($result);
		return $player;
	}
	
	/**
	 * Looks up and returns a team defense on the basis of abbreviation.
	 *
	 * This function is used primarily by the stats aggregator.
	 * @param string $abbv should be a valid NFL team abbreviation (see NFL.com for valid abbreviations)
	 * @return OFFL_Player
	 */
	function getDefenseByNFLTeamAbbv($abbv)
	{
		$x = new OFFL_NFLTeam();
		$team = $x->getNFLTeamByAbbv($abbv);
		$sql = "SELECT player_id FROM players WHERE nflteam_id=" . $team->getNFLTeamID() . " AND lname='" . $team->getNFLTeamName() . "'";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		$player = new OFFL_Player(mysql_result($result, 0, "player_id"));
		mysql_free_result($result);
		
		return $player;
	}

} // end OFFL_Player class

// Functions for sorting the OFFL_Player class

/**
 * Sorts {@link OFFL_Player} objects by position.  For use with {@link usort() usort()}, et al.
 */
function cmp_players ($p1, $p2)
// sort function for players by position (for roster checks)
{
	if ($p1->getPositionID() == $p2->getPositionID())
		return 0;
	return ($p1->getPositionID() < $p2->getPositionID()) ? -1 : 1;
}

/**
 * Sorts {@link OFFL_Player} objects by position.  For use with {@link usort() usort()}, et al.
 */
function cmp_players_starter($p1, $p2)
{
	if ($p1->getStarter() > $p2->getStarter())
		return -1;
	if ($p1->getStarter() < $p2->getStarter())
		return 1;
	if ($p1->getPositionID() > $p2->getPositionID())
		return 1;
	if ($p1->getPositionID() < $p2->getPositionID())
		return -1;
	$lname = strcasecmp($p1->getLastName(), $p2->getLastName());
	if ($lname != 0)
		return $lname;
	return strcasecmp($p1->getFirstName(), $p2->getFirstName());
}

/**
 * Sorts {@link OFFL_Player} objects by position.  For use with {@link usort() usort()}, et al.
 */
function cmp_players_waiver_deadline ($p1, $p2)
{
	if ($p1->getWaiverStatus() == $p2->getWaiverStatus())
		return 0;
	return ($p1->getWaiverStatus() < $p2->getWaiverStatus()) ? -1 : 1;
}
?>
Return current item: Online Fantasy Football League