<?php

/*  Alternate WebKit
(C) 2012-2014 by Alternate Tools
-----------------------------------------------------

File:         site_survey.php
Description:  Survey site

*/

    include "modules/mod_site.php";
    include "modules/class_textfile.php";
    include "modules/class_survey.php";
    
    $errorString = "";
    $bErrorOccured = false;

    // Do not show survey page, if turned off (even direct link name)
    if ($SITE_SHOWSURVEY == false)
    {
        header ("location: " . $SITE_MAIN_SITE);
        return;
    }
    if ($SITE_SHOWCONSTRUCTION == true)
    {
        header ("location: " . $SITE_CONSTRUCTION_SITE);
        return;
    }

    // Send survey data?
    if (isset($_POST['survey_Send']) == true)
    {
        $inputValues = "";
        // Check answer
        if (isset($_POST['survey_Option']) == false || $_POST['survey_Option'] == "")
        {
            $errorString = '<br><b>' . get_translation('admin_survey_errorinput') . ' </b><br>' . $LINE_FEED;
            $bErrorOccured = true;
        }
        else   // Check input fields
        {
            for ($i = 0; $i < $_POST['survey_InputFieldsCount']; $i ++)
            {
                if (isset($_POST['survey_InputField' . $i]) == false || $_POST['survey_InputField' . $i] == "")
                {
                    $errorString = '<br><b>' . get_translation('admin_survey_errorinput') . ' </b><br>' . $LINE_FEED;
                    $bErrorOccured = true;
                }
                else
                {
                    if ($i == 0)
                        $inputValues = $_POST['survey_InputField' . $i];
                    else
                        $inputValues = $inputValues . ";" . $_POST['survey_InputField' . $i];
                }
            }
        }
        if ($bErrorOccured == false)  // Store result...
        {
            $surSurvey = new cSurvey;
            $surSurvey->CreateSurvey();
            $surSurvey->SetFolder(retrieve_absolute_path($SURVEY_FOLDER));
            $surSurvey->SetTitle($SURVEY_TITLE);
            $surSurvey->SetQuestion($SURVEY_QUESTION);
            $surSurvey->SetOptions($SURVEY_OPTIONS);
            $surSurvey->SetSelectedOption($_POST['survey_Option']);
            $surSurvey->SetInputNames($SURVEY_INPUTFIELDS);
            $surSurvey->SetInputValues($inputValues);

            if ($surSurvey->SaveSurvey() == true)
            {
                // Send notification mail?
                if ($SURVEY_SENDMAIL != false)
                {
                    $contMessage = get_translation('admin_survey_date') . ': ' . $surSurvey->GetDate() . $LINE_FEED;
                    $contMessage = $contMessage . (get_translation('admin_survey_title') . ': ' . $surSurvey->GetTitle() . $LINE_FEED);
                    $contMessage = $contMessage . (get_translation('admin_survey_question') . ': ' . $surSurvey->GetQuestion() . $LINE_FEED);
                    $contMessage = $contMessage . (get_translation('admin_survey_answer') . ': ' . $surSurvey->GetSelectedOption() . $LINE_FEED);
                    $contMessage = $contMessage . $LINE_FEED;
                    $inputNames = explode(";", $surSurvey->GetInputNames());
                    $inputValues = explode(";", $surSurvey->GetInputValues());
                    for ($i = 0; $i < count($inputNames); $i ++)
                    {
                        $contMessage = $contMessage . ($inputNames[$i] . ': ' . $inputValues[$i] . $LINE_FEED);
                    }
                    $mailAddress = $SITE_CONTACT_ADDRESS;
                    if ($SURVEY_ADDITIONALRECIPIENTS != "")
                        $mailAddress = $mailAddress . ',' . $SURVEY_ADDITIONALRECIPIENTS;
                    send_mail($mailAddress, get_translation('site_survey_messagefrom') . ' '  . $SITE_ADDRESS, $contMessage, 'From:' . $SITE_CONTACT_ADDRESS);
                }
                header ("location: " . $SITE_MAIN_SITE);
                return;
            }
            else
            {
                $errorString = '<br><b>' . get_translation('admin_survey_errorsave') . ' </b><br>' . $LINE_FEED;
                $bErrorOccured = true;
            }
        }
    }

    $cFile = new cTextFile;
    
    $contPage = build_page($SITE_THEME, false, false);
    $contPage = fill_titles($contPage, $SITE_TITLE . ' - ' . $SURVEY_TITLE, $SURVEY_TITLE);
    // Error occured?
    if ($errorString != "")
        $contPage = build_error_message($errorString, $contPage);

    $contContent = "";
    // Question
    $contContent .= echo_sr($SURVEY_QUESTION . '<br><br>');
    // Answering otpions
    $contContent .= echo_sr('<form method="post" action="">');;
    $aOptions = explode(";", $SURVEY_OPTIONS);
    $contContent .= echo_sr('<table class="surveyoption_tbl" width="100%" border="0">');
    if ($SURVEY_OPTIONSHORIZONTAL == true)
        $contContent .= echo_sr('  <tr class="surveyoption_tbltr" id="surveyoption_tbltr1">');

    for ($i = 0; $i < count($aOptions); $i ++)
    {
        if ($SURVEY_OPTIONSHORIZONTAL == false)
            $contContent .= echo_sr('  <tr class="surveyoption_tbltr" id="surveyoption_tbltr' . ($i + 1) . '">');
        $contContent .= echo_sr('    <td class="surveyoption_tbltd" id="surveyoption_tbltd' . ($i + 1) . '"><input type="radio" name="survey_Option" class="survey_Option" id="survey_Option' . ($i + 1) . '" value="' . $aOptions[$i] .'"></td>');
        $contContent .= echo_sr('    <td class="surveyoption_tbltd2" id="surveyoption_tbltd2_' . ($i + 1) . '">' . $aOptions[$i] .'</td>');
        if ($SURVEY_OPTIONSHORIZONTAL == false)
            $contContent .= echo_sr('  </tr>');
    }
    if ($SURVEY_OPTIONSHORIZONTAL == true)
        $contContent .= echo_sr('  </tr>');
    $contContent .= echo_sr('</table>');
    $contContent .= echo_sr('<input type="hidden" name="survey_Options" value="' . $SURVEY_QUESTION .'">');
    // Input fields
    $aInputFields = explode(";", $SURVEY_INPUTFIELDS);
    $contContent .= echo_sr('<table class="surveyinput_tbl" width="100%" border="0">');
    for ($i = 0; $i < count($aInputFields); $i ++)
    {
        $contContent .= echo_sr('  <tr class="surveyinput_tbltr" id="surveyinput_tbltr' . ($i + 1) .'">');
        $contContent .= echo_sr('    <td class="surveyinput_tbltd" id="surveyinput_tbltd' . ($i + 1) .'">' . $aInputFields[$i] . ':</td>');
        if (isset($_POST['survey_InputField' . $i]) == false)
                  $_POST['survey_InputField' . $i] = "";
        $contContent .= echo_sr('    <td class="surveyinput_tbltd2" id="surveyinput_tbltd2_' . ($i + 1) .'"><input class="survey_InputField" id="survey_InputField' . ($i + 1) . '" type="text" size="60" name="survey_InputField' . $i . '" value="' . $_POST['survey_InputField' . $i] . '"></td>');
        $contContent .= echo_sr('  </tr>');
    }
    $contContent .= echo_sr('</table>');
    $contContent .= echo_sr('<input type="hidden" name="survey_InputFields" value="' . $SURVEY_INPUTFIELDS .'">');
    $contContent .= echo_sr('<input type="hidden" name="survey_InputFieldsCount" value="' . $i .'">');
    // Text
    $contContent .= echo_sr($cFile->LoadFromFile($SURVEYTITLE_CONFIG_FILE));
    $contContent .= echo_sr('<br><br><input type="submit" class="survey_btn" name="survey_Send" value="' . get_translation('admin_send') . '">');
    $contContent .= echo_sr('</form>');

    $contContent .= echo_sr('<br><br>');
    $contContent .= echo_sr('<a href="' . $SITE_MAIN_SITE . '">' . get_translation('admin_continuewithmainpage') . '</a><br><br>');
    $contContent = $contContent . build_site_counter();

    $contPage = fill_content($contPage, $contContent);

    echo_r($contPage);

?>