<?php

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

File:         list_guestbook.php
Description:  Administrative overview of all
              guestbook entries

*/

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


    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
    }
    if (isset ($_POST['guestl_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }


    $pageId = "0";
    // Determin pageId
    if (isset($_GET['id']) == true)
        $pageId = $_GET['id'];
    if (isset($_POST['guestl_id']) == true)
        $pageId = $_POST['guestl_id'];
        
    // Edit guestbook entry?
    if (isset ($_POST['guestl_EditGuestbookEntry']))
    {
        header ('Location: ' . $ADMIN_EDIT_GUESTBOOK_SITE . '?id=' . $_POST['guestl_GuestbookEntryId'] . '&pageid=' . $pageId . '&action=edit');
        return;
    }
    // Delete guestbook entry?
    if (isset ($_POST['guestl_DeleteGuestbookEntry']))
    {
        header ('Location: ' . $ADMIN_MODIFY_GUESTBOOK_SITE . '?id=' . $_POST['guestl_GuestbookEntryId'] . '&action=delete&pageid=' . $pageId);
        return;
    }
    // Lock guestbook entry?
    if (isset ($_POST['guestl_LockGuestbookEntry']))
    {
        header ('Location: ' . $ADMIN_MODIFY_GUESTBOOK_SITE . '?id=' . $_POST['guestl_GuestbookEntryId'] . '&action=lock&pageid=' . $pageId);
        return;
    }
    // Unlock guestbook entry?
    if (isset ($_POST['guestl_UnlockGuestbookEntry']))
    {
        header ('Location: ' . $ADMIN_MODIFY_GUESTBOOK_SITE . '?id=' . $_POST['guestl_GuestbookEntryId'] . '&action=unlock&pageid=' . $pageId);
        return;
    }

    // Display
    print_admin_header(get_translation('admin_guestl_title'), true, get_translation('admin_login_logout'), false, true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGECONTENTS_SITE, get_translation('admin_overview'));
    $guest = new cGuestbookEntry;
    $guestList = new cGuestbookEntryList;
    $guestFilter = 0;
    if (isset ($_GET['filter']) && $_GET['filter'] != "")
        $guestFilter = $_GET['filter'];
    else if (isset ($_POST['guestl_filter']) && $_POST['guestl_filter'] != "")
        $guestFilter = $_POST['guestl_filter'];

    $guest->SetFolder($GUESTBOOK_FOLDER);
    $guestList->SetFolder($GUESTBOOK_FOLDER);
    $guestList->SetPageId($pageId);

    $guestList->LoadGuestbookEntryList($guestFilter);
//    $newsList->SortByPubDate(false);
    $list = $guestList->GetGuestbookEntryList();
    rsort($list);

    echo_r ('<form method="post" action="">');
    echo_r ('<table border="0" width="100%">');
    echo_r ('<tr>');
    echo_r ('  <td width="20%"></td>');
    echo_r ('  <td width="10%">' . get_translation('admin_filter') . '</td>');
    echo_r ('  <td>');
    echo_r ('  <select name="guestl_filter" onChange="submit();">');
    echo   ('  <option value="0"');
    if ($guestFilter == 0)
        echo (' selected');
    echo_r ('>' . get_translation('admin_all') . '</option>');
    echo   ('  <option value="1"');
    if ($guestFilter == 1)
        echo (' selected');
    echo_r ('>' . get_translation('admin_unlocked') . '</option>');
    echo   ('  <option value="2"');
    if ($guestFilter == 2)
        echo (' selected');
    echo_r ('>' . get_translation('admin_locked') . '</option>');
    echo_r ('  </select>');
    echo_r ('  </td>');
    echo_r ('</tr>');
    echo_r ('</table>');
    echo_r ('<input type="hidden" name="guestl_id" value="' . $pageId . '">');
    echo_r ('</form>');

    // No entries?
    if (count($list) < 1)
        echo_r ('<p>&nbsp;' . get_translation('admin_noentries') . '</p>');
    foreach ($list as $value)
    {
        $guest->LoadGuestbookEntryFromId($value);

        echo_r('<hr>');
        echo_r('<table width="100%" border="0">');
        echo_r('  <tr>');
        echo_r('    <td width="15%">');
        echo  ('      <a href="' . $ADMIN_EDIT_GUESTBOOK_SITE . '?id=' . $guest->GetId() . '&amp;pageid=' . $pageId . '&amp;action=edit">');
        echo_r('<img src="' . $PAGE_GUESTBOOK_IMAGE . '" title="' . get_translation('admin_edit') . '" alt=""></a>');
        if ($guest->IsLocked() == true)
            echo_r('       <img src="' . $LOCKED_IMAGE . '" title="' . get_translation('admin_locked') . '" alt="">');
        echo_r('    </td>');
        echo_r('    <td width="28%">');
        echo_r('       ' . get_translation('admin_guestbook_id') . ': <b>' . $guest->GetId() . '</b><br>');
        echo_r('       ' . get_translation('admin_page_id') . ': ' . $pageId . '<br>');
        echo_r('       ' . get_translation('admin_guestbook_date') . ': ' . $guest->GetEnterDate());
        echo_r('    </td>');
        echo_r('    <td width="27%">');
        echo_r('       ' . get_translation('admin_guestbook_title') . ': ' . $guest->GetTitle() . '<br>');
        echo_r('       ' . get_translation('admin_guestbook_name') . ': ' . $guest->GetName() . '<br>');
        echo_r('       ' . get_translation('admin_guestbook_email') . ': ' . $guest->GetEmail() . '<br>');
        echo_r('    </td>');
        echo_r('    <td width="30%">');
        echo_r('    <form method="post" action="">');
        echo_r('    <input type="hidden" value="' . $guest->GetId() . '" name="guestl_GuestbookEntryId">  ');
        if (user_is_reporter() == false)
        {
            echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_edit') . '" name="guestl_EditGuestbookEntry">  ');
            if ($guest->IsLocked() == true)
            {
                echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_unlock') . '" name="guestl_UnlockGuestbookEntry">  ');
            }
            else
            {
                echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_lock') . '" name="guestl_LockGuestbookEntry">  ');
            }
        }
        echo_r('    <input type="submit"  class="side_btn" value="' . get_translation('admin_delete') . '" name="guestl_DeleteGuestbookEntry">  ');
        echo_r('    <input type="hidden" name="guestl_id" value="' . $pageId . '">');
        echo_r('    </form>');
        echo_r('    </td>');
        echo_r('  </tr>');
        echo_r('</table>');
    }
    print_bottom_links("guestl_Cancel", get_translation('admin_ok'));

    print_admin_footer();
?>