<?php

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

File:         edit_form.php
Description:  Administrative module to edit forms

*/

    include "../modules/class_form.php";
    include "../modules/class_page.php";
    include "../modules/mod_admin.php";

    $b_ErrorOccured = false;
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Load the form
    $pagFolder = $PAGES_FOLDER;
    $frmFolder = $FORM_FOLDER;

    $pagPage = new cPage;
    $pagPage->SetFolder($pagFolder);
    $frmForm = new cForm;
    $frmForm->SetFolder($frmFolder);

    if (isset ($_POST['frm_id']) == true)
        $frmId = $_POST['frm_id'];
    else if (isset ($_GET['id']) == true)
        $frmId = $_GET['id'];
    // Load hosting page
    if ($pagPage->LoadPageFromId($frmId) == false)
    {
        print_error_message(get_translation('admin_formedt_errorload') . ', ID: ' . $frmId);
        return;
    }
    // Then load form
    $frmId = $pagPage->m_cForm->GetId();
    if ($frmForm->LoadFormFromId($frmId) == false)
    {
        print_error_message(get_translation('admin_formedt_errorload') . ', ID: ' . $frmId);
        return;
    }

    // Form edit cancelled?
    if (isset ($_POST['frm_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
        return;
    }

    // Check for errors and if none, store the form values
    if (isset ($_POST['frm_Save']))
    {
        for ($i = 0; $i < $frmForm->GetMaxFields(); $i ++)
        {
             $frmForm->SetDescription($i, $_POST['frm_Description' . $i]);
             $frmForm->SetType($i, $_POST['frm_Type' . $i]);
             $frmForm->SetValue($i, $_POST['frm_Value' . $i]);
             $frmForm->SetSize($i, $_POST['frm_Size' . $i]);
             $frmForm->SetLength($i, $_POST['frm_Length' . $i]);
             $frmForm->SetMandatory($i, isset($_POST['frm_Mandatory' . $i]));
        }
        $frmForm->SetAdditionalEMail($_POST['frm_AdditionalEmail']);
        if ($frmForm->SaveForm() == false)
        {
            $errorString = get_translation('admin_formedt_errorsave') . '!';
            $b_ErrorOccured = true;
        }
        else
        {
            // Form is saved.
            log_message ('User ' . get_user_name() . ' saved form ' . $frmForm->GetId());
            header ('Location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
            return;
        }
    }

    // Display the input dialog for edit link list
    print_admin_header(get_translation('admin_formedt_head'), true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGECONTENTS_SITE, get_translation('admin_overview'), false);
    echo_r('<script language="JavaScript" type="Text/JavaScript">');
    echo_r('function changeFieldType(fieldType, index)');
    echo_r('{');
    echo_r('  document.getElementById(\'fieldValue\' + index).style.display = \'none\';');
    echo_r('  if (fieldType == ' . FORMTYPE_RADIO .  ' || fieldType == ' . FORMTYPE_COMBOBOX . ')');
    echo_r('    document.getElementById(\'fieldValue\' + index).style.display = \'\';');
    echo_r('  document.getElementById(\'fieldSize\' + index).style.display = \'none\';');
    echo_r('  if (fieldType == ' . FORMTYPE_INPUT .  ' || fieldType == ' . FORMTYPE_TEXTINPUT . ')');
    echo_r('    document.getElementById(\'fieldSize\' + index).style.display = \'\';');
    echo_r('  document.getElementById(\'fieldLength\' + index).style.display = \'none\';');
    echo_r('  if (fieldType == ' . FORMTYPE_INPUT .  ' || fieldType == ' . FORMTYPE_TEXTINPUT . ')');
    echo_r('    document.getElementById(\'fieldLength\' + index).style.display = \'\';');
    echo_r('}');
    echo_r('</script>');
    finish_admin_header(get_translation('admin_formedt_head'), true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGES_SITE, get_translation('admin_overview'));
    if ($errorString !="")
        print_error_message($errorString);

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');

    echo_r('<table width="100%">');
    // Ids
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_id') . ':</td>');
    echo_r('    <td>' . $pagPage->GetId());
    echo_r('      <input type="hidden" value="' .  $pagPage->GetId() . '" name="frm_id">  ');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_title') . ':</td>');
    echo_r('    <td><b>' . $pagPage->GetTitle() . '</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_form_id') . ':</td>');
    echo_r('    <td><b>' . $frmForm->GetId() . '</b>');
    echo_r('    </td>');
    echo_r('  </tr>');
    // Fields
    for ($i = 0; $i < $frmForm->GetMaxFields(); $i ++)
    {
        echo_r('  <tr>');
        echo_r('  <td colspan="2"><hr></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_form_field') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td></td>');
        echo_r('  </tr>');
        // Description
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_form_description') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="frm_Description' . $i . '" value="' . $frmForm->GetDescription($i) . '" size="65"></td>');
        echo_r('  </tr>');
        // Type
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_form_type') . ' ' . ($i + 1) . ':</td>');
        if (isset($_POST['frm_Type' . $i]) == true)
            $frmType = $_POST['frm_Type' . $i];
        else
            $frmType = $frmForm->GetType($i);
        echo_r ('      <td><select id="fieldType' . $i . '" name="frm_Type' . $i . '" onChange="changeFieldType(document.getElementById(\'fieldType' . $i .'\').value, ' . $i . ');">');
        echo ('      <option value="' . FORMTYPE_INPUT . '"');
        if ($frmType == FORMTYPE_INPUT)
            echo (' selected');
        echo_r('>' . get_translation('admin_form_typeinput') . '</option>');
        echo ('      <option value="' . FORMTYPE_TEXTINPUT . '"');
        if ($frmType == FORMTYPE_TEXTINPUT)
            echo (' selected');
        echo_r('>' . get_translation('admin_form_typetextinput') . '</option>');
        echo ('      <option value="' . FORMTYPE_CHECKBOX . '"');
        if ($frmType == FORMTYPE_CHECKBOX)
            echo (' selected');
        echo_r('>' . get_translation('admin_form_typecheckbox') . '</option>');
        echo ('      <option value="' . FORMTYPE_RADIO . '"');
        if ($frmType == FORMTYPE_RADIO)
            echo (' selected');
        echo_r('>' . get_translation('admin_form_typeradio') . '</option>');
        echo ('      <option value="' . FORMTYPE_COMBOBOX . '"');
        if ($frmType == FORMTYPE_COMBOBOX)
            echo (' selected');
        echo_r('>' . get_translation('admin_form_typecombobox') . '</option>');
        echo_r('      </select></td>');
        echo_r('  </tr>');
        // Value(s)
        echo  ('  <tr id="fieldValue' . $i . '"');
        if ($frmType != FORMTYPE_RADIO && $frmType != FORMTYPE_COMBOBOX)
            echo (' style="display:none"');
        echo_r('>');
        echo_r('    <td width="30%">' . get_translation('admin_form_value') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="frm_Value' . $i . '" value="' . $frmForm->GetValue($i) . '" size="55"></td>');
        echo_r('  </tr>');
        // Size
        echo  ('  <tr id="fieldSize' . $i . '"');
        if ($frmType != FORMTYPE_INPUT && $frmType != FORMTYPE_TEXTINPUT)
            echo (' style="display:none"');
        echo_r('>');
        echo_r('    <td width="30%">' . get_translation('admin_form_size') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="frm_Size' . $i . '" value="' . $frmForm->GetSize($i) . '" size="5"></td>');
        echo_r('  </tr>');
        // Length
        echo  ('  <tr id="fieldLength' . $i . '"');
        if ($frmType != FORMTYPE_INPUT && $frmType != FORMTYPE_TEXTINPUT)
            echo (' style="display:none"');
        echo_r('>');
        echo_r('    <td width="30%">' . get_translation('admin_form_length') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="frm_Length' . $i . '" value="' . $frmForm->GetLength($i) . '" size="5"></td>');
        echo_r('  </tr>');
        // Mandatory
        echo_r('  <tr>');
        if (isset($_POST['frm_Mandatory' . $i]) == true)
            $frmMandatory = isset($_POST['frm_Mandatory' . $i]);
        else
            $frmMandatory = $frmForm->GetMandatory($i);
        echo_r('    <td width="30%">' . get_translation('admin_form_mandatory') . ' ' . ($i + 1) . ':</td>');
            echo('    <td><input type="checkbox" name="frm_Mandatory' .$i . '" value="1"');
        if ($frmMandatory == true)
            echo(' checked');
        echo_r('></td>');
        echo_r('  </tr>');
    }
    // Additional recipient
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_form_emailadditional') . ' :</td>');
    echo_r('    <td><input type="text" name="frm_AdditionalEmail" value="' . $frmForm->GetAdditionalEMail() . '" size="65"></td>');
    echo_r('  </tr>');
    echo_r('</table>');

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

    print_admin_footer();
?>