<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc');
}

if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/includesroutinesadmin.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/includesroutinesadmin.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/includesroutinesadmin.inc');
}

if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/pollobjectsarc.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/pollobjectsarc.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/pollobjectsarc.inc');
}

if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/db/mysqlroutines.mail.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/db/mysqlroutines.mail.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/db/mysqlroutines.mail.inc');
}


$cn = db_connect();
$usr_arr = GetUserIDs();
if (getSetting('portal','useRights',0) == 1 && IsUserLogin() == 1 && (isUserAdmin() || db_isUserAdminToPoll($cn, GetUserIDs(), $_REQUEST['skpolltype'], (isset($_REQUEST['skproject']) ? $_REQUEST['skproject'] : NULL), (isset($_REQUEST['skproject']) ? NULL : $_REQUEST['skbranch']))) && 
	isset($_REQUEST['idpolltype']) && isset($_REQUEST['newStatus']) && ($_REQUEST['newStatus'] >= -1 && $_REQUEST['newStatus'] < 4)  && isset($_REQUEST['skpolltype']) && (isset($_REQUEST['skproject']) or isset($_REQUEST['skbranch']))){
	$idpolltype = $_REQUEST['idpolltype'];
	$newStatus = $_REQUEST['newStatus'];
	$skproject = (isset($_REQUEST['skproject']) ? $_REQUEST['skproject'] : NULL);
	$skbranch = (isset($_REQUEST['skbranch']) ? $_REQUEST['skbranch'] : NULL);
	switch ($newStatus) {
		case -1:
			$keys = db_getPollObjectKeys($cn, $idpolltype, $skproject, $skbranch);
			for($p=0; $p<sizeof($keys); $p++){
				$skpollingobject = $keys[$p]['skpollingobject'];
				if(isset($_REQUEST['notify']))
					db_createMailQueue_DeletePoll($cn, $idpolltype, $skbranch, $skproject);
				deletePollObjectArc($cn, $skpollingobject, $idpolltype);
				db_deletePollingObject($cn, $skpollingobject, $idpolltype);
			}
			break;
		case 0:
			if(isset($_REQUEST['notify']))
				db_createMailQueue_StopPoll($cn, $idpolltype, $skbranch, $skproject);
			db_setStatusToPoll($cn, $_REQUEST['idpolltype'], $skproject, $skbranch, $newStatus);
			break;
		case 1:
			if(isset($_REQUEST['notify']))
				db_createMailQueue_ContinuePoll($cn, $idpolltype, $skbranch, $skproject);
			db_setStatusToPoll($cn, $_REQUEST['idpolltype'], $skproject, $skbranch, $newStatus);
			break;
		case 2:
			if(isset($_REQUEST['notify']))
				db_createMailQueue_HidePoll($cn, $idpolltype, $skbranch, $skproject);
			db_setStatusToPoll($cn, $_REQUEST['idpolltype'], $skproject, $skbranch, $newStatus);
			break;
	}		
	renderPollStatusChangeLinks($cn, $usr_arr, $_REQUEST['idpolltype'], $_REQUEST['skpolltype'], (isset($_REQUEST['skproject']) ? $_REQUEST['skproject'] : NULL),  (isset($_REQUEST['skbranch']) ? $_REQUEST['skbranch'] : NULL), $_REQUEST['newStatus']);
}
ob_end_flush();
?>