<?php
	/**************************************************************************\
	* phpGroupWare - Headlines Administration                                  *
	* http://www.phpgroupware.org                                              *
	* Written by Joseph Engo <jengo@phpgroupware.org>                          *
	* --------------------------------------------                             *
	*  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: newheadline.php,v 1.1.1.3.6.1 2004/09/27 03:51:46 kazuyan Exp $ */

	$phpgw_info['flags'] = array(
		'admin_only' => True,
		'currentapp' => 'headlines',
		'nonavbar'   => True,
		'noheader'   => True
	);
	include('../header.inc.php');

	if ($_POST['submit'])
	{
		if (! $_POST['n_display'])
		{
			$errors[] = lang('You must enter a display');
		}
		else
		{
			$n_display = $GLOBALS['phpgw']->db->db_addslashes($_POST['n_display']);
		}

		if (! $_POST['n_base_url'])
		{
			$errors[] = lang('You must enter a base url');
		}
		else
		{
			if( substr($_POST['n_base_url'], -1) == '/') 
			{
				$_POST['n_base_url'] = substr($_POST['n_base_url'], 0 , -1);
			}
			
			if( substr($_POST['n_base_url'], 0, 4) != 'http') 
			{
				$_POST['n_base_url'] = 'http://' . $_POST['n_base_url'];
			}
			$n_base_url = $GLOBALS['phpgw']->db->db_addslashes($_POST['n_base_url']);
		}

		if (! $_POST['n_newsfile'])
		{
			$errors[] = lang('You must enter a news url');
		}
		else
		{
			if(substr($_POST['n_newsfile'], 0, 1) != '/')
			{
				$_POST['n_newsfile'] =  '/' . $_POST['n_newsfile'];
			}
			$n_newsfile = $GLOBALS['phpgw']->db->db_addslashes($_POST['n_newsfile']);
		}

		if (! intval($_POST['n_cachetime']))
		{
			$errors[] = lang('You must enter the number of minutes between reload');
		}
		else
		{
			$n_cachetime = intval($_POST['n_cachetime']);
		}

		if (! intval($_POST['n_listings']))
		{
			$errors[] = lang('You must enter the number of listings display');
		}
		else
		{
			$n_listings = intval($_POST['n_listings']);
		}

		$GLOBALS['phpgw']->db->query('SELECT display FROM phpgw_headlines_sites'
				. " WHERE base_url='" . strtolower($n_base_url) . "'"
				. " AND newsfile LIKE'$n_newsfile'", __LINE__, __FILE__);
		$GLOBALS['phpgw']->db->next_record();
		if ($GLOBALS['phpgw']->db->f('display'))
		{
			$errors[] = lang('That site has already been entered');
		}

		if (! is_array($errors))
		{
			$sql = "INSERT INTO phpgw_headlines_sites (display,base_url,newsfile,"
					. "lastread,newstype,cachetime,listings) "
					. "VALUES ('$n_display',"
					. "'" . strtolower($n_base_url) . "',"
					. "'$n_newsfile',0,"
					. "'" . $GLOBALS['phpgw']->db->db_addslashes($_POST['n_newstype']) . "',"
					. "$n_cachetime,$n_listings)";

			$GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);

			$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/headlines/admin.php','cd=28'));
		}
	}

	$GLOBALS['phpgw']->common->phpgw_header();
	echo parse_navbar();

	// This is done for a reason (jengo)
	$GLOBALS['phpgw']->template->set_root($GLOBALS['phpgw']->common->get_tpl_dir('headlines'));

	$GLOBALS['phpgw']->template->set_file(array(
		'admin_form' => 'admin_form.tpl'
	));
	$GLOBALS['phpgw']->template->set_block('admin_form','form');
	$GLOBALS['phpgw']->template->set_block('admin_form','buttons');

	if (is_array($errors))
	{
		$GLOBALS['phpgw']->template->set_var('messages',$GLOBALS['phpgw']->common->error_list($errors));
	}

	$GLOBALS['phpgw']->template->set_var('title',lang('Headlines Administration'));
	$GLOBALS['phpgw']->template->set_var('lang_header',lang('Create new headline'));
	$GLOBALS['phpgw']->template->set_var('th_bg',$phpgw_info['theme']['th_bg']);
	$GLOBALS['phpgw']->template->set_var('row_on',$phpgw_info['theme']['row_on']);
	$GLOBALS['phpgw']->template->set_var('row_off',$phpgw_info['theme']['row_off']);
	$GLOBALS['phpgw']->template->set_var('lang_display',lang('Display'));
	$GLOBALS['phpgw']->template->set_var('lang_base_url',lang('Base URL'));
	$GLOBALS['phpgw']->template->set_var('lang_news_file',lang('News File'));
	$GLOBALS['phpgw']->template->set_var('lang_minutes',lang('Minutes between refresh'));
	$GLOBALS['phpgw']->template->set_var('lang_listings',lang('Listings Displayed'));
	$GLOBALS['phpgw']->template->set_var('lang_type',lang('News Type'));
	$GLOBALS['phpgw']->template->set_var('lang_button',lang('Add'));

	$GLOBALS['phpgw']->template->set_var('input_display','<input name="n_display" value="' . htmlentities($n_display) . '" size="40">');
	$GLOBALS['phpgw']->template->set_var('input_base_url','<input name="n_base_url" value="' . htmlentities($n_base_url) . '" size="40">');
	$GLOBALS['phpgw']->template->set_var('input_news_file','<input name="n_newsfile" value="' . htmlentities($n_newsfile) . '" size="40">');
	$GLOBALS['phpgw']->template->set_var('input_minutes','<input name="n_cachetime" value="' . $n_cachetime . '" size="4">');
	$GLOBALS['phpgw']->template->set_var('input_listings','<input name="n_listings" value="' . $n_listings . '" size="2">');

	$news_type = array('rdf','fm','lt','sf','rdf-chan');
	while (list(,$item) = each($news_type))
	{
		$_select .= '<option value="' . $item . '"' . ($n_newstype == $item?' checked':'')
					. '>' . $item . '</option>';
	}
	$GLOBALS['phpgw']->template->set_var('input_type','<select name="n_newstype">' . $_select . '</select>');

	$GLOBALS['phpgw']->template->set_var('action_url',$GLOBALS['phpgw']->link('/headlines/newheadline.php'));

	$GLOBALS['phpgw']->template->parse('buttons','buttons');
	$GLOBALS['phpgw']->template->pfp('out','form');
	$GLOBALS['phpgw']->common->phpgw_footer();
?>
