<?php
  /**************************************************************************\
  * phpGroupWare application (NNTP)                                          *
  * http://www.phpgroupware.org                                              *
  * Written by Mark Peters <mpeters@satx.rr.com>                             *
  * --------------------------------------------                             *
  *  This program is free software; you can redistribute it and/or modify it *
  *  under the terms of the GNU General Public License as published by the   *
  *  Free Software Foundation; either version 2 of the License, or (at your  *
  *  option) any later version.                                              *
  \**************************************************************************/

	/* $Id: index.php,v 1.36 2001/05/17 02:38:56 skeeter Exp $ */

	$phpgw_flags = Array(
		'currentapp'	=>	'nntp',
		'noheader'	=>	True,
		'nonavbar'	=> True
	);

	$phpgw_info['flags'] = $phpgw_flags;

	include('../header.inc.php');

	$news = Array();
	while($pref = @each($phpgw_info['user']['preferences']['nntp']))
	{
		$news[]=$pref[0];
	}

	if(count($news) == 0)
	{
		header('Location: '.$phpgw->link('/nntp/preferences.php'));
		$phpgw->common->phpgw_exit();
	}

	function close_routine()
	{
		global $nntp;
		$nntp->close_port();
	}

	@set_time_limit(0);
	$nntp->display = $phpgw_info['user']['preferences']['common']['maxmatchs'];
	if ($nntp->errorset)
	{
		echo $nntp->error['msg'].':'.$nntp->error['desc']."<br>\n";
		$phpgw->common->phpgw_exit();
	}
	else
	{
		$phpgw->common->phpgw_header();
		echo parse_navbar();
	}

	register_shutdown_function('close_routine');

	$p = CreateObject('phpgwapi.Template',$phpgw->common->get_tpl_dir('nntp'));
	$templates = array(
		'index_form' => 'index.tpl'
	);
	$p->set_file($templates);
	$p->set_block('index_form','index','index');
	$p->set_block('index_form','layout_table','layout_table');
	$p->set_block('index_form','basic_row','basic_row');

   reset($news);
   $j = 0;
	$i = count($news);

	while ($group = each($news))
	{
		$j++;
		$s = '';
		if($nntp->read_table(intval($group[1])))
		{
			$channel_url = '<a href="'.$phpgw->link('/nntp/viewgroup.php','folder='.$group[1]).'" target="new" style="text-decoration: none; color: #000000; text-align: center">';
			$channel_title = $nntp->mailbox.'</a>';
		}
		else
		{
			$channel_url = '';
			$channel_title = $nntp->error['msg'].':'.$nntp->error['desc'];
		}
		
		$var = Array(
			'channel_url'	=> $channel_url,
			'channel_title'	=> $channel_title
		);
		
		$p->set_var($var);
		
		if ($nntp->errorset)
		{
//			echo 'Found an error reading group: '.$group[1]."<br>\n";
			$p->set_var('item_link','');
			$p->set_var('item_label',$nntp->error['msg'].':'.$nntp->error['desc']);
			$s .= $p->fp('o_','basic_row');
		}
		elseif ($nntp->active == 'N')
		{
			$p->set_var('item_link','');
			$p->set_var('item_label',lang('x not active',$nntp->mailbox));
			$s .= $p->fp('o_','basic_row');
		}
		else
		{
			$links = $nntp->get_subjects();
			if (count($links)==0 || $links == 0)
			{
				$p->set_var('channel_url','');
				$p->set_var('channel_title',$nntp->mailbox);
				$p->set_var('item_link','');
				$p->set_var('item_label','No articles found!');
				$s .= $p->fp('o_','basic_row');
			}
			else
			{
				krsort($links);
				$clinks = count($links);
				for ($k=0,reset($links); $k<$clinks; $k++,next($links))
				{
					$key = key($links);
					$link_url = $phpgw->link('/nntp/read_article.php','folder='.$group[1].'&msgnum='.$key);
					$p->set_var('item_link','<a href="'.$link_url.'">');
					$p->set_var('item_label',$links[$key].'</a>');
					$s .= $p->fp('o_','basic_row');
				}
			}
		}
		$p->set_var('rows',$s);
		$p->set_var('section_' . $j,$p->fp('o','layout_table'));

		if ($j == 3 || $i == 1)
		{
			$p->pfp('out','index');
			$p->set_var('section_1', '');
			$p->set_var('section_2', '');
			$p->set_var('section_3', '');
			$j = 0;
		}
		$i--;
	}
	$phpgw->common->phpgw_footer();
?>
