<?php

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

File:         modify_pageimages.php
Description:  Administrative module to unlock/lock/delete a pageimage

*/

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

    $bPageImageLoadFailed = false;
    $pgiId = "";
    $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
    $pgiModAction = "unlock";
    if (isset ($_POST['pgi_action']) == true)
    {
        if ($_POST['pgi_action'] == "lock")
            $pgiModAction = "lock";
        else if ($_POST['pgi_action'] == "delete")
            $pgiModAction = "delete";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "lock")
            $pgiModAction = "lock";
        else if ($_GET['action'] == "delete")
            $pgiModAction = "delete";
    }

    $pgiPageImage = new cPageImage;
    $pgiPageImage->SetFolder($PAGEIMAGES_FOLDER);

    if (isset ($_POST['pgi_id']) == true)
        $pgiId = $_POST['pgi_id'];
    else if (isset ($_GET['id']) == true)
        $pgiId = $_GET['id'];

    if ($pgiPageImage->LoadPageImageFromId($pgiId) == false)
    {
        print_error_message(get_translation('admin_pageimgmod_errorload') . ', ID: ' . $pgiId . '!!');
        $bPageImageLoadFailed = true;
    }

    // Cancel modification?
    if (isset ($_POST['pgi_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGEIMAGES_SITE);
        return;
    }
    // Modify download?
    if (isset ($_POST['pgi_DoAction']) && $bPageImageLoadFailed == false)
    {
        if ($pgiModAction == "lock")
        {
            if ($pgiPageImage->SetLocked(true) == false)
                $errorString = get_translation('admin_pageimgmod_errorlock') . ', ID: ' . $pgiId . '!!';
            else
            {
                // Update the imagelist .js-file for editor
                generate_pageimage_list();
                log_message ('User ' . get_user_name() . ' locked pageimage ' . $pgiPageImage->GetId());
                header('location: ' . $ADMIN_LIST_PAGEIMAGES_SITE);
                return;
            }
        }
        else if ($pgiModAction == "delete")
        {
            $bDone = $pgiPageImage->DeletePageImageFinal();  // Page images can be unlinked
            if ($bDone == false)
                $errorString = get_translation('admin_pageimgmod_errordelete') . ', ID: ' . $pgiId . '!!';
            else
            {
                // Update the imagelist .js-file for editor
                generate_pageimage_list();
                log_message ('User ' . get_user_name() . ' deleted pageimage ' . $pgiPageImage->GetId());
                header('location: ' . $ADMIN_LIST_PAGEIMAGES_SITE);
                return;
            }
        }
        else
        {
            if ($pgiPageImage->SetLocked(false) == false)
                $errorString = get_translation('admin_pageimgmod_errorunlock') . ', ID: ' . $pgiId . '!!';
            else
            {
                // Update the imagelist .js-file for editor
                generate_pageimage_list();
                log_message ('User ' . get_user_name() . ' unlocked pageimage ' . $pgiPageImage->GetId());
                header('location: ' . $ADMIN_LIST_PAGEIMAGES_SITE);
                return;
            }
        }
    }

    // Display
    $titleString = get_translation('admin_pageimgmod_unlockhead');
    if ($pgiModAction == "lock")
        $titleString = get_translation('admin_pageimgmod_lockhead');
    else if ($pgiModAction == "delete")
        $titleString = get_translation('admin_pageimgmod_deletehead');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGEIMAGES_SITE, get_translation('admin_overview'));

    if ($errorString != "")
        print_error_message($errorString);
        
    if ($pgiModAction == "lock")
        echo_r('<p>' . get_translation('admin_pageimgmod_questlock') . ':</p><br>');
    else if ($pgiModAction == "delete")
        echo_r('<p>' . get_translation('admin_pageimgmod_questdelete') . ':</p><br>');
    else
        echo_r('<p>' . get_translation('admin_pageimgmod_questunlock') . ':</p><br>');
    echo_r('<table width="100%" border="0">');
    echo_r('  <tr>');
    echo_r('    <td width="20%">');
    echo_r('       <a href="' . $pgiPageImage->GetImageFile() . '" target="_blank">' . get_imagezoomed($pgiPageImage->GetImageFile(), $pgiPageImage->GetWidth(), $pgiPageImage->GetHeight(), $PAGEIMAGES_PREVIEW_WIDTH, $PAGEIMAGES_PREVIEW_WIDTH, "", "") . '</a>');
    echo_r('    </td>');
    echo_r('    <td width="80%">');
    echo_r('       <b>' . get_translation('admin_pageimg_title') . ': ' . $pgiPageImage->GetTitle() . '</b><br>');
    echo_r('       ' . get_translation('admin_pageimg_orginname') . ': ' . $pgiPageImage->GetOriginName() . '<br>');
    echo_r('       ' . get_translation('admin_pageimg_id') . ': ' . $pgiPageImage->GetId() . '<br>');
    echo_r('       ' . get_translation('admin_pageimg_created') . ': ' . $pgiPageImage->GetDate() . '<br>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    
    echo_r('<form method="post" action="">');
    print_bottom_links("pgi_DoAction", get_translation('admin_yes'),
                       "pgi_Cancel", get_translation('admin_cancel'), false);
    echo_r('  <input type="hidden" value="' .  $pgiId . '" name="pgi_id">  ');
    echo_r('  <input type="hidden" value="' .  $pgiModAction . '" name="pgi_action">  ');
    echo_r('</form>');

    print_admin_footer();
?>