Location: PHPKode > projects > WB News > base/News.php
<?php

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

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: News.php,v 1.1.2.5.2.1 2008/07/14 11:02:38 pmcilwaine Exp $
 */

require_once( $config["installdir"] . "/baseNews.php" );

class News extends baseNews
{

	function News()
	{
		parent::baseNews();
	}

	function DisplayLatest()
	{
		if ( $this->config["systemstatus"] )
		{
			return;
		}
		
		$news = $this->GetNews( SHOW_NEWS_LATEST );
		
		if ( !is_array($news) && count($news) == 0 )
		{
			return FALSE;
		}

		$news = array_shift( $news );
		
		$avatar = FALSE;
		if ( $news["avatar_name"] )
		{
			$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $this->config['installdir'] );
			$avatar = 'http://' . $_SERVER['HTTP_HOST'] . $path . "/avatar/" . $news["avatar_name"];
		}
		else if ( $news["avatar_url"] )
		{
			$avatar = $news["avatar_url"];
		}

		$this->tmpl->SetFilename( BuildPath( "view-news.ihtml" ) );
		list( $y, $m, $d, $h, $i, $s ) = split("[-: ]", $news["ts"] );
		
		$this->tmpl->AddParam( "title", filter( $news["title"] ) );
		$this->tmpl->AddParam( "news", filter( $news["news"] ) );
		$this->tmpl->AddParam( "date", tz_date( $this->config["dateFormat"], mktime( $h, $i, $s, $m, $d, $y ) ) );
		$this->tmpl->AddParam( "postname", htmlspecialchars( $news["postname"] ) );
		$this->tmpl->AddParam( "avatar", $avatar );
		
		return $this->tmpl->GetHTML();
	}

	/**
	 * Display single news article given by newsid parameter
	 *
	 * @param int $newsid the news id to be shown
	 * @since 2.0.0
	 *	-- this use to be displaySingleNews in 1.x
	 */
	function DisplayNewsArticle( $newsid = 0 )
	{
		if ( $this->config["systemstatus"] )
		{
			return;
		}
	
		if ( !$newsid )
		{
			return FALSE;
		}
		
		$news = $this->GetNews( SHOW_NEWS_ARTICLE, NULL, $newsid );
		if ( !is_array($news) && count($new) == 0 )
		{
			return FALSE;
		}

		$news = array_shift( $news );
		
		$avatar = FALSE;
		if ( $news["avatar_name"] )
		{
			$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $this->config['installdir'] );
			$avatar = 'http://' . $_SERVER['HTTP_HOST'] . $path . "/avatar/" . $news["avatar_name"];
		}
		else if ( $news["avatar_url"] )
		{
			$avatar = $news["avatar_url"];
		}

		$this->tmpl->SetFilename( BuildPath( "view-news.ihtml" ) );
		list( $y, $m, $d, $h, $i, $s ) = split("[-: ]", $news["ts"] );
		
		$this->tmpl->AddParam( "title", filter( $news["title"] ) );
		$this->tmpl->AddParam( "news", filter( $news["news"] ) );
		$this->tmpl->AddParam( "date", tz_date( $this->config["dateFormat"], mktime( $h, $i, $s, $m, $d, $y ) ) );
		$this->tmpl->AddParam( "postname", htmlspecialchars( $news["postname"] ) );
		$this->tmpl->AddParam( "avatar", $avatar );
		
		return $this->tmpl->GetHTML();
	}

	/**
	 * Display standard news list
	 *
	 * @param string $news_display
	 * @param int $limit_chars @deprecated deprecated since version 2.0
	 *
	 * @return void
	 */
	function DisplayNews( $news_display = SHOW_NEWS_ALL, $limit_chars = NULL )
	{
		global $auth, $userinfo;
		
		if ( $this->config["systemstatus"] )
		{
			return;
		}
	
		$page = isset($_GET["offset"]) ? $this->DB->escape( $_GET["offset"] ) : 0 ;
		$newslist = $this->GetNews( SHOW_NEWS_ALL, $page );
		
		if ( !$newslist )
		{
			return ;
		}

		$this->tmpl->SetFilename( BuildPath( "list-news.ihtml" ) );
		$rows =& $this->tmpl->AddParam( "rows", array() );

		// if we are here means we have some news
		foreach ( $newslist as $news )
		{
			$avatar = FALSE;
			if ( $news["avatar_name"] )
			{
				$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $this->config['installdir'] );
				$avatar = 'http://' . $_SERVER['HTTP_HOST'] . $path . "/avatar/" . $news["avatar_name"];
			}
			else if ( $news["avatar_url"] )
			{
				$avatar = $news["avatar_url"];
			}

			$allow_comments = intval($news["allowcomments"]);
			if ( $auth->is_logged() && !$allow_comments && $auth->has_perm( "cancomment" ) )
			{
				$allow_comments = intval($news["comments_logged_user"]);
			}
			
			list( $y, $m, $d, $h, $i, $s ) = split("[-: ]", $news["ts"] );
		
			$rows[] = array(
				"newsid" => $news["id"],
				"postname" => htmlspecialchars( $news["postname"] ),
				"title" => filter( $news["title"] ),
				"date" => tz_date( $this->config["dateFormat"], mktime( $h, $i, $s, $m, $d, $y ) ),
				"news" => filter( $news["news"] ),
				"allowcomments" => $allow_comments,
				"comments" => $this->CountComments( $news["id"] ),
				"avatar" => $avatar
				);
		}

		return $this->tmpl->GetHTML();
	}

	/**
	 * A character limit is no longer used, instead we use the summary field to display the news.
	 * 
	 * @param int $charlimit the number of characters to be displayed @deprecated deprecated since version 2.0
	 */
	function DisplayShortNews( $charlimit = 200 )
	{
		global $auth;
		
		if ( $this->config["systemstatus"] )
		{
			return;
		}
		
		$page = isset($_GET["offset"]) ? $this->DB->escape( $_GET["offset"] ) : 0 ;
		$newslist = $this->GetNews( SHOW_NEWS_SHORT, $page );
		
		if ( !$newslist )
		{
			return ;
		}

		$this->tmpl->SetFilename( BuildPath( "list-news-summary.ihtml" ) );
		$rows =& $this->tmpl->AddParam( "rows", array() );

		// if we are here means we have some news
		foreach ( $newslist as $news )
		{
			$avatar = FALSE;
			if ( $news["avatar_name"] )
			{
				$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $this->config['installdir'] );
				$avatar = 'http://' . $_SERVER['HTTP_HOST'] . $path . "/avatar/" . $news["avatar_name"];
			}
			else if ( $news["avatar_url"] )
			{
				$avatar = $news["avatar_url"];
			}

			$allow_comments = intval($news["allowcomments"]);
			if ( $auth->is_logged() && !$allow_comments && $auth->has_perm( "cancomment" ) )
			{
				$allow_comments = intval($news["comments_logged_user"]);
			}
			
			list( $y, $m, $d, $h, $i, $s ) = split("[-: ]", $news["ts"] );
			
			$rows[] = array(
				"newsid" => $news["id"],
				"postname" => htmlspecialchars( $news["postname"] ),
				"title" => filter( $news["title"] ),
				"date" => tz_date( $this->config["dateFormat"], mktime( $h, $i, $s, $m, $d, $y ) ),
				"summary" => filter( $news["summary"] ),
				"allowcomments" => $news["allowcomments"],
				"comments" => $this->CountComments( $news["id"] ),
				"avatar" => $avatar
				);
		}

		return $this->tmpl->GetHTML();
	}

	/**
	 * Show news grouped by date
	 *
	 * @param String $dateformat the PHP format to have the date displayed otherwise use config
	 * @return void
	 */
	function DisplayGroupedNews( $dateformat=NULL )
	{
		global $auth;
		
		if ( $this->config["systemstatus"] )
		{
			return;
		}

		$page = isset($_GET["offset"]) ? $this->DB->escape( $_GET["offset"] ) : 0 ;
		$newslist = $this->GetNews( SHOW_NEWS_GROUPED, $page );
		
		if ( !$newslist )
		{
			return ;
		}

		$this->tmpl->SetFilename( BuildPath( "list-news-grouped.ihtml" ) );
		$group_date =& $this->tmpl->AddParam( "group_date", array() );
		$rows =& $this->tmpl->AddParam( "rows", array() );

		foreach ( $newslist as $news )
		{
			list( $day, $month, $year ) = explode( "-", $news["date"] );
			$group_date[ $news["date"] ] = date( "D dS F Y", mktime( 0, 0, 0, $month, $day, $year ) );

			$avatar = FALSE;
			if ( $news["avatar_name"] )
			{
				$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $this->config['installdir'] );
				$avatar = 'http://' . $_SERVER['HTTP_HOST'] . $path . "/avatar/" . $news["avatar_name"];
			}
			else if ( $news["avatar_url"] )
			{
				$avatar = $news["avatar_url"];
			}

			$allow_comments = intval($news["allowcomments"]);
			if ( $auth->is_logged() && !$allow_comments && $auth->has_perm( "cancomment" ) )
			{
				$allow_comments = intval($news["comments_logged_user"]);
			}
			
			list( $y, $m, $d, $h, $i, $s ) = split("[-: ]", $news["ts"] );

			$rows[ $news["date"] ][] = array(
				"newsid" => $news["id"],
				"postname" => htmlspecialchars( $news["postname"] ),
				"title" => filter( $news["title"] ),
				"date" => tz_date( $this->config["dateFormat"], mktime( $h, $i, $s, $m, $d, $y ) ),
				"news" => filter( $news["news"] ),
				"allowcomments" => $allow_comments,
				"comments" => $this->CountComments( $news["id"] ),
				"avatar" => $avatar
				);
		}
		
		return $this->tmpl->GetHTML();
	}

	/**
	 * Works the same as DisplayNewsArticle excepts uses a template for printing
	 *
	 * @param int $newsid the news id to be shown
	 * @return void
	 */
	function DisplayPrintNews( $newsid = 0 )
	{
		if ( $this->config["systemstatus"] )
		{
			return;
		}
	

		if ( !$newsid )
		{
			return FALSE;
		}
		
		$news = $this->GetNews( SHOW_NEWS_ARTICLE, NULL, $newsid );
		if ( !is_array($news) && count($new) == 0 )
		{
			return FALSE;
		}

		$news = array_shift( $news );
		
		$avatar = FALSE;
		if ( $news["avatar_name"] )
		{
			$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $this->config['installdir'] );
			$avatar = 'http://' . $_SERVER['HTTP_HOST'] . $path . "/avatar/" . $news["avatar_name"];
		}
		else if ( $news["avatar_url"] )
		{
			$avatar = $news["avatar_url"];
		}

		$this->tmpl->SetFilename( BuildPath( "view-news.ihtml" ) );
		list( $y, $m, $d, $h, $i, $s ) = split("[-: ]", $news["ts"] );
		
		$this->tmpl->AddParam( "title", filter( $news["title"] ) );
		$this->tmpl->AddParam( "news", filter( $news["news"] ) );
		$this->tmpl->AddParam( "date", tz_date( $this->config["dateFormat"], mktime( $h, $i, $s, $m, $d, $y ) ) );
		$this->tmpl->AddParam( "postname", htmlspecialchars( $news["postname"] ) );
		$this->tmpl->AddParam( "avatar", $avatar );
		
		return $this->tmpl->GetHTML();
	}
	
    /**
     *	Creates a home page (splash page) news with title and link to article
     *	@note pagination isn't possible with this setup
     *
     *	@param @int 	limit 		numeric limit or leave NULL to use news config limit
	 *	@param @array	$path		use the category "id" => "url" to create proper URLs otherwise use news default if not set in a category
     *	@return void
     */
    function SplashNews( $limit = NULL, $paths = array() )
	{
		if ( $this->config["systemstatus"] )
		{
			return;
		}

		$page = isset($_GET["offset"]) ? $this->DB->escape( $_GET["offset"] ) : 0 ;
		$newslist = $this->GetNews( SHOW_NEWS_SPLASH, $page, NULL, $limit );

		if ( !is_array( $newslist ) && count($newslist) == 0 )
		{
			return FALSE;
		}

		$this->tmpl->SetFilename( BuildPath( "splashnews.ihtml" ) );
		$rows =& $this->tmpl->AddParam( "rows", array() );
		
		foreach ( $newslist as $news )
		{
			$rows[$news["id"]] = array(
				"id" => htmlspecialchars( $news["id"] ),
				"title" => htmlspecialchars( $news["title"] ),
				"summary" => htmlspecialchars( $news["summary"] )
				);
			if ( array_key_exists( $news["catid"], $paths ) )
			{
				$rows[$news["id"]]["view-link"] = make_url_html( array("newsid"=>$news["id"]), $paths[ $news["catid"] ] );
			}
			else if ( isset( $paths[""] ) && "" != $paths[""] )
			{
				$rows[$news["id"]]["view-link"] = make_url_html( array("newsid"=>$news["id"]), $paths[""] );
			}
		}

		return $this->tmpl->GetHTML();
	}

	/**
	 * Builds paging links
	 *
	 * @param string $type
	 * @param string $current_url - This is used if using Rewrite URLs
	 * @param bool $no_page_enabled - If TRUE will show paging even if theres only 1 page
	 */
	function pagination( $type = NULL, $current_url=NULL, $no_page_enabled=TRUE )
	{
		if ( $this->config["systemstatus"] )
		{
			return;
		}

		$type = NULL == $type ? PAGINATION_NEXT_PREV : $type;
		
		switch ( $type )
		{
		case PAGINATION_NEXT_PREV :

			$total = intval( $this->total_news );
			$cur_page = intval( $this->page );
			$per_page = intval( $this->config["newslimit"] );

			$previous = $cur_page > 0 && ($cur_page-$per_page) >= 0 ? $cur_page - $per_page : FALSE;
			$next = ( $cur_page + $per_page <= $total ) ? $cur_page + $per_page : FALSE;

			if ( !$no_page_enabled && !$next && !$cur_page  )
			{
				return;
			}

			$this->tmpl->SetFilename( BuildPath( "pagination-next-prev.ihtml" ) );

			$this->tmpl->AddParam( "current_url", $current_url ? $current_url : $_SERVER["SCRIPT_URI"] );
			$this->tmpl->AddParam( "prev_link", $previous !== FALSE ? make_url_html( array("offset"=>$previous) ) : FALSE );
			$this->tmpl->AddParam( "next_link", $next !== FALSE  ? make_url_html( array("offset"=>$next) ) : FALSE );
			
			break;
		case PAGINATION_NUMBERED :

			$total = intval( $this->total_news );
			$cur_page = intval( $this->page );
			$per_page = intval( $this->config["newslimit"] );

			$previous = $cur_page > 0 && ($cur_page-$per_page) >= 0 ? $cur_page - $per_page : FALSE;
			$next = ( $cur_page + $per_page <= $total ) ? $cur_page + $per_page : FALSE;

			if ( !$no_page_enabled && !$next && !$cur_page )
			{
				return;
			}

			$this->tmpl->SetFilename( BuildPath( "pagination-paging.ihtml" ) );
			$this->tmpl->AddParam( "current_url", $current_url ? $current_url : $_SERVER["SCRIPT_URI"] );
			$this->tmpl->AddParam( "prev_link_offset", $previous !== FALSE ? $previous : FALSE );
			$this->tmpl->AddParam( "prev_link", $previous !== FALSE ? make_url_html( array("offset"=>$previous), NULL, TRUE ) : FALSE );
			$this->tmpl->AddParam( "next_link_offset", $next !== FALSE ? $next : FALSE );
			$this->tmpl->AddParam( "next_link", $next !== FALSE  ? make_url_html( array("offset"=>$next), NULL, TRUE ) : FALSE );
			$pages =& $this->tmpl->AddParam( "pages", array() );
			
			/** set up paging numbers **/
			$current_page = 1;
			$start_page = 1; // start at page 1
			$total_page = ceil( $total / $per_page );
			if ( $cur_page != 0 )
			{
				$current_page = ($cur_page / $per_page) + 1;
				$start_page = ($current_page - 3) >= 1 ? $current_page - 3 : 1;
				$max_pages = ($start_page + 5) <= $total_page ? $start_page + 5 : $total_page;
			}
			else
			{
				$max_pages = $total_page >= 5 ? 5 : $total_page;
			}
			
			for ( $i = $start_page; $i <= $max_pages; $i++ )
			{
				$pages[] = array(
					"label" => htmlspecialchars( $i ),
					"view-link-offset" => (($i-1) * $per_page),
					"view-link" => make_url_html( array("offset"=>(($i-1) * $per_page) ), NULL, TRUE ),
					"cur_page" => $current_page == $i
					);
			}
			
			break;
		}
		
		return $this->tmpl->GetHTML();
	}

	/**
	 * @param int $id news article id
	 * @deprecated deprecated since version 2.0
	 */
	function displaySingleNews( $id )
	{
		return $this->DisplayNewsArticle( $id );
	}

	/**
	 *
	 * @deprecated deprecated since version 2.0
	 */
	function displayNewsDateGroup( $_var )
	{
		return $this->DisplayGroupedNews();
	}

	/**
	 * @deprecated deprecated since version 2.0
	 */
	function displayLastestNews()
	{
		return $this->DisplayLatest();
	}

}

return;

?>
Return current item: WB News