Location: PHPKode > projects > WB News > baseNews.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 30th Dec 2006                           #||
||#     Filename: baseNews.php                           #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: baseNews.php,v 1.1.2.3.2.1 2008/07/14 10:54:25 pmcilwaine Exp $
 */

class baseNews
{

	var $catid = array();
	var $_themes = array();
	
	// Below are our global variables
	
	/**
	 *	Other objects
	 */
	var $DB = NULL;
	var $tmpl = NULL;

	/**
	 *	Other Arrays
	 */
	var $config = array();

	/**
	 * Constructor for baseNews, it initiates the DB, Tmpl and config property
	 * variables
	 * 
	 * @return void
	 */
	function baseNews()
	{
		global $DB, $tmpl, $config; // globals suck but to make your work easier we have to do it
		
		$this->DB =& $DB;
		$this->tmpl =& $tmpl;

		$this->config =& $config;

		return;
	}

	/**
	 *  Get the category id based on either an ID given or the category name given
	 *  Example:
	 *  	setCategory( 1 ); if the category id was 1 for 'wb news'
	 * 		setCategory( 'wb news'); if the category name was wb news, the name is not case sensitive.
	 *  
	 *	@param mixed $catid the id or name of category
	 *	@return bool
	 */
	function SetCategory( $catid="" )
	{
		$catid = $this->DB->escape($catid);
		
		$cond = array();
		$cond[] = "(id = '$catid' OR lower(\"name\") = '" . strtolower($catid) . "')";
		$cond = join( " AND ", $cond );
		
		$ids = $this->DB->ListBy( TBL_CATEGORY, $cond );
		
		if ( count($ids) == 1 )
		{
			$this->catid[] = $ids[0]["id"];
			return TRUE;
		}

		return FALSE;
	}

	/**
	 *  Returns the category id
	 * 
	 *	@return int
	 */
	function GetCategory()
	{
		return $this->catid;
	}

	/**
	 *  Gets the news based on several different types and returns the SQL results array
	 *  back to the caller 
	 * 
	 *	@param string $type
	 *	@param int $page - the current offset
	 *	@param int $newsid - the news id if we have one (for single news display only)
	 *  @param int $limit - the number of news articles to limit per page
	 *	@return array - SQL Produces array
	 */
	function GetNews( $type = SHOW_NEWS_ALL, $page = 0, $newsid = NULL, $limit = NULL )
	{
		$cond = array();
		$fields = array();
		$joins = array();

		$alias = "n";

		$this->page = $page;
		
		// from tables
		$fromtables = array();
		$fromtables[] = TBL_NEWS . " as $alias";

		if ( NULL == $limit )
		{
			$limit = $this->config["newslimit"];
		}

		$sort = "ts DESC";
		
		switch ( $type )
		{
		case SHOW_NEWS_ALL :

			$users = TBL_USERS;
			$category = TBL_CATEGORY;
		
			$fields = array(
				"\"postname\"",
				"$alias.\"id\"",
				"$alias.\"catid\"",
				"$alias.\"title\"",
				"$alias.\"news\"",
				"$alias.\"ts\"",
				"$alias.\"allowcomments\"",
				"$alias.\"comments_logged_user\"",
				"\"avatar_name\"",
				"\"avatar_url\""
				);

			$joins["leftjoin"][] = "$users ON $users.userid = $alias.userid";
			$joins["leftjoin"][] = "$category ON $category.id = $alias.catid";

			$cond[] = "(\"release_date\"<='" . date( "Y-m-d" ) . "' OR \"release_date\" IS NULL)";
			$cond[] = "(\"archive_date\">'" . date( "Y-m-d" ) . "' OR \"archive_date\" IS NULL)";

			break;
		case SHOW_NEWS_GROUPED :
			
			$users = TBL_USERS;
			$category = TBL_CATEGORY;
		
			$fields = array(
				"\"postname\"",
				"$alias.\"id\"",
				"EXTRACT(day from \"ts\") || '-' || EXTRACT(month from \"ts\") || '-' || EXTRACT(year from \"ts\") as date",
				"$alias.\"catid\"",
				"$alias.\"title\"",
				"$alias.\"news\"",
				"$alias.\"ts\"",
				"$alias.\"allowcomments\"",
				"$alias.\"comments_logged_user\"",
				"\"avatar_name\"",
				"\"avatar_url\""
				);

			$joins["leftjoin"][] = "$users ON $users.userid = $alias.userid";
			$joins["leftjoin"][] = "$category ON $category.id = $alias.catid";

			$cond[] = "(\"release_date\"<='" . date( "Y-m-d" ) . "' OR \"release_date\" IS NULL)";
			$cond[] = "(\"archive_date\">'" . date( "Y-m-d" ) . "' OR \"archive_date\" IS NULL)";

			break;
		case SHOW_NEWS_ARTICLE :
			
			$users = TBL_USERS;
			$category = TBL_CATEGORY;
		
			$fields = array(
				"\"postname\"",
				"$alias.\"id\"",
				"$alias.\"catid\"",
				"$alias.\"title\"",
				"$alias.\"news\"",
				"$alias.\"ts\"",
				"$alias.\"allowcomments\"",
				"$alias.\"comments_logged_user\"",
				"\"avatar_name\"",
				"\"avatar_url\""
				);

			$joins["leftjoin"][] = "$users ON $users.userid = $alias.userid";
			$joins["leftjoin"][] = "$category ON $category.id = $alias.catid";

			$cond[] = "$alias.\"id\"='$newsid'";

			$limit = FALSE;
			
			break;
		case SHOW_NEWS_LATEST :
			
			$users = TBL_USERS;
			$category = TBL_CATEGORY;
		
			$fields = array(
				"\"postname\"",
				"$alias.\"id\"",
				"$alias.\"catid\"",
				"$alias.\"title\"",
				"$alias.\"news\"",
				"$alias.\"ts\"",
				"$alias.\"allowcomments\"",
				"$alias.\"comments_logged_user\"",
				"\"avatar_name\"",
				"\"avatar_url\""
				);

			$joins["leftjoin"][] = "$users ON $users.userid = $alias.userid";
			$joins["leftjoin"][] = "$category ON $category.id = $alias.catid";

			$cond[] = "(\"release_date\"<='" . date( "Y-m-d" ) . "' OR \"release_date\" IS NULL)";
			$cond[] = "(\"archive_date\">'" . date( "Y-m-d" ) . "' OR \"archive_date\" IS NULL)";
			
			$limit = 1;
			
			break;
		case SHOW_NEWS_ARCHIVE :
		
			$sort = "\"archive_date\" DESC";
			$fields = array(
				"DISTINCT EXTRACT( YEAR FROM \"archive_date\" ) || '-' || EXTRACT( MONTH FROM \"archive_date\" ) as yearmonth"
				);

			$cond[] = "\"archive_date\"<'" . date( "Y-m-d" ) . "'";
			
			break;
		case SHOW_NEWS_ARCHIVE_LIST:

			
			$users = TBL_USERS;
			$category = TBL_CATEGORY;
		
			$fields = array(
				"\"postname\"",
				"$alias.\"id\"",
				"$alias.\"catid\"",
				"$alias.\"title\"",
				"$alias.\"news\"",
				"$alias.\"ts\"",
				"$alias.\"allowcomments\"",
				"$alias.\"comments_logged_user\"",
				"\"avatar_name\"",
				"\"avatar_url\""
				);

			$joins["leftjoin"][] = "$users ON $users.userid = $alias.userid";
			$joins["leftjoin"][] = "$category ON $category.id = $alias.catid";

			$cond[] = "\"archive_date\"<'" . date( "Y-m-d" ) . "'";
			$cond[] = "EXTRACT( YEAR FROM \"archive_date\" ) = '" . $this->year . "'";
			$cond[] = "EXTRACT( MONTH FROM \"archive_date\" ) = '" . $this->month . "'";

			break;
		case SHOW_NEWS_SPLASH :

			$fields = array(
				"id",
				"catid",
				"title",
				"summary"
				);
				
			$cond[] = "(\"release_date\"<='" . date( "Y-m-d" ) . "' OR \"release_date\" IS NULL)";
			$cond[] = "(\"archive_date\">'" . date( "Y-m-d" ) . "' OR \"archive_date\" IS NULL)";
		
			break;
		case SHOW_NEWS_SHORT :
			
			$users = TBL_USERS;
			$category = TBL_CATEGORY;
		
			$fields = array(
				"\"postname\"",
				"$alias.\"id\"",
				"$alias.\"catid\"",
				"$alias.\"title\"",
				"$alias.\"summary\"",
				"$alias.\"ts\"",
				"$alias.\"allowcomments\"",
				"$alias.\"comments_logged_user\"",
				"\"avatar_name\"",
				"\"avatar_url\""
				);

			$joins["leftjoin"][] = "$users ON $users.userid = $alias.userid";
			$joins["leftjoin"][] = "$category ON $category.id = $alias.catid";

			$cond[] = "(\"release_date\"<='" . date( "Y-m-d" ) . "' OR \"release_date\" IS NULL)";
			$cond[] = "(\"archive_date\">'" . date( "Y-m-d" ) . "' OR \"archive_date\" IS NULL)";

			break;
		default :
			return FALSE;
		}

		if ( is_array( $this->catid ) && count( $this->catid ) != 0 )
		{
			$cond[] = "catid IN ('" . join( "', '", $this->catid ) . "')";
		}

		/** basic news stuff **/
		$cond[] = "\"publish\"=1";
		
		$cond = join( " AND ", $cond );

		$this->total_news = $this->DB->CountBy( $fromtables, $cond, $joins, $alias );

		return $this->DB->ListByJoin(
			$fromtables,
			$fields,
			$cond,
			$joins,
			$sort,
			$limit,
			$page,
			NULL,
			$fields
			);
	}

	/**
	 *  Get the total number of comments for a news article
	 *  
	 *	@access private
	 *	@param int $newsid
	 *	@return int
	 */
	function CountComments( $newsid )
	{
		$newsid = $this->DB->escape( $newsid );
		
		$cond = array();
		$cond[] = "newsid='$newsid'";
		$cond[] = "is_spam = -1";
		$cond = join( " AND ", $cond );

		return $this->DB->CountBy( TBL_COMMENTS, $cond );
	}

}

return;

?>
Return current item: WB News