<?php
	/**************************************************************************\
	* phpGroupWare module (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.                                              *
	\**************************************************************************/

  /**************************************************************************\
  * Modified by Steve Price <steep@sdprice.plus.com>                         *
  * Modified to make the search function work properly, and work with the    *
  * nextmatchs class                                                         *
  \**************************************************************************/

	/* $Id: admin.php,v 1.25.2.1.2.3 2007/08/14 12:42:22 skwashd Exp $ */

	if ( (isset($_POST['submit']) && $_POST['submit']) 
		&& (isset($_POST['nntplist']) && $_POST['nntplist']) )
	{
		$phpgw_flags = Array(
				'currentapp'					=>	'nntp',
				'admin_header'					=>	True,
				'enable_nextmatchs_class'		=>	True,
				'noheader'						=>	True,
				'nonavbar'						=>	True
				);
	}
	else
	{
		$phpgw_flags = Array(
				'currentapp'					=>	'nntp',
				'admin_header'					=>	True,
				'enable_nextmatchs_class'		=>	True
				);
	}

	$GLOBALS['phpgw_info']['flags'] =& $phpgw_flags;
	include('../header.inc.php');

	function get_tg($where='')
	{
		$GLOBALS['phpgw']->db->query("SELECT count(con) FROM newsgroups $where");
		$GLOBALS['phpgw']->db->next_record();
		$tg = $GLOBALS['phpgw']->db->f(0);
		if($tg == 0)
		{
			@set_time_limit(0);
			$GLOBALS['nntp']->load_table();
			return get_tg($where);
		}
		else
		{
			return $tg;
		}
	}

	if( (!isset($_REQUEST['submit']) || !$_REQUEST['submit']) && (!isset($_REQUEST['nntplist']) || !$_REQUEST['nntplist']) )
	{
		if ( empty($GLOBALS['phpgw_info']['server']['nntp_server']) )
		{
			echo lang('NNTP is unconfigured. Please <a href="%1">setup NNTP</a> before continuing', 
				$GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'admin.uiconfig.index', 'appname' => 'nntp')));
			$GLOBALS['phpgw']->common->phpgw_footer();
			exit;
		}

		if ( !isset($_REQUEST['tg']) || !$_REQUEST['tg'] )
		{
			$tg = intval(get_tg());
		}
		else
		{
			$tg = intval($_REQUEST['tg']);
		}

		$max = intval($GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs']);

		$urlname = '/nntp/admin.php';

		$p =& $GLOBALS['phpgw']->template;
		$p->set_root( $GLOBALS['phpgw']->common->get_tpl_dir('nntp') );
		
		$p->set_file('nntp_form', 'nntp.tpl');

		$p->set_block('nntp_form','nntp','nntp');
		$p->set_block('nntp_form','nntp_list','nntp_list');

		if ( !isset($_REQUEST['start']) || !$_REQUEST['start'] )
		{
			$start = 0;
		}
		else
		{
			$start = (int) $_REQUEST['start'];
		}

		if ( !isset($_REQUEST['order']) || !$_REQUEST['order'] )
		{
			$order = 1;
		}
		else
		{
			$order = (int) $_REQUEST['order'];
		}

		$sort = 'ASC';

		$orderby = '';
		if ( $order > 0 && $order < 4 ) //see below
		{
			$sort = isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'DESC' ? 'DESC' : 'ASC';

			switch ($order)
			{
				case 1:
					$orderby = ' ORDER BY con '.$sort;
					break;
				case 2:
					$orderby = ' ORDER BY name '.$sort;
					break;
				case 3:
					$orderby = ' ORDER BY active '.$sort;
					break;
			}
		}

		$where_str = '' ;
		if (isset($_REQUEST['search']) && $_REQUEST['search'])
			{
			$where_str = "WHERE name LIKE '%" . $GLOBALS['phpgw']->db->db_addslashes($_REQUEST['query']) ."%'" ;
			$tg = intval(get_tg($where_str));

			// if 'stop' is set then this is a search
			if (isset($_REQUEST['stop']))
			{
				// Always reset counter for a new search
				$start=0;
			}
		}

		$common_hidden_vars = "<input type=\"hidden\" name=\"start\" value=\"{$start}\" />\n"
					. '<input type="hidden" name="stop" value="' . ($start + $max) . "\">\n"
					. "<input type=\"hidden\" name=\"tg\" value=\"{$tg}\">";

		$extra_parms = array();
		$extra_parms['tg'] = $tg;

		if( $sortorder )
		{
			$extra_parms['sortorder'] = $sortorder;
		}
		if( $order )
		{
			$extra_parms['order'] = $order;
		}

		$query_val = get_var('query', array('POST', 'GET'), '');
		if( $query_val )
		{
			$extra_parms['query'] = $query_val;
			$extra_parms['search'] = 'Search' ;
			$common_hidden_vars .= 
					"<input type=\"hidden\" name=\"query\" value=\"{$query_val}\" />\n" .
					"<input type=\"hidden\" name=\"search\" value=\"Search\">";
			
		}

		$var = array
			(
				'search_value'		=>	$query_val,
				'search'		=>	lang('search'),
				'next'			=>	lang('next'),
				'nml'			=>	$GLOBALS['phpgw']->nextmatchs->left($urlname,intval($start),intval($tg),$extra_parms),
				'nmr'			=>	$GLOBALS['phpgw']->nextmatchs->right($urlname,intval($start),intval($tg),$extra_parms),
				'title'			=>	lang('Newsgroups'),
				'action_url'		=>	$GLOBALS['phpgw']->link('/nntp/admin.php'),
				'common_hidden_vars'	=>	$common_hidden_vars,
				'th_bg'			=>	$GLOBALS['phpgw_info']['theme']['th_bg'],
				'th_font'		=>	$GLOBALS['phpgw_info']['theme']['font'],
				'sort_con'		=>	$GLOBALS['phpgw']->nextmatchs->show_sort_order($sort, 1, $order, $urlname,' # ','&tg='.$tg),
				'sort_group'		=>	$GLOBALS['phpgw']->nextmatchs->show_sort_order($sort, 2, $order, $urlname,lang('Group'),'&tg='.$tg),
				'sort_active'		=>	$GLOBALS['phpgw']->nextmatchs->show_sort_order($sort, 3, $order, $urlname,lang('Active'),'&tg='.$tg)
			    );

		$p->set_var($var);

		if ($max <= $tg - $start)
		{
			$totaltodisplay = $max;
		}
		else
		{
			$totaltodisplay = ($tg - $start) - 1;
		}

		$GLOBALS['phpgw']->db->limit_query("SELECT con, name, active FROM newsgroups $where_str $orderby", $start);

		for ( $i = 0; $i < $totaltodisplay; $i++)
		{
			$GLOBALS['phpgw']->db->next_record();
			$tr_color = $GLOBALS['phpgw']->nextmatchs->alternate_row_color($tr_color);
			$con = $GLOBALS['phpgw']->db->f('con');

			$name = $GLOBALS['phpgw']->db->f('name');
			if (!$name)
			{
				$name  = '&nbsp;';
			}
			$group_name = $name;

			$active = $GLOBALS['phpgw']->db->f('active');
			if ($active == 'Y')
			{
				$checked = ' checked="checked"';
			}
			else
			{
				$checked = '';
			}
			$active_var = "<input type=\"checkbox\" name=\"nntplist[]\" value=\"{$con}\" {$checked} />";

			$p->set_var(array
				(
					'tr_color'	=>	$tr_color,
					'con'		=>	$con,
					'group'		=>	$group_name,
					'active'	=>	$active_var
				));

			$p->parse('rows','nntp_list',True);
		}
		$var = array
			(
				'common_hidden_vars'	=>	$common_hidden_vars,
				'lang_update'	=>	lang('update'),
				'checkmark'	=>	$GLOBALS['phpgw']->common->get_image_path('email').'/check.gif'
			);

		$p->set_var($var);

		$p->pparse('out', 'nntp');

		$GLOBALS['phpgw']->common->phpgw_footer();
	}
	else
	{
		$GLOBALS['phpgw']->db->lock('newsgroups');

		$GLOBALS['phpgw']->db->query('UPDATE newsgroups SET active=\'N\' WHERE con>= ' . intval($_REQUEST['start']) . ' AND con<= ' . intval($_REQUEST['stop']), __LINE__, __FILE__ );

		for ( $i = 0; $i < count($_POST['nntplist']); ++$i)
		{
			$GLOBALS['phpgw']->db->query('UPDATE newsgroups SET active=\'Y\' WHERE con=' . intval($_POST['nntplist'][$i]) , __LINE__, __FILE__ );
		}
		$GLOBALS['phpgw']->db->unlock();

		$params = array() ;
		foreach (array('start', 'tg', 'search', 'query') as $field)
		{
			if (isset($_REQUEST[$field]))
			{
				$params[$field] = $_REQUEST[$field] ;
			}
		}
	
		$GLOBALS['phpgw']->redirect_link('/nntp/admin.php', $params) ;
	}
?>
