Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_transactionitem.php
<?php
/**
 * Defines the {@link OFFL_TransactionItem} 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_transaction.php");
require_once($DOC_ROOT . "/lib/classes/offl_player.php");
require_once($DOC_ROOT . "/lib/classes/offl_fflteam.php");
/**
 * Defines interfaces for individual player movements within transactions
 *
 * A transaction is composed of potentially many related transaction items, each corresponding to a single player involved
 * in the transaction.  While a "drop" will have only one TI, a trade might easily have 4 or 6.  This class provides the
 * detailed interface to the status of each player individually.
 *
 * @package offl
 */
class OFFL_TransactionItem extends OFFL_DBObject
{
	/**
	 * @var integer
	 * @see getTransItemID()
	 */
	var $_transitem_id = NULL;
	/**
	 * @var integer
	 */
	var $_trans_id = NULL;
	/**
	 * @var integer
	 */
	var $_player_id = NULL;
	/**
	 * @var string
	 */
	var $_trans_type = NULL;
	/**
	 * @var integer
	 */
	var $_fromfflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_tofflteam_id = NULL;

	/**
	 * Constructor
	 *
	 * @param integer $transitem_id Optional: If set, loads transaction item from database
	 * @see populate()
	 */
	function OFFL_TransactionItem ($transitem_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(isset($transitem_id))
		{
			$this->_transitem_id = $transitem_id;
			$this->populate();
		}
	}

	/**
	 * Pulls info from database.  {@link $_transitem_id} must be set.
	 * @return boolean TRUE if successful, FALSE otherwise.
	 */
	function populate ()
	{
		if(is_null($this->_transitem_id))
		{
			$this->_emsg = "\$_transitem_id is not set.  Cannot populate OFFL_TransactionItem object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT * FROM transactionitems WHERE transitem_id=" . $this->_transitem_id;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No draft records found for transaction item " . $this->_transitem_id . ".";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_trans_id = mysql_result($result,0,"trans_id");
		$this->_player_id = mysql_result($result,0,"player_id");
		$this->_trans_type = htmlspecialchars(stripslashes(mysql_result($result,0,"trans_type")));
		$this->_fromfflteam_id = mysql_result($result,0,"fromfflteam_id");
		$this->_tofflteam_id = mysql_result($result,0,"tofflteam_id");
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves the transaction item to the database
	 *
	 * Parameter determines "INSERT" vs "UPDATE" mode.  With UPDATE, the trans_id is not altered.  Is this a mistake?
	 * I'm not sure offhand
	 * @param string $mode Optional: set to "add" (default) for INSERT or "edit" for UPDATE
	 * @return boolean TRUE if successful, FALSE otherwise
	 */
	function save ($mode="add")
	{
		if(is_null($this->_fromfflteam_id))
		{	$this->_fromfflteam_id = "NULL";	}
		if(is_null($this->_tofflteam_id))
		{	$this->_tofflteam_id = "NULL";	}

		if($mode == "add")
		{
			// insert
			$sql = "INSERT INTO transactionitems (trans_id, player_id, trans_type, fromfflteam_id, tofflteam_id) VALUES (" . $this->_trans_id . ", " . $this->_player_id . ", '" . mysql_escape_string($this->_trans_type) . "', " . $this->_fromfflteam_id . ", " . $this->_tofflteam_id . ")";
		}
		elseif($mode == "edit")
		{
			// update (don't update transaction)
			$sql = "UPDATE transactionitems SET player_id=" . $this->_player_id . ", trans_type='" . mysql_escape_string($this->_trans_type) . "', fromfflteam_id=" . $this->_fromfflteam_id . ", tofflteam_id=" . $this->_tofflteam_id . " WHERE transitem_id=" . $this->_transitem_id;
		}
		else { return FALSE; }

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		if($mode == "add")
		{	$this->_transitem_id = mysql_insert_id();	}

		return $result;
	}

	/**
	 * @return integer
	 */
	function getTransItemID()
	{
		return $this->_transitem_id;
	}

	/**
	 * @return integer
	 */
	function getTransID()
	{
		return $this->_trans_id;
	}

	/**
	 * @param integer $trans_id
	 */
	function setTransID($trans_id)
	{
		$this->_trans_id = $trans_id;
	}

	/**
	 * @return OFFL_Transaction
	 */
	function getTransaction()
	{
		return new OFFL_Transaction($this->_trans_id);
	}

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

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

	/**
	 * @return OFFL_Player
	 */
	function getPlayer()
	{
		return new OFFL_Player($this->_player_id);
	}

	/**
	 * @param string $trans_type
	 */
	function setTransType($trans_type)
	{
		$this->_trans_type = $trans_type; 
	}

	/**
	 * @return string
	 */
	function getTransType()
	{
		return $this->_trans_type;
	}

	/**
	 * @param integer $fromfflteam_id
	 */
	function setFromFFLTeamID($fromfflteam_id)
	{
		$this->_fromfflteam_id = $fromfflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getFromFFLTeamID()
	{
		return $this->_fromfflteam_id;
	}

	/**
	 * @return OFFL_FFLTeam
	 */
	function getFromFFLTeam()
	{
		return new OFFL_FFLTeam($this->_fromfflteam_id);
	}

	/**
	 * @param integer $tofflteam_id
	 */
	function setToFFLTeamID($tofflteam_id)
	{
		$this->_tofflteam_id = $tofflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getToFFLTeamID()
	{
		return $this->_tofflteam_id;
	}

	/**
	 * @return OFFL_FFLTeam
	 */
	function getToFFLTeam()
	{
		return new OFFL_FFLTeam($this->_tofflteam_id);
	}

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