Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_news.php
<?php
/**
 * This file defines the {@link OFFL_News} class and lookup interfaces
 *
 * @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");

/**
 * This class provides the interface to FFL news items.
 *
 * Work is needed to get this to FFL 1.0 spec, notably consideration for using user_id vs fflteam_id (or both)
 * and what info should be pulled for support purposes (like FFL Team Name)
 *
 * @package offl
 */
class OFFL_News extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_news_id = NULL;
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var string
	 */
	var $_headline = NULL;
	/**
	 * @var integer Timestamp from {@link gmtime() gmtime()}
	 */
	var $_postdate = NULL;
	/**
	 * @var string
	 */
	var $_story = NULL;
	/**
	 * @var integer
	 */
	var $_player_id = NULL;
	/**
	 * @var integer
	 */
	var $_fflteam_id = NULL;

// **************
// READ-ONLY DATA
// **************
	/**
	 * @var string Read-only
	 */
	var $_lname = NULL;
	/**
	 * @var string Read-only
	 */
	var $_fname = NULL;
	/**
	 * @var string Read-only
	 */
	var $_nfl_profile = NULL;
	/**
	 * @var string Read-only
	 */
	var $_fflteam_city = NULL;
	/**
	 * @var string Read-only
	 */
	var $_fflteam_name = NULL;

	/**
	 * Class constructor
	 *
	 * @param integer $news_id  Optional.  If set, loads news entry with given news_id from database.
	 */
	function OFFL_News ($news_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(!is_null($news_id))
		{
			$this->_news_id = $news_id;
			$this->populate();
		}
	}
	
	/**
	 * Loads relevant info from the database for this object.  {@link $_news_id} must be set.
	 * @return boolean Returns TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(is_null($this->_news_id))
		{
			$this->_emsg = "\$_news_id is not set.	Cannot populate News object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Left outer join glory! w00t!
		$sql = "select * " . 
		"from news " . 
		"left join players " . 
		"on news.player_id=players.player_id " . 
		"left join fflteams " . 
		"on news.fflteam_id=fflteams.fflteam_id " . 
		"where news_id=$this->_news_id";

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No person records found for news_id $this->_news_id.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_league_id = mysql_result($result,0,"league_id");
		$this->_headline = htmlspecialchars(stripslashes(mysql_result($result,0,"headline")));
		$this->_postdate = mysql_result($result,0,"postdate");
		$this->_story = stripslashes(mysql_result($result,0,"story"));
		$this->_player_id = mysql_result($result,0,"player_id");
		$this->_fflteam_id = mysql_result($result,0,"fflteam_id");
		$this->_user_id = mysql_result($result,0,"user_id");

		$this->_lname = htmlspecialchars(stripslashes(mysql_result($result,0,"lname")));
		$this->_fname = htmlspecialchars(stripslashes(mysql_result($result,0,"fname")));
		$this->_nfl_profile = htmlspecialchars(stripslashes(mysql_result($result,0,"nfl_profile")));
		$this->_fflteam_city = htmlspecialchars(stripslashes(mysql_result($result,0,"fflteam_city")));
		$this->_fflteam_name = htmlspecialchars(stripslashes(mysql_result($result,0,"fflteam_name")));

		mysql_free_result($result);

		return TRUE;
	}

	/**
	 * Saves info to database.
	 *
	 * will INSERT if {@link $_news_id} is not set (allowing MySQL to autoincrement), or UPDATE otherwise.
	 */
	function save ()
	{
		$gmdatetime = gmdate("Y-m-d H:i:s", time());
		if(is_null($this->_news_id))
		{
			// insert
			$sql = "INSERT INTO news (league_id, headline, postdate, story, player_id, fflteam_id, user_id) VALUES (" . $this->_league_id . ", '" . mysql_escape_string($this->_headline) . "', '" . $gmdatetime . "', '" . mysql_escape_string($this->_story) . "', " . $this->_player_id . ", " . $this->_fflteam_id . ", " . $this->_user_id . ")";
		}
		else
		{
			// update
			$sql = "UPDATE news SET league_id=" . $this->_league_id . ", headline='" . mysql_escape_string($this->_headline) . "', story='" . mysql_escape_string($this->_story) . "', player_id=" . $this->_player_id . ", fflteam_id=" . $this->_fflteam_id . ", user_id=" . $this->_user_id . " WHERE news_id=" . $this->_news_id;
		}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

	/**
	 * Deletes this news item from the database
	 */
	function deleteNews()
	{
		$sql = "DELETE FROM news WHERE news_id=" . $this->_news_id;
		$result = mysql_query($sql, $this->_conn) or die (mysql_error() . ": $sql");
	}

	/**
	 * @return integer
	 */
	function getNewsID()
	{
		return $this->_news_id;
	}

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

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

	/**
	 * @return integer Returns MySQL DATETIME format from {@link gmmktime() gmmktime($h, $i, $s, $M, $D, $Y)}
	 */
	function getPostdate()
	{
		list($date, $time) = explode(" ", $this->_postdate);
		list($Y, $M, $D) = explode("-", $date);
		list($h, $i, $s) = explode(":", $time);
		$time = gmmktime($h, $i, $s, $M, $D, $Y);
		return $time;
	}

	/**
	 * @param string $headline
	 */
	function setHeadline($headline)
	{
		$this->_headline = $headline; 
	}

	/**
	 * @return string
	 */
	function getHeadline()
	{
		return $this->_headline;
	}

	/**
	 * @param string $story
	 */
	function setStory($story)
	{
		$this->_story = $story; 
	}

	/**
	 * @return string
	 */
	function getStory()
	{
		return $this->_story;
	}

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

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

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

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

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

	/**
	 * @param integer $fflteam_id
	 */
	function setFFLTeamID($fflteam_id)
	{
		$this->_fflteam_id = $fflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getFFLTeamID()
	{
		return $this->_fflteam_id;
	}

	/**
	 * @param integer $fflteam_id
	 */
	function setUserID($user_id)
	{
		$this->_user_id = $user_id; 
	}

	/**
	 * @return integer
	 */
	function getUserID()
	{
		return $this->_user_id;
	}

	/**
	 * Retrieves multiple news items based on optional criteria
	 *
	 * This function needs a lot of consideration to get it up to spec.  Should user_id be included?  Should it replace fflteam_id?
	 *
	 * @param integer $league_id Optional: leave blank or set NULL for news from all leagues, otherwise restricts to given league_id
	 * @param integer $limit Optional: leave blank or set NULL for all relevant entries, otherwise returns $limit newest.
	 * @param integer $fflteam_id Optional: leave blank or set NULL for news from all fflteams, otherwise restricts to given fflteam_id
	 * @param integer $player_id Optional: leave blank or set NULL for news regarding all players, otherwise restricts to given player_id
	 * @return array Returns an array of {@link OFFL_News} objects
	 */
	function getAllNews($league_id=NULL, $limit=NULL, $user_id=NULL, $fflteam_id=NULL, $player_id=NULL)
	{
		$retArr = array();
		$sql = "SELECT news_id FROM news";
		if(!is_null($league_id))
		{
			$sql .= " WHERE league_id=" . $league_id;
			if (!is_null($user_id))
			{
				$sql .= " AND user_id=" . $user_id;
				if (!is_null($fflteam_id))
				{
					$sql .= " AND fflteam_id=" . $fflteam_id;
					if (!is_null($player_id))
					{	$sql .= " AND player_id=" . $player_id;	}
				}
			}
		}
		$sql .= " ORDER BY postdate DESC";
		if(!is_null($limit))
		{	$sql .= " LIMIT " . $limit;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$news = new OFFL_News(mysql_result($result,$i,"news_id"));
			array_push($retArr, $news);
		}
	
		mysql_free_result($result);

		return $retArr;
	}

}
?>
Return current item: Online Fantasy Football League