<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}
include ( 'sessioninc.php' );

define( 'PAGE_ID', 'poll_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;
}

if (  $_GET['edit'] ) {

	$id = $_GET['edit'];

	$t->assign( 'data', $osDB->getRow( 'SELECT * FROM ! WHERE optionid = ?', array( POLLOPTS_TABLE, $id ) ) );

	$t->assign( 'error	', get_lang('poll_error',$_GET['errid']) );

	$t->assign('rendered_page', $t->fetch('admin/polloptsedit.tpl'));

	$t->display( 'admin/index.tpl' );

	exit;
}

if ( $_POST['groupaction']  ) {

	$pollid =  $_POST['txtpollid'];

	$optid =  $_POST['txtoptionid'];

	$opts =  $_POST['txtcheck'];

	if ( $_POST['groupaction']	== get_lang('enable_selected') ) {

		foreach( $opts as $val ) {

			$osDB->query('UPDATE ! SET enabled = ? WHERE optionid = ? AND pollid = ?', array( POLLOPTS_TABLE, 'Y', $val, $pollid ) );
		}
	} elseif ( $_POST['groupaction']	== get_lang('disable_selected') ) {

		foreach( $opts as $val ) {

			$osDB->query( 'UPDATE ! SET enabled = ? WHERE optionid = ? AND pollid = ?', array( POLLOPTS_TABLE, 'N', $val, $pollid ) );

		}

	} elseif ( $_POST['groupaction']	== get_lang('delete_selected') ) {

		foreach( $opts as $val ) {

			$osDB->query('DELETE FROM ! WHERE optionid = ? AND pollid = ?', array( POLLOPTS_TABLE, $val, $pollid ) );

		}

	}
	unset($opts);

}

if ( $_POST['frm'] == 'frmDelOption' ) {

	$pollid 	= $_POST['txtpollid'];

	$optid 	= $_POST['txtoptionid'];

	$osDB->query('DELETE FROM ! WHERE optionid = ? AND pollid = ?', array( POLLOPTS_TABLE, $optid, $pollid ) );

}

if ( $_GET['pollid'] ) {

	$pollid = $_GET['pollid'];

} else {

	$pollid = $_POST['txtpollid'];

}

//Default Sorting

$sort = findSortBy('optionid');

$t->assign( 'data', $osDB->getAll('SELECT * from ! WHERE pollid = ? order by ' . $sort, array( POLLOPTS_TABLE, $pollid ) ) );

$t->assign( 'lang', $lang );

$t->assign( 'poll_question', stripslashes( $osDB->getOne( 'SELECT question from ! WHERE pollid = ?', array( POLLS_TABLE, $pollid ) ) ) );

$t->assign( 'pollid', $pollid );

$t->assign( 'sort_type', checkSortType( $_GET['type'] ) );

$t->assign('rendered_page', $t->fetch('admin/polloptions.tpl'));

$t->display( 'admin/index.tpl' );


?>