<?php
	/**************************************************************************\
	* phpGroupWare - Polls                                                     *
	* http://www.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: admin_addanswer.php,v 1.14.2.2.2.5 2005/12/08 11:44:59 skwashd Exp $ */

	$GLOBALS['phpgw_info'] = array();
	$GLOBALS['phpgw_info']['flags'] = array(
		'admin_only'              => True,
		'currentapp'              => 'polls',
		'enable_nextmatchs_class' => True,
		'admin_header'            => True
	);
	include('../header.inc.php');

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

	if ( isset($_POST['submit']) && $_POST['submit'] )
	{
		$poll_id = intval($_POST['poll_id']);
		$answer  = $GLOBALS['phpgw']->db->db_addslashes(htmlentities($_POST['answer']));
		$vote_id = intval($_POST['vote_id']);

		$GLOBALS['phpgw']->db->query("SELECT MAX(vote_id)+1 FROM phpgw_polls_data WHERE poll_id = $poll_id",__LINE__,__FILE__);
		$GLOBALS['phpgw']->db->next_record();
		$vote_id = ($GLOBALS['phpgw']->db->f(0) ? $GLOBALS['phpgw']->db->f(0) : 1);

		$GLOBALS['phpgw']->db->query('INSERT INTO phpgw_polls_data (poll_id,option_text,option_count,vote_id) '
			. "VALUES ($poll_id, '$answer', 0, $vote_id)",__LINE__,__FILE__);
		$GLOBALS['phpgw']->template->set_var('message',lang('Answer has been added to poll'));
	}

	$GLOBALS['phpgw']->template->set_var('header_message',lang('Add answer to poll'));
	$GLOBALS['phpgw']->template->set_var('td_message','&nbsp;');
	$GLOBALS['phpgw']->template->set_var('th_bg',$GLOBALS['phpgw_info']['theme']['th_bg']);
	$GLOBALS['phpgw']->template->set_var('form_action',$GLOBALS['phpgw']->link('/polls/admin_addanswer.php'));
	$GLOBALS['phpgw']->template->set_var('form_button_1','<input type="submit" name="submit" value="' . lang('Add') . '">');
	$GLOBALS['phpgw']->template->set_var('form_button_2','</form><form method="POST" action="' . $GLOBALS['phpgw']->link('/polls/admin.php') . '"><input type="submit" name="submit" value="' . lang('Cancel') . '">');

	$poll_select = '<select name="poll_id">';
	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_polls_desc",__LINE__,__FILE__);
	while ($GLOBALS['phpgw']->db->next_record())
	{
		$poll_select .= '<option value="' . $GLOBALS['phpgw']->db->f('poll_id') . '"';
		if ($poll_id == $GLOBALS['phpgw']->db->f('poll_id'))
		{
			$poll_select .= ' selected';
		}
		$poll_select .= '>' . stripslashes($GLOBALS['phpgw']->db->f('poll_title')) . '</option>';
	}
	$poll_select .= '</select>';

	add_template_row($GLOBALS['phpgw']->template,lang('Which poll'),$poll_select);
	add_template_row($GLOBALS['phpgw']->template,lang('Answer'),'<input name="answer">');

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