Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_transaction.php
<?php
/**
 * Defines the {@link OFFL_Transaction} 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_transactionitem.php");
/**
 * Defines interfaces for transaction information
 *
 * This class does not actually enable the transaction process, it merely documents it.  Additionally, it relies
 * heavily on the {@link OFFL_TransactionItem} class to fully document player data.
 *
 * @see OFFL_TransactionItem
 * @package offl
 */
class OFFL_Transaction extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_trans_id = NULL;
	/**
	 * @var string SQL DATETIME format, GMT
	 */
	var $_trans_datetime = NULL;
	/**
	 * @var string
	 */
	var $_trans_note = NULL;
	/**
	 * I need to document allowed (useful) values of this variable
	 * @var string
	 */
	var $_trans_type = NULL;
	/**
	 * @var integer
	 */
	var $_league_id = NULL;

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

		if(isset($trans_id))
		{
			$this->_trans_id = $trans_id;
			$this->populate();
		}
	}

	/**
	 * Pulls info from database.  {@link $_trans_id} must be set.
	 * @return boolean TRUE if successful, FALSE otherwise.
	 */
	function populate ()
	{
		if(is_null($this->_trans_id))
		{
			$this->_emsg = "\$_trans_id is not set.	Cannot populate Transaction object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT * FROM transactions WHERE trans_id=" . $this->_trans_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 " . $this->_trans_id . ".";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_trans_datetime = mysql_result($result,0,"trans_datetime");
		$this->_trans_note = htmlspecialchars(stripslashes(mysql_result($result,0,"trans_note")));
		$this->_trans_type = htmlspecialchars(stripslashes(mysql_result($result,0,"trans_type")));
		$this->_league_id = mysql_result($result,0,"league_id");
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves the transaction to the database
	 *
	 * Parameter determines "INSERT" vs "UPDATE" mode.  With UPDATE, the trans_datetime 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($mode == "add")
		{
			$sql = "INSERT INTO transactions (trans_datetime, trans_note, trans_type, league_id) VALUES ('" . gmdate("Y-m-d H:i:s", time()) . "', '" . mysql_escape_string($this->_trans_note) . "', '" . mysql_escape_string($this->_trans_type) . "', " . $this->_league_id . ")";
		}
		elseif($mode == "edit")
		{	// timestamp is not updated.  Why?  Accidental omission?
			$sql = "UPDATE transactions SET trans_note='" . mysql_escape_string($this->_trans_note) . "', trans_type='" . mysql_escape_string($this->_trans_type) . "', league_id=" . $this->_league_id . " WHERE trans_id=" . $this->_trans_id;
		}
		else { return FALSE; }

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

		return $result;
	}

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

	/**
	 * Sets internal value to {@link gmdate() gmdate} output in SQL DATETIME format
	 * @param integer $datetime Timestamp, GMT
	 */
	function setTransDatetime($datetime)
	{
		if (is_null($datetime))
		{
			$this->_datetime = NULL;
		}
		else
		{
			$this->_datetime = gmdate("Y-m-d H:i:s", $datetime);
		}
	}

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

	/**
	 * @param string $trans_note
	 */
	function setTransNote($trans_note)
	{
		$this->_trans_note = $trans_note; 
	}

	/**
	 * @return string
	 */
	function getTransNote()
	{
		return $this->_trans_note;
	}

	/**
	 * I need to document the allowed strings
	 * @param string $trans_type
	 */
	function setTransType($trans_type)
	{
		$this->_trans_type = $trans_type; 
	}

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

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

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

	/**
	 * Returns all transaction items related to this transaction
	 * @return array Array of {@link OFFL_TransactionItem} objects
	 */
	function getTransItems()
	{
		$retArr = array();
		$sql = "SELECT transitem_id FROM transactionitems WHERE trans_id=" . $this->_trans_id;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$ti = new OFFL_TransactionItem(mysql_result($result,$i,"transitem_id"));
			array_push($retArr, $ti);
		}

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Returns all transactions, optionally modified by league and year
	 *
	 * I think this function should be superceded by {@link getTransactionsByType()}
	 * @see getTransactionsByType()
	 * @param integer $league_id Optional: if set, returns only transactions from given league
	 * @param integer $year Optional: if set, returns only transactions from given year
	 * @return array Array of OFFL_Transaction objects
	 */
	function getAllTransactions($league_id = NULL, $year = NULL)
	{
		$retArr = array();
		$sql = "SELECT trans_id FROM transactions";
		if(isset($league_id))
		{
			$sql .= " WHERE league_id=" . $league_id;
			if(isset($year))
			{	$sql .= " AND year(trans_datetime)=" . $year;	}
		}
		$sql .= " ORDER BY trans_datetime DESC";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$trans = new OFFL_Transaction(mysql_result($result,$i,"trans_id"));
			array_push($retArr, $trans);
		}

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Returns all transactions, optionally modified by type, league, and year
	 *
	 * I need to document allowable {@link $_trans_type trans_type} values
	 * @param string $transtype Optional: defaults to "all", otherwise returns only transactions with the matching trans_type string
	 * @param integer $league_id Optional: if set, returns only transactions from given league
	 * @param integer $year Optional: if set, returns only transactions from given year
	 * @return array Array of OFFL_Transaction objects
	 */
	function getTransactionsByType($transtype="all", $league_id = NULL, $year = NULL)
	{
		$retArr = array();
		$sql = "select trans_id from transactions WHERE trans_type";
		if ($transtype == "all")
		{	$sql .= " LIKE '%'";	}
		else
		{	$sql .= "='" . mysql_escape_string($transtype) . "'";	}
		if(isset($league_id))
		{
			$sql .= " AND league_id=" . $league_id;
			if(isset($year))
			{	$sql .= " AND year(trans_datetime)=" . $year;	}
		}
		$sql .= " ORDER BY trans_datetime DESC";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$trans = new OFFL_Transaction(mysql_result($result,$i,"trans_id"));
			array_push($retArr, $trans);
		}

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Returns years with transactions, optionally restricted to a single league
	 * @param integer $league_id Optional: if set, restricts results to the given league
	 * @return array Array of integers
	 */
	function getTransactionYears($league_id = NULL)
	{
		$retArr = array();
		$sql = "SELECT DISTINCT year(trans_datetime) AS year FROM transactions";
		if (isset($league_id))
		{	$sql .= " WHERE league_id=" . $league_id;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$yr = mysql_result($result,$i,"year");
			array_push($retArr, $yr);
		}

		mysql_free_result($result);

		return $retArr;
	}

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