Location: PHPKode > projects > Xcomic > Xcomic_0.8.3/includes/ComicAssociatedNewsDisplay.class.php
<?php
/**
Xcomic

$Id: ComicAssociatedNewsDisplay.class.php,v 1.5.2.1.2.1 2005/07/22 01:49:07 mikexstudios Exp $
*/

/*
define('IN_XCOMIC', true);

$xcomicRootPath='../';
include_once($xcomicRootPath.'initialize.php');
*/

//$xcomicRootPath is defined in the file that includes this file
//Unfortunately, for sites with register_globals turned on, this poses
//a security theat--especially since $xcomicRootPath is being used in include
//statements. Therefore, check for hacking attempts
if ( !defined('IN_XCOMIC') )
{
	die("Hacking attempt");
}

include_once('NewsDisplay.class.php');
include_once('ComicDisplay.class.php');


class ComicAssociatedNewsDisplay extends NewsDisplay {
	
	var $comicDisplay;
	var $associatedNewsIds; //Array of them
	var $curAssociatedNewsId=0; //Focused cid
	
	function ComicAssociatedNewsDisplay($inCid='') {
		if(!empty($inCid))
		{
			//Create ComicDisplay object
			$this->comicDisplay = new ComicDisplay($inCid);
			$this->associateNews();
		}
	}
	
	function setCid($inCid) {
		//This is commented out since getComicInfo also sets the id.
		//This is a small logic error that is corrected in the next release, but
		//shouldn't affect you at all.
		//$this->comicDisplay->setCurrentComicId($inCid);
		
		//Now update the comicDisplay for the newly set Cid
		$this->comicDisplay->getComicInfo($inCid); 
	}
	
	function associateNews() {
		$this->getAssociatedNewsIds();
		
		//If next news id does not exist, display the last
		//most recent news
		$nextNid = $this->getNextAssociatedNewsId();
		if($nextNid)
		{
			$this->getNewsInfo($nextNid);
		}
		else
		{
			//Set to previous news id
			$this->setCid($this->comicDisplay->prevId());
			$this->getAssociatedNewsIds();
			$this->getNewsInfo($this->getNextAssociatedNewsId());
		}	
	}
	
	function getAssociatedNewsIds($order='ASC') {
		global $xcomicDb, $message;
		
		//comicDate holds *nix timestamp
		$comicDate = $this->comicDisplay->getDate();
		
		//Get the news id associated with a comic
		$sql = 'SELECT id
			FROM '.XCOMIC_NEWS_TABLE."
			WHERE date >= '$comicDate'";
		
		//Get the date of the next comic (if exists)
		$nextCid = $this->comicDisplay->nextId();
		if($nextCid) //A next comic exists
		{
			$this->comicDisplay->setCurrentComicId($nextCid);
			$this->comicDisplay->getComicInfo($nextCid);
			$nextComicDate = $this->comicDisplay->getDate();
			
			//Add to SQL query
			$sql .= " AND date < '$nextComicDate'";
		}
		
		//Add sorting
		$sql .= " ORDER BY id $order"; //Should make this variable
		
		//Make the changes happen
		if(!($result = $xcomicDb->sql_query($sql)))
		{
			echo 'Unable to get the news ids associated with the selected comic.';
		}
		
		//Get the result. (Can be more than one)

		while ( $row = $xcomicDb->sql_fetchrow($result) )
		{
			//Place associated ids in array
			$this->associatedNewsIds[] = $row['id'];
			//print_r($this->associatedNewsIds);			
		}

	}
	
	function getNextAssociatedNewsId() {
		
		$nid = $this->associatedNewsIds[$this->curAssociatedNewsId];
		if(isset($nid))
		{
			$this->curAssociatedNewsId++;
			return $nid;
		}
		else
		{
			return false;
		}	
	}
	
	function updateForNextId() {
		$nextId = $this->getNextAssociatedNewsId();
		if($nextId)
		{
			$this->getNewsInfo($nextId);
			return true;
		}
		else
		{
			return false;
		}
	}
	
}

/*
//Testing LatestNewsDisplay
$x = new ComicAssociatedNewsDisplay('1');
echo $x->getTitle();
echo $x->getContent();
*/


?>
Return current item: Xcomic