<?php
/**
* function view_news ()
* Lists the articles stored in the database in a "News Page"
*
* @version $Id: AM_view_news.php,v 1.9 2004/11/08 01:05:21 adarkling Exp $
*
* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
* @module Article Manager
* @param none
* @return none
*/

/* Set up display variables */
$now = date('Y-m-d H:i:s');
$GLOBALS['CNT_article']['content'] = '';
/* If article row datum hasn't been retrieved/viewed yet... */
if(!isset($this->pager) || !isset($_REQUEST['PAGER_limit']) 
      || isset($_REQUEST['newsFilter']) || isset($_REQUEST['order'])) 
{
  $this->pager = new PHPWS_Pager;
  $this->pager->setLinkBack('./index.php?module=article&amp;view=news');
  $this->pager->makeArray(TRUE);
  $this->pager->limit = $this->val['listings_per_page'];

  if (isset($_REQUEST['newsFilter']))
    $this->news_filter['letter'] = $_REQUEST['newsFilter'];
  if (!empty($this->news_filter['letter'])) {
    $filter = ' AND title LIKE "'.$this->news_filter['letter'].'%" ORDER BY title ';
    $sort = 'ASC';
  }
  else {
    $this->news_filter['letter'] = '';
    $filter = ' ORDER BY updated_date ';
    $sort = 'DESC';
  }
  /* Allow user to override display order */
  if (isset($_REQUEST['order']) && $o = strtolower($_REQUEST['order'])) {
    if ($o=='asc')
      $sort = 'ASC';
    if ($o=='desc')
      $sort = 'DESC';
  }

  $this->news_filter['order'] = $filter . $sort;
  $result = $GLOBALS['core']->getCol('SELECT id FROM ' . $this->sql_article_table 
      . ' WHERE ' . $this->get_published_sql().' AND mainarticle=0'.$this->sql_predicate . $this->news_filter['order']);
  /* If user is just looking for additional articles that did not 
   * show up on the homepage, filter out those that were shown. */
  $TotalCount = $this->val['summaries_on_homepage'] + $this->val['prev_n_articles'];
  if (isset($_REQUEST['show']) && $_REQUEST['order']='more' && $TotalCount>0 
    && $exclusions = $GLOBALS['core']->getCol('SELECT * FROM '  . $this->sql_article_table 
      . ' WHERE ' . $this->get_published_sql().' AND mainarticle=0'. $this->sql_predicate 
      . ' AND announce ORDER BY updated_date DESC LIMIT ' . $TotalCount)) {
    $result = array_diff($result, $exclusions);
  }
  $this->pager->setData($result);
  $result = NULL;
}

$this->pager->pageData();
if ($data = implode(',', $this->pager->getData())) 
{
	/* Retrieve all article listings for this page */
  $sql = 'SELECT * FROM ' . $this->sql_article_table
          . ' WHERE id IN ('.$data.')'. $this->news_filter['order'];
  if(!$result = $GLOBALS['core']->query($sql)) 
    return;
  while($summary=$result->fetchrow(DB_FETCHMODE_ASSOC)) 
	{
  	$temp = new PHPWS_Article($summary['id'], $summary);
  	$GLOBALS['CNT_article']['content'] .= $temp->view(false,true);
  }
	unset($temp);
	unset($result);
}
else
	$GLOBALS['CNT_article']['content'] = '<br /><br />'
	  . $_SESSION['translate']->it('No matching articles were found in the database!')
	  . '<br /><br />';


/* Create NewsPage HTML */
$alphabet = PHPWS_User::alphabet();
$tags['ALPHABET'] = PHPWS_Text::moduleLink($_SESSION['translate']->it('ALL'), 'article', array('view'=>'news', 'newsFilter'=>'') ) . "&nbsp;\n";
foreach ($alphabet as $alphachar)
  if ($alphachar!=$this->news_filter['letter'])
	  $tags['ALPHABET'] .= PHPWS_Text::moduleLink($alphachar, 'article', array('view'=>'news', 'newsFilter'=>$alphachar) ) . "&nbsp;\n";
	else
	  $tags['ALPHABET'] .= $alphachar . "&nbsp;\n";
$tags['PAGE_BACKWARD_LINK'] = $this->pager->getBackLink();
$tags['PAGE_FORWARD_LINK'] = $this->pager->getForwardLink();
$tags['SECTION_LINKS'] = $this->pager->getSectionLinks();
$tags['SECTION_INFO'] = $this->pager->getSectionInfo() . $_SESSION['translate']->it('Articles');
$tags['LIMIT_LINKS'] = $this->pager->getLimitLinks();
$tags['LIMIT_LINKS_LABEL'] = $_SESSION['translate']->it('Rows to show per page: ');

$title = $_SESSION['translate']->it('All Articles');
if ($this->news_filter['letter']!='') 
  $title .= $_SESSION['translate']->it(' Starting With `[var1]`', $this->news_filter['letter']);
$GLOBALS['Layout_title'] = $title . ' - ' . $_SESSION['OBJ_layout']->page_title;
$GLOBALS['CNT_article']['title'] = $title;

$content = PHPWS_Template::processTemplate($tags,'article','pager.tpl');
$GLOBALS['CNT_article']['content'] = $content . '<br />'
  . $GLOBALS['CNT_article']['content'] . $content; 

?>
