<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/ArticlesPanel.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSuperGridPager.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Openads.inc.php" );

	class FilteredArticlesPager extends FrontendSuperGridPager {
		protected function prepareUriArray( $grid , $input , $pagenum ) {
			$retval = parent::prepareUriArray( $grid , $input , $pagenum );
			unset( $retval["article_category"] );
			return $retval;
		}
	}

	class FilteredArticlesPanel extends Widget {
		public $sTitle = "";
		public $sRSSUrl = "";
		public $sFilterArticleCategory = "";
		protected $iPagesize = 10;
		protected $iPagenum = 0;

		public $_pagenum = "";
		public $_pagesize = "";
		
		public function setFilterArticleCategory( $v ) { $this->sFilterArticleCategory = $v; }

		public function __construct( $id = "ArticlePanel" , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->article_model = new ArticlesModel();
			$this->ac_model = new ArticlecategoriesModel();
			

			$this->oBottomBanner = new Openads( "adserver_165" , $this , 165 );

			$this->_pagenum = "pn";
			$this->_pagesize = "ps";
		}
		
		public function modifyGetPost( &$g , &$p ) {
			$retval = parent::modifyGetPost( $g , $p );
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );
			$this->oArticlesPanel = new ArticlesPanel( "ArticlesPanel" , $this );
			$this->oArticlesPager = new FilteredArticlesPager( "ArticlesPager" , $this );
			$this->oArticlesPager->setGridParent( $this );
			return $retval;
		}

		protected function whereClause() {
			$filters = array();
			$filters["is_active"]="is_active";
			$filters["is_deleted"]="not is_deleted";
			$filters["article_category_id"]=sprintf( "article_category_id in (select article_category_id from article_categories where article_category_name='%s')",FNTH_db_addslashes( $this->sFilterArticleCategory ) );
			return sprintf( " where %s" , implode( " and " , $filters ) );
		}

		protected function orderClause() {
			return " order by article_ordering_tstamp desc";
		}

		protected function limitClause() {
			return sprintf( " limit %d offset %d" , 
				$this->iPagesize,
				$this->iPagenum*$this->iPagesize );
		}

		protected function getNumberOfArticleIds() {
			$query = sprintf( "select count(article_id) as num from articles %s",
				$this->whereClause() );
			return FNTH_db_get_field( $query , "num" );
		}

		protected function getArticleIds() {
			$query = sprintf( "select article_id from articles %s %s %s",
				$this->whereClause(),
				$this->orderClause(),
				$this->limitClause() );
			$article_ids = FNTH_db_get_column( $query , "article_id" );
			return $article_ids;
		}
		
		public function loadPageNumSizeFromArray( $g = null ) {
			if (!isset( $g )) $g = $this->aG;
			$this->iPagenum = (isset( $g[$this->_pagenum] )) ? $g[$this->_pagenum] : $this->iPagenum;
			$this->iPagesize = (isset( $g[$this->_pagesize] )) ? $g[$this->_pagesize] : $this->iPagesize;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$number_of_articles = $this->getNumberOfArticleIds();
			$this->oArticlesPager->setNumberOfRecords( $number_of_articles );
			$this->loadPageNumSizeFromArray();

			$this->oCommunityToolbar->setTitle( $this->sTitle );
			$this->oCommunityToolbar->setIsSendPrint( false );
			$this->oCommunityToolbar->setCommunityLinks( false );
			if (!empty( $this->sRSSUrl ))
				$this->oCommunityToolbar->setSendPrint( sprintf( "<a href=\"%s\"><img class=\"nborder\" src=\"/images/community/rss.png\" /></a>",
					$this->sRSSUrl ) );
			
			$this->oArticlesPanel->setArticleIds( $this->getArticleIds() );
			return parent::render();
		}

	}



?>
