<?php

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

File:         modify_guestbook.php
Description:  Administrative module to unlock/lock/delete a guestbook entry

*/

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

    $bGuestbookEntryLoadFailed = false;
    $guestId = "";
    $errorString = "";
    
    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Get action
    $guestModAction = "unlock";
    if (isset ($_POST['guest_action']) == true)
    {
        if ($_POST['guest_action'] == "lock")
            $guestModAction = "lock";
        else if ($_POST['guest_action'] == "delete")
            $guestModAction = "delete";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "lock")
            $guestModAction = "lock";
        else if ($_GET['action'] == "delete")
            $guestModAction = "delete";
    }

    $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_guestmod_errorload') . ': ' . $guestId . '!!');
        $bGuestbookEntryLoadFailed = true;
    }

    // Cancel modification?
    if (isset ($_POST['guest_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId);
        return;
    }
    // Modify guestbook entry?
    if (isset ($_POST['guest_DoAction']) && $bGuestbookEntryLoadFailed == false)
    {
        if ($guestModAction == "lock")
        {
            if ($guestGuest->SetLocked(true) == false)
                $errorString = get_translation('admin_guestmod_errorlock') . ': ' . $guestId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' locked guestbook entry ' . $guestGuest->GetId());
                header('location: ' . $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId);
                return;
            }
        }
        else if ($guestModAction == "delete")
        {
            $bDone = $guestGuest->DeleteGuestbookEntryFinal();  // Guestbook entries can be unlinked
            if ($bDone == false)
                $errorString = get_translation('admin_guestmod_errordelete') . ': ' . $guestId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' deleted guestbook entry ' . $guestGuest->GetId());
                header('location: ' . $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId);
                return;
            }
        }
        else
        {
            if ($guestGuest->SetLocked(false) == false)
                $errorString = get_translation('admin_guestmod_errorunlock') . ': ' . $guestId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' unlocked guestbook entry ' . $guestGuest->GetId());
                header('location: ' . $ADMIN_LIST_GUESTBOOK_SITE . '?id=' . $pageId);
                return;
            }
        }
    }

    // Display
    $titleString = get_translation('admin_guestmod_unlockhead');
    if ($guestModAction == "lock")
        $titleString = get_translation('admin_guestmod_lockhead');
    else if ($guestModAction == "delete")
        $titleString = get_translation('admin_guestmod_deletehead');
    print_admin_header($titleString, 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);
        
    if ($guestModAction == "lock")
        echo_r('<p>' . get_translation('admin_guestmod_questlock') . ':</p><br>');
    else if ($guestModAction == "delete")
        echo_r('<p>' . get_translation('admin_guestmod_questdelete') . ':</p><br>');
    else
        echo_r('<p>' . get_translation('admin_guestmod_questunlock') . ':</p><br>');
    echo_r('<table width="100%" border="0">');
    echo_r('  <tr>');
    echo_r('    <td width="10%">');
    echo_r('       <img src="' . $PAGE_GUESTBOOK_IMAGE . '">');
    echo_r('    </td>');
    echo_r('    <td width="90%">');
    echo_r('       ' . get_translation('admin_guestbook_id') . ': <b>' . $guestGuest->GetId() . '</b><br>');
    echo_r('       ' . get_translation('admin_guestbook_date') . ': ' . $guestGuest->GetEnterDate() . '<br>');
    echo_r('       ' . get_translation('admin_guestbook_title') . ': ' . $guestGuest->GetTitle() . '<br>');
    echo_r('       ' . get_translation('admin_guestbook_name') . ': ' . $guestGuest->GetName() . '<br>');
    echo_r('       ' . get_translation('admin_guestbook_email') . ': ' . $guestGuest->GetEmail() . '<br>');
    echo_r('       ' . get_translation('admin_guestbook_text') . ': ' . $guestGuest->GetText() . '<br>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    
    echo_r('<form method="post" action="">');
    print_bottom_links("guest_DoAction", get_translation('admin_yes'),
                       "guest_Cancel", get_translation('admin_cancel'), false);
    echo_r('  <input type="hidden" value="' .  $guestId . '" name="guest_id">  ');
    echo_r('  <input type="hidden" value="' .  $guestModAction . '" name="guest_action">  ');
    echo_r('  <input type="hidden" name="guest_pageid" value="' . $pageId . '">');
    echo_r('</form>');

    print_admin_footer();
?>