<?php

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

File:         modify_page.php
Description:  Administrative module to unlock/lock/delete a page

*/

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

    $bPageLoadFailed = false;
    $pageId = "";
    $errorString = "";
    
    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_reporter() == true)
    {
        header("location: " . $ADMIN_MAIN_SITE);
        return;
    }

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

    $pagePage = new cPage;
    $pagePage->SetFolder($PAGES_FOLDER);

    if (isset ($_POST['page_id']) == true)
        $pageId = $_POST['page_id'];
    else if (isset ($_GET['id']) == true)
        $pageId = $_GET['id'];

    if ($pagePage->LoadPageFromId($pageId) == false)
    {
        print_error_message(get_translation('admin_pagemod_errorload') . ', ID: ' . $pageId . '!!');
        $bPageLoadFailed = true;
    }

    // Cancel modification?
    if (isset ($_POST['page_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGES_SITE);
        return;
    }
    // Modify page?
    if (isset ($_POST['page_DoAction']) && $bPageLoadFailed == false)
    {
        if ($pageModAction == "lock")
        {
            if ($pagePage->SetLocked(true, get_user_name()) == false)
                $errorString = get_translation('admin_pagemod_errorlock') . ', ID: ' . $pageId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' locked page ' . $pagePage->GetId());
                header('location: ' . $ADMIN_LIST_PAGES_SITE);
                return;
            }
        }
        else if ($pageModAction == "delete")
        {
            $bDone = $pagePage->DeletePageFinal();  // News can be unlinked
            if ($bDone == false)
                $errorString = get_translation('admin_pagemod_errordelete') . ', ID: ' . $pageId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' deleted page ' . $pagePage->GetId());
                header('location: ' . $ADMIN_LIST_PAGES_SITE);
                return;
            }
        }
        else
        {
            if ($pagePage->SetLocked(false, get_user_name()) == false)
                $errorString = get_translation('admin_pagemod_errorunlock') . ', ID: ' . $pageId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' unlocked page ' . $pagePage->GetId());
                header('location: ' . $ADMIN_LIST_PAGES_SITE);
                return;
            }
        }
    }

    // Display
    $titleString = get_translation('admin_pagemod_unlockhead');
    if ($pageModAction == "lock")
        $titleString = get_translation('admin_pagemod_lockhead');
    else if ($pageModAction == "delete")
        $titleString = get_translation('admin_pagemod_deletehead');
    print_admin_header($titleString, 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);
        
    if ($pageModAction == "lock")
        echo_r('<p>' . get_translation('admin_pagemod_questlock') . ':</p><br>');
    else if ($pageModAction == "delete")
        echo_r('<p>' . get_translation('admin_pagemod_questdelete') . ':</p><br>');
    else
        echo_r('<p>' . get_translation('admin_pagemod_questunlock') . ':</p><br>');
    echo_r('<table width="100%" border="0">');
    echo_r('  <tr>');
    echo_r('    <td width="10%">');
    if ($pagePage->GetType() == PAGETYPE_LINK)
        echo_r('       <img src="' . $PAGE_LINK_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_GALLERY)
        echo_r('       <img src="' . $PAGE_GALLERY_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_CONTACT)
        echo_r('       <img src="' . $PAGE_CONTACT_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_YOUTUBEGALLERY)
        echo_r('       <img src="' . $PAGE_YOUTUBEGALLERY_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_LINKS)
        echo_r('       <img src="' . $PAGE_LINKLIST_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_GUESTBOOK)
        echo_r('       <img src="' . $PAGE_GUESTBOOK_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_SITEMAP)
        echo_r('       <img src="' . $PAGE_SITEMAP_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_NEWSLETTER)
        echo_r('       <img src="' . $PAGE_NEWSLETTER_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_SEARCH)
        echo_r('       <img src="' . $PAGE_SEARCH_IMAGE . '">');
    else if ($pagePage->GetType() == PAGETYPE_FORM)
        echo_r('       <img src="' . $PAGE_FORM_IMAGE . '">');
    else
        echo_r('       <img src="' . $PAGE_IMAGE . '">');
    echo_r('    </td>');
    echo_r('    <td width="90%">');
    if ($pagePage->IsMainPage() == true)
        echo_r('       <b><i>' . get_translation('admin_page_title') . ': ' . $pagePage->GetTitle() . '</i></b><br>');
    else
        echo_r('       <b>' . get_translation('admin_page_title') . ': ' . $pagePage->GetTitle() . '</b><br>');
    echo_r('       ' . get_translation('admin_page_navtitle') . ': ' . $pagePage->GetNavigationTitle() . '<br>');
    if ($pagePage->ShowInNavigation() == true)
        echo_r('       ' . get_translation('admin_page_navposition') . ': ' . $pagePage->GetNavigationPosition() . ' [' . $pagePage->GetNavigationLevel() . ']<br>');
    else
        echo_r('       <i>' . get_translation('admin_page_notinnavigation') . '</i><br>');
    echo_r('       ' . get_translation('admin_page_id') . ': ' . $pagePage->GetId() . '<br>');
    echo_r('       ' . get_translation('admin_page_created') . ': ' . $pagePage->GetDate() . '<br>');
    echo_r('       ' . get_translation('admin_page_modified') . ': ' . $pagePage->GetModified() . '<br>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    
    echo_r('<form method="post" action="">');
    print_bottom_links("page_DoAction", get_translation('admin_yes'),
                       "page_Cancel", get_translation('admin_cancel'), false);
    echo_r('  <input type="hidden" value="' .  $pageId . '" name="page_id">  ');
    echo_r('  <input type="hidden" value="' .  $pageModAction . '" name="page_action">  ');
    echo_r('</form>');

    print_admin_footer();
?>