<?php

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

File:         edit_survey.php
Description:  Administrative module to survey

*/

    include "../modules/mod_admin.php";
    include "../modules/class_textfile.php";

    $bErrorOccured = false;
    $errorString = "";
    $cFile = new cTextFile;

    // Check privilege
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_reporter() == true)
    {
        header('location: '. $ADMIN_MAIN_SITE);
        return;
    }

    // Cancel titles?
    if (isset($_POST['survey_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }
    
    // Save survey page?
    if (isset($_POST['survey_Save']))
    {
        if (isset($_POST['survey_Title']) == false || $_POST['survey_Title'] == "" ||
            isset($_POST['survey_Question']) == false || $_POST['survey_Question'] == "" ||
            isset($_POST['survey_Options']) == false || $_POST['survey_Options'] == "" ||
            isset($_POST['survey_InputFields']) == false || $_POST['survey_InputFields'] == "")
        {
            $bErrorOccured = true;
            $errorString = get_translation('admin_survey_errorinput') . "!";
        }
        if ($bErrorOccured == false)
        {
            // Activate
            $cConfig->SetParameter("site_show_survey", isset($_POST['survey_Activate']) == true);
            // Params
            $cConfig->SetParameter("survey_title", $_POST['survey_Title']);
            $cConfig->SetParameter("survey_question", $_POST['survey_Question']);
            $cConfig->SetParameter("survey_options", $_POST['survey_Options']);
            $cConfig->SetParameter("survey_options_horizontal", isset($_POST['survey_OptionsHorizontal']) == true);
            $cConfig->SetParameter("survey_inputfields", $_POST['survey_InputFields']);
            $cConfig->SetParameter("survey_sendmail", isset($_POST['survey_SendMail']) == true);
            $cConfig->SetParameter("survey_additionalrecipients", $_POST['survey_AdditionalRecipients']);
            // Survey text
            $cFile->SaveToFile('../' . $SURVEYTITLE_CONFIG_FILE, $_POST['survey_Text']);
            if ($cConfig->SaveConfigFile() == false)
                $errorString = get_translation('admin_survey_errorconfig') . "!";
            if ($errorString == "")
            {
                log_message ('User ' . get_user_name() . ' saved survey');
                header('location: ' . $ADMIN_MAIN_SITE);
                return;
            }
        }
    }

    // Display
    print_admin_header(get_translation('admin_survey_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-5', true, get_translation('admin_backtomain'), "", "", false);
    // Enable editor?
    if ($ADMIN_USEWYSIGWYG == true)
        print_wysiwyg_header();
    finish_admin_header(get_translation('admin_survey_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-5', true, get_translation('admin_backtomain'), "", "");

    if ($errorString != "")
        print_error_message ($errorString);
        
    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    // Activate survey page
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_activate') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="survey_Activate" value="1"');
    if ($cConfig->GetParameter("site_show_survey") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Survey inputs
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    // Title
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_title') . ':</td>');
    echo_r ('    <td>');
    if (isset($_POST['survey_Title']) == false)
        $_POST['survey_Title'] = $cConfig->GetParameter("survey_title");
    echo_r ('      <input type="text" name="survey_Title" size="80" value="' . $_POST['survey_Title'] . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    // Question
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_question') . ':</td>');
    echo_r ('    <td>');
    if (isset($_POST['survey_Question']) == false)
        $_POST['survey_Question'] = $cConfig->GetParameter("survey_question");
    echo_r ('      <input type="text" name="survey_Question" size="80" value="' . $_POST['survey_Question'] . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    // Options
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_options') . ':</td>');
    echo_r ('    <td>');
    if (isset($_POST['survey_Options']) == false)
        $_POST['survey_Options'] = $cConfig->GetParameter("survey_options");
    echo_r ('      <input type="text" name="survey_Options" size="80" value="' . $_POST['survey_Options'] . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_optionshorizontal') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="survey_OptionsHorizontal" value="1"');
    if ($cConfig->GetParameter("survey_options_horizontal") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    // Input fields
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_inputfields') . ':</td>');
    echo_r ('    <td>');
    if (isset($_POST['survey_InputFields']) == false)
        $_POST['survey_InputFields'] = $cConfig->GetParameter("survey_inputfields");
    echo_r ('      <input type="text" name="survey_InputFields" size="80" value="' . $_POST['survey_InputFields'] . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

    // Survey page text
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_text') . ':</td>');
    echo_r ('    <td>');
    echo ('      <textarea cols="80" rows="20" id="page_Content" name="survey_Text"');
    if ($ADMIN_WRAPEDITOR == true)
        echo (' wrap="off"');
    echo_r ('>' . $cFile->LoadFromFile('../' . $SURVEYTITLE_CONFIG_FILE) . '</textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Notify via email
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_survey_email') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="survey_SendMail" value="1"');
    if ($cConfig->GetParameter("survey_sendmail") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('    <td>' . get_translation('admin_survey_emailadditional') . ': <input type="text" name="survey_AdditionalRecipients" size="40" value="' . $cConfig->GetParameter("survey_additionalrecipients") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

    print_bottom_links("survey_Save", get_translation('admin_save'),
                       "survey_Cancel", get_translation('admin_cancel'), false);
    echo_r ('</form>');

    print_admin_footer();
?>