<?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/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');
}


checkUserAndRedirectToLogin();
RedirectToModelAnonymous();

$cn = db_connect();
$lng = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'] : '');
$lngid = db_getLanguageID($cn, getLanguage());
$lngshort = db_getLanguageShortByID($cn, empty($lngid) ? $_SESSION['user_idlanguageinterface'] : $lngid, true);
$contlng = $lng;
$contlngid = $lngid;
if (getSetting('portal','useRights',0) == 1){
	$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
	$contlngid = $_SESSION['user_idlanguagecontent'];
}

if ( getSetting('portal','useRights',0) == 1 && !IsUserLogin()){
        header("Location: ".getCurPageURLWO()."/login.php");
        ob_end_flush();
        db_disconnect($cn);
        exit;
} else if ( GetSetting('portal','useRights',0) == 0 ) {
        header("Location: ".getCurPageURLWO()."/buisnessmodel.php");
        ob_end_flush();
        db_disconnect($cn);
        exit;
}

$id = 0;

if (!isset($_REQUEST['skpollingobject']) || !isset($_REQUEST['skpolltype']) || !is_numeric($_REQUEST['skpollingobject']) || !is_numeric($_REQUEST['skpolltype'])) {
        header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
        ob_end_flush();
        $oguid = null;
        exit;
}

$usr_arr = GetUserIDs();
$isuser = $usr_arr[0];
$skpollingobject = $_REQUEST['skpollingobject'];
$skpolltype =$_REQUEST['skpolltype'];

$skbranch = db_getSKBranchByUserAnswerValue($cn, $skpollingobject, $skpolltype , $isuser );
$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);
        header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
        ob_end_flush();
        exit;
}

if ($skbranch == 0 || $skbranch === NULL ) {
        db_disconnect($cn);
        header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
        ob_end_flush();
        exit;
}

//Task 60586
$pOStatus = db_getPollObjectStatus($cn, $_REQUEST['skpollingobject'],$_REQUEST['skpolltype']);
if ($pOStatus != 1 ){
        db_disconnect($cn);
        echo "<div id='error'><div id='errormessage'>";
        getResource("message.haveNotRights.pollanswer");
        echo "</div></div>";
        ob_end_flush();
        exit;
}


$allowComment = db_getPollTypeAllowComment($cn, $_REQUEST['skpolltype']);
if (isset($_REQUEST['valueadd'] )) {
        $uav_value = (isset($_REQUEST['uav_value']) && $_REQUEST['uav_value'] === 'on' ? 1 : (isset($_REQUEST['uav_value']) ? $_REQUEST['uav_value'] : 0));
        $postonforum  = (isset($_REQUEST['postonforum'] ) && $_REQUEST['postonforum'] === 'true');
        $uav_body = (isset($_REQUEST['message']) ? $_REQUEST['message'] : '');
        db_savePolltypeQuestionAnswer($cn, $skpollingobject, $skpolltype, $usr_arr[0], $uav_value, $uav_body, $postonforum, $pollobjectoguid, $skbranch );
        createPollObjectArc($cn, $skpollingobject, $isuser);
}
$isreadonly = FALSE;
?>
<!doctype html>
<html>
<head>
        <title><?php echo renderTitle();?></title>
        <? 
        renderCommonMeta(renderTitle()); 
        renderCommonCSS(); 
        ?>
</head>
<body>
        <?php renderModalHourGlassDiv();?>
        <div id="container-on-window">
                <div id="content" class="ui-layout-center">
                        <br/>
                        <form name="sendForm" id="sendForm" action="" method="post" enctype="multipart/form-data" style="margin-bottom: 15px;">
                                <div id="versiondescription" class="pollobjectanswer-answer"><nobr>
                                <?php getResource("title.pollobjectanswer.answer");?>:
                                <?php 
                                        $uav_value = 0; 
                                        $uav_body = '';
                                        $uav_tmst = '';
                                        $ss_key = '';
                                        $idforummessage = 0;
                                        $answer = db_getPolltypeQuestionAnswer($cn, $skpollingobject, $skpolltype, $usr_arr[0] );
                                        if ($answer !== NULL && is_array($answer) && sizeof($answer) > 0 && sizeof($answer[0]) > 0 ) {
                                                $uav_value = $answer[0]['uav_value']; 
                                                $uav_body = $answer[0]['uav_body'];
                                                $uav_tmst = $answer[0]['uav_tmst'];
                                                $ss_key = (isset($answer[0]['ss_key']) ? $answer[0]['ss_key'] : '');
                                                $idforummessage = ($answer[0]['uav_body'] === NULL ? 0 : $answer[0]['idforummessage'] );
                                        }
                                        $isreadonly = (!isset($_SESSION['ss_key']) || (db_getPollTypeAllowToChangeAnswer($cn, $skpolltype) == 1 && $_SESSION['ss_key'] != $ss_key && $ss_key != '' && isset($answer[0]['uav_value']) && $answer[0]['uav_value'] != 0 /* 62767 */));
                                        $qst = db_getPolltypeQuestion($cn, $skpollingobject, $skpolltype, $usr_arr[0], $contlngid );
                                        if ($qst === NULL || !is_array($answer) || !(sizeof($qst) > 0) || !(sizeof($qst[0]) > 0) ) {
                                                db_disconnect($cn);
                                                header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
                                                ob_end_flush();
                                                exit;
                                        }
                                        if ($qst[0]['idpolltypequestiontype'] == 1) {
                                                echo '<input '.($uav_value > 0 ? 'checked' : '').' type="checkbox" id="uav_value" name="uav_value" '.($isreadonly ? 'disabled readonly' : '').'><label id="label_uav_value" for="uav_value">'.$qst[0]['rft_columnname_text'].'</label>';
                                        } else if ($qst[0]['idpolltypequestiontype'] == 2) {
                                                echo '<select id="uav_value" name="uav_value"'.($isreadonly ? 'disabled="true" readonly' : '').' style="margin-right: 8px;">';
                                                $qsv = db_getQuestionSelectValues($cn, $qst[0]['skpolltypequestion'], $contlngid );
                                                echo '<option value="0"></option>';
                                                for($i=0; $i<sizeof($qsv); $i++){
                                                        echo '<option '.($uav_value == $qsv[$i]['skpolltypequestionselectvalue'] ? 'selected' : '').'  value="'.$qsv[$i]['skpolltypequestionselectvalue'].'">'.$qsv[$i]['rft_columnname_text'].'</option>';
                                                } 
                                                echo '</select>';
                                        }
                                        if (is_array($answer) && sizeof($answer) > 0 && isset($answer[0]) && isset($answer[0]['uav_value']) && strlen($answer[0]['uav_value']) > 0 && $answer[0]['uav_value'] != 0 ) {
                                                echo '  '.$uav_tmst.' ';
                                                //remove archive icon (Task 61845)
                                        }
                                ?>
                                </nobr></div> 
                                <?php if ($allowComment){ ?>
                                        <h3 class="pollobjectanswer-h3"><?php getResource("title.pollobjectanswer.comment");?>:</h3>
                                        <textarea id="message" name="message" cols="80" rows="4" style="width: 100%;"><?php echo  $uav_body;?></textarea>
                                <?}?>        
                                <input type="hidden" id="valueadd" name="valueadd" value="true">
                                <input type="hidden" id="postonforum" name="postonforum" value="false">
                        </form>
                        <span><button <?php if ($isreadonly) {echo 'disabled="disabled"';} ?> name="buttonSendForm" id="buttonSendForm" class="btn-top btn-top-text <?php if ($isreadonly) {echo 'btn-top-text-disabled';} ?>" title="<?php getResource("button.SendAnswer.hint");?>"><span><?php getResource("button.SendAnswer");?></span></button></span>
                        <?php /* Task 59715
                        <?php if ($allowComment){ ?>
                                <span><button name="buttonSendFormAndPostForum" id="buttonSendFormAndPostForum" style="margin-left: 9px;" class="btn-top btn-top-text" title="<?php getResource("button.PostForum.hint");?>"><span><?php getResource("button.PostForum");?></span></button></span>
                                <div id="forummessagelink" style="margin-top: 10px;">
                                        <?php if ($idforummessage > 0) {?> 
                                                <a target="_parent" href="<?php echo getCurPageURLWO()."/pollobject.php?skpollingobject=".$skpollingobject."&skpolltype=".$skpolltype."&skbranch=".$skbranch."&forumid=".$idforummessage.'&oguid='.$pollobjectoguid.'&rguid='.$pollobjectrguid.(isset($_REQUEST['idpolltype']) ? '&idpolltype='.$_REQUEST['idpolltype'] : '');?>"><?php getResource("field.forum.messagelink");?></a>
                                        <?}?>        
                                </div>
                        <?}?>
                        <? */ ?>
                </div>
        </div>
        <?php renderFooterTemplate(); ?>
</body>
<script src="js/libs/jquery.1.8.3.js"></script>
<script src="ckeditor/ckeditor.js"></script>
<script >
var GForumOGUID = '<?php echo $pollobjectoguid;?>';
var gShortLang = '<?php echo $lngshort;?>';
var GForumEmprtyMessageAlert = '<?php getResource("message.messageIsEmpty");?>';
var GAnswerEmprtyMessageAlert = '<?php getResource("message.answerIsEmpty");?>';
var existUnsavedData = false;
var gConfirmsavelesValue ="<?php getResource("confirm.savelesValue");?>";
var gIsReadOnly = <?php echo ($isreadonly ? "true" : "false"); ?>;
</script>
<script src="js/pollobjectanswer.js"></script>
<script src="js/script.js"></script>
</html>
<?php
db_disconnect($cn);
ob_end_flush(); ?>
