<?php

/*  Alternate WebKit
(C) 2011 by Alternate Tools
-----------------------------------------------------

File:         edit_guestbook.php
Description:  Administrative module to edit guestbook entries

*/

    include "../modules/class_guestbookentry.php";
    include "../modules/class_guestbookentrylist.php";
    include "../modules/mod_admin.php";

    $b_ErrorOccured = false;
    $guestId = "";
    $errorString = "";

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

    $guestGuest = new cGuestbookEntry;
    $guestGuest->SetFolder($GUESTBOOK_FOLDER);

    if (isset ($_POST['guest_id']) == true)
        $guestId = $_POST['guest_id'];
    else if (isset ($_GET['id']) == true)
        $guestId = $_GET['id'];
    if (isset ($_POST['guest_pageid']) == true)
        $pageId = $_POST['guest_pageid'];
    else if (isset ($_GET['pageid']) == true)
        $pageId = $_GET['pageid'];

    if ($guestGuest->LoadGuestbookEntryFromId($guestId) == false)
    {
        print_error_message(get_translation('admin_guestedt_errorload') . ', ID: ' . $guestId);
        return;
    }

    // Guestbook entry edit cancelled?
    if (isset ($_POST['guest_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId);
        return;
    }

    // Check for errors and if none, store the guestbook entry values
    if (isset ($_POST['guest_Save']))
    {
        if (isset($_POST['guest_Email']) == false || $_POST['guest_Email'] == "")
        {
            $errorString = get_translation('admin_guestedt_errorinput') . '!';
            $b_ErrorOccured = true;
        }
        if (isset($_POST['guest_Title']) == false || $_POST['guest_Title'] == "")
        {
            $errorString = get_translation('admin_guestedt_errorinput') . '!';
            $b_ErrorOccured = true;
        }
        if (isset($_POST['guest_EnterDate']) == false || $_POST['guest_EnterDate'] == "")
        {
            $errorString = get_translation('admin_guestedt_errorinput') . '!';
            $b_ErrorOccured = true;
        }
        else if (isset($_POST['guest_Name']) == false || $_POST['guest_Name'] == "")
        {
            $errorString = get_translation('admin_guestedt_errorinput') . '!';
            $b_ErrorOccured = true;
        }
        else if (isset($_POST['guest_Text']) == false || $_POST['guest_Text'] == "")
        {
            $errorString = get_translation('admin_guestedt_errorinput') . '!';
            $b_ErrorOccured = true;
        }

        if ($b_ErrorOccured == false)
        {
            $guestGuest->SetEnterDate($_POST['guest_EnterDate']);
            $guestGuest->SetTitle($_POST['guest_Title']);
            $guestGuest->SetName($_POST['guest_Name']);
            $guestGuest->SetEmail($_POST['guest_Email']);
            $guestGuest->SetText($_POST['guest_Text']);
            if ($guestGuest->SaveGuestbookEntry() == false)
            {
                $errorString = get_translation('admin_guestedt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                // Guestbook entry is saved.
                log_message ('User ' . get_user_name() . ' saved guestbook entry ' . $guestGuest->GetId());
                header ('Location: ' . $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId);
                return;
            }
        }
    }

    // Display the input dialog for edit guestbook entries
    print_admin_header(get_translation('admin_guestedt_title'), true, get_translation('admin_login_logout'), false, true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId, 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%">');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_guestbook_id') . ':</td>');
    echo_r('    <td><b>' . $guestGuest->GetId() . '</b>');
    echo_r('      <input type="hidden" value="' .  $guestGuest->GetId() . '" name="guest_id">  ');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_id') . ':</td>');
    echo_r('    <td>' . $guestGuest->GetPageId() . '</td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_guestbook_date') . ':</td>');
    if (isset($_POST['guest_EnterDate']))
        echo_r('    <td><input type="text" name="guest_EnterDate" size="40" value="' . $_POST['guest_EnterDate'] . '"></td>');
    else
        echo_r('    <td><input type="text" name="guest_EnterDate" size="40" value="' .  $guestGuest->GetEnterDate() . '"></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_guestbook_title') . ':</td>');
    if (isset($_POST['guest_Title']))
        echo_r('    <td><input type="text" name="guest_Title" size="40" value="' . $_POST['guest_Title'] . '"></td>');
    else
        echo_r('    <td><input type="text" name="guest_Title" size="40" value="' .  $guestGuest->GetTitle() . '"></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_guestbook_name') . ':</td>');
    if (isset($_POST['guest_Title']))
        echo_r('    <td><input type="text" name="guest_Name" size="40" value="' . $_POST['guest_Name'] . '"></td>');
    else
        echo_r('    <td><input type="text" name="guest_Name" size="40" value="' .  $guestGuest->GetName() . '"></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_guestbook_email') . ':</td>');
    if (isset($_POST['guest_Email']))
        echo_r('    <td><input type="text" name="guest_Email" size="40" value="' . $_POST['guest_Email'] . '"></td>');
    else
        echo_r('    <td><input type="text" name="guest_Email" size="40" value="' .  $guestGuest->GetEmail() . '"></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_guestbook_text') . ':</td>');
    if (isset($_POST['guest_Text']))
        echo_r('    <td><textarea name="guest_Text" cols="80" rows="8">' . $_POST['guest_Text'] . '</textarea></td>');
    else
        echo_r('    <td><textarea name="guest_Text" cols="80" rows="8">' .  $guestGuest->NoHTMLLinefeed($guestGuest->GetText()) . '</textarea></td>');
    echo_r('  </tr>');
    echo_r('</table>');
    print_bottom_links("guest_Save", get_translation('admin_save'),
                       "guest_Cancel", get_translation('admin_cancel'), false);
    echo_r('<input type="hidden" name="guest_pageid" value="' . $pageId . '">');
    echo_r('</form>');

    print_admin_footer();
?>