<?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 ( GetSetting('portal','useRights',0) == 1 && !IsUserLogin()){
	ob_end_flush();
	exit;
}

if (!isset($_REQUEST['skpollingobject']) || !isset($_REQUEST['skpolltype']) || !is_numeric($_REQUEST['skpollingobject']) || !is_numeric($_REQUEST['skpolltype'])) {
	ob_end_flush();
	exit;
}

$cn = db_connect();
$idbranch = '';
$usr_arr = GetUserIDs();
$skpollingobject = $_REQUEST['skpollingobject'];
$skpollingobjectUrlPart = 'skpollingobject='.$_REQUEST['skpollingobject'].'&skpolltype='.$_REQUEST['skpolltype'];
$pOStatus = db_getPollObjectStatus($cn, $skpollingobject, $_REQUEST['skpolltype']);
$isuserassigned = db_getSKBranchByUserAnswerValue($cn, $skpollingobject, $_REQUEST['skpolltype'], $usr_arr[0] ) != 0 && $pOStatus == 1;
if (isset($_REQUEST['skbranch'])) {
	$skbranch = $_REQUEST['skbranch'];
	$idbranch = db_getIDBranch($cn, $skbranch );	
} 

if ($idbranch == '' || !db_isBrancheComplite($cn, $skbranch) ) {
	db_disconnect($cn);
	ob_end_flush();
	exit;
}

$rguid = $_REQUEST['rguid'];
$oguid = $_REQUEST['oguid'];
$poguids = db_getGuidsByPollingObject($cn, $skpollingobject);
if (is_array($poguids) && sizeof($poguids) > 0 && sizeof($poguids[0]) > 0){
        $pollobjectoguid = $poguids[0]['o_guid'];
        $pollobjectrguid = $poguids[0]['r_guid'];
} else {
	db_disconnect($cn);
	ob_end_flush();
	exit;
}

$objecthash = db_getObjectVersionuHashByPollingObject($cn, $skpollingobject);

if  (!isset($_REQUEST['oguid']) || !db_isObjectExits($cn, $_REQUEST['oguid'], $skbranch)) {
	db_disconnect($cn);
	ob_end_flush();
	exit;
}
if  (db_getUserRightsToObject($cn, GetUserIDs(), $_REQUEST['oguid'], $skbranch) === null ){
	header("Location: ".getCurPageURLWO('service')."notenoughrights.php?url=".getCurPageWithEncodeParams());
	ob_end_flush();
	exit;
}

$url = getCurPageURL();
$urlWO = getCurPageURLWO();
$contlng = NULL;
$contlngid = NULL;

if (getSetting('portal','useRights',0) == 1){
  $contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
  $contlngid = $_SESSION['user_idlanguagecontent'];
} else {
	$contlng = 'lang='.db_getDefaultLangShortName($cn);
	$contlngid = db_getDefaultLangID($cn);
}
$lngParam = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'].'&' : '');

$objectName = db_getObjectName($cn, $oguid, $contlngid, $skbranch);
$reportName = ''.$rguid;
if ($oguid !== $rguid) {
	$reportName = db_getReportName($cn, $rguid, $contlngid, $skbranch);
}

$sourceparams = db_getPollingObjectSource($cn, $skpollingobject);
$idpolltype = db_getIDPollTypeBySK($cn,(isset($_REQUEST['skpolltype']) ? $_REQUEST['skpolltype'] : NULL));
$urlpoll = getCurPageURLWO('service').'/poll.php?idpolltype='.$idpolltype.(isset($_REQUEST['skproject']) ? '&skproject='.$_REQUEST['skproject'] : (isset($_REQUEST['skbranch']) ? '&skbranch='.$_REQUEST['skbranch'] : ''));
$objectfullname = $objectName.' ('.$reportName.')';

if (isset($_REQUEST['skpollingobject']) ) {
	header('HTTP/1.0 200 Ok');
?> 
		<div class="inline-float-left">
			<div id="box-poll-bread-crumbs-text" >
			<?php if ($isuserassigned && isset($_REQUEST['idpolltype'])) {?>
				<a href="polls.php"><?php getResource("title.polls");?></a> \ <a id="poll-part-bread-crumbs" href="<?php echo $urlpoll;?>"><?php echo ''.db_getPolltypeName($cn, $idpolltype, $contlngid).': '; if ($sourceparams[0]['source'] === 'skproject') { echo db_getProjectName($cn, $sourceparams[0]['id'], $contlngid);} else { echo db_getBranchName($cn, $sourceparams[0]['id'], $contlngid);} ?></a> \ <a style="display:none;" href="#" oguid="<?php echo $oguid;?>" rguid="<?php echo $rguid;?>" class="selfPollReload box-poll-bread-crumbs-objectfullname-link"> <?php echo $objectfullname;?></a> <span class="box-poll-bread-crumbs-objectfullname-span"> <?php echo $objectfullname;?></span> 
			<?php } else {?>
				<a href="polls.php"><?php getResource("title.polls");?></a> \ <?php echo db_getBranchName($cn, $skbranch, $contlngid, NULL); ?> \ <?php echo $objectfullname;?> 
			<?php } ?>
			</div>
		</div>
		<div class="float-right font-awesome-color" style="margin-top: 8px;">
		<?php
		if (isset($_REQUEST['filteruniqid']) && isset($_SESSION['pollsfilteredlist'.$_REQUEST['filteruniqid']]) && is_array($_SESSION['pollsfilteredlist'.$_REQUEST['filteruniqid']]) && sizeof($_SESSION['pollsfilteredlist'.$_REQUEST['filteruniqid']]) > 0) {
			$resarr = $_SESSION['pollsfilteredlist'.$_REQUEST['filteruniqid']];
		} else {
			db_getPollObjetcList($cn, (isset($_REQUEST['idpolltype']) ? $_REQUEST['idpolltype'] : NULL), (isset($_REQUEST['skproject']) ? $_REQUEST['skproject'] : NULL),  (isset($_REQUEST['skbranch']) ? $_REQUEST['skbranch'] : NULL), $contlngid, $usr_arr, (isset($_REQUEST['foldergroup']) ? $_REQUEST['foldergroup'] : NULL), (isset($_REQUEST['answers']) ? $_REQUEST['answers'] : NULL) );
			$resarr = array();
			$q_st = "SELECT COUNT(0) as cnt FROM `poll` ";
			$resarr = db_runQueryAndReturnArray($cn,$q_st);
			if ($resarr[0]['cnt'] > 1) {
				$q_st = 'SELECT `skpollingobject`, `skpolltype` , `skbranch` , `o_guid` , `r_guid` , `idobject` , `idreport`  FROM `poll` ORDER BY o_name desc';
				$resarr = db_runQueryAndReturnArray($cn,$q_st);
			}
		}
		session_write_close();
		$size = (is_array($resarr) ? sizeof($resarr) : 0 );
		if ($size > 1) {
			$curentIndexOfPollObject = 0;
			for($i=0; $i<$size; $i++){						
				if ($resarr[ $i ][ "r_guid" ] == $pollobjectrguid && $resarr[ $i ][ "o_guid" ] == $pollobjectoguid && $resarr[ $i ][ "skbranch" ] == $skbranch && $resarr[ $i ][ "skpollingobject" ] == $skpollingobject && $resarr[ $i ][ "skpolltype" ] == (isset($_REQUEST['skpolltype']) ? $_REQUEST['skpolltype'] : '') ){
					$curentIndexOfPollObject = $i;
					break;
				}
			}
			$ulrPONextIndex = ( isset($resarr[$curentIndexOfPollObject+1]) ? $curentIndexOfPollObject + 1 : 0 );
			$ulrPONext = $urlWO.'/pollobjectbreadcrumbs.php?skpollingobject='.$resarr[ $ulrPONextIndex ][ "skpollingobject" ].'&skpolltype='.$resarr[ $ulrPONextIndex ][ "skpolltype" ].'&skbranch='.$resarr[ $ulrPONextIndex ][ "skbranch" ].'&oguid='.$resarr[ $ulrPONextIndex ][ "o_guid" ].'&rguid='.$resarr[ $ulrPONextIndex ][ "r_guid" ].(isset($_REQUEST['idpolltype']) ? '&idpolltype='.$_REQUEST['idpolltype'] : '').(isset($_REQUEST['skproject']) ? '&skproject='.$_REQUEST['skproject'] : '').'&idbranch='.db_getIDBranch($cn,$resarr[ $ulrPONextIndex ][ "skbranch" ]).(isset($_REQUEST['filteruniqid']) ? '&filteruniqid='.$_REQUEST['filteruniqid'] : '');
			$ulrPOPrevIndex = ( isset($resarr[$curentIndexOfPollObject-1]) ? $curentIndexOfPollObject - 1 : $size - 1 );	
			$ulrPOPrev = $urlWO.'/pollobjectbreadcrumbs.php?skpollingobject='.$resarr[ $ulrPOPrevIndex ][ "skpollingobject" ].'&skpolltype='.$resarr[ $ulrPOPrevIndex ][ "skpolltype" ].'&skbranch='.$resarr[ $ulrPOPrevIndex ][ "skbranch" ].'&oguid='.$resarr[ $ulrPOPrevIndex ][ "o_guid" ].'&rguid='.$resarr[ $ulrPOPrevIndex ][ "r_guid" ].(isset($_REQUEST['idpolltype']) ? '&idpolltype='.$_REQUEST['idpolltype'] : '').(isset($_REQUEST['skproject']) ? '&skproject='.$_REQUEST['skproject'] : '').'&idbranch='.db_getIDBranch($cn,$resarr[ $ulrPOPrevIndex ][ "skbranch" ]).(isset($_REQUEST['filteruniqid']) ? '&filteruniqid='.$_REQUEST['filteruniqid'] : '');
		?>						
		<a class="poll-bread-crumbs-arrow-link" href="<?php echo $ulrPOPrev;?>"><i class="fas fa-arrow-up fa-lg"></i></a><a class="poll-bread-crumbs-arrow-link" style="padding-left: 10px;" href="<?php echo $ulrPONext;?>"><i class="fas fa-arrow-down fa-lg"></i></a>
		<?php
		}
		?>
		</div>
<?}?>

<?php
db_disconnect($cn);
ob_end_flush();
?>
