<?php

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

File:         edit_pageimages.php
Description:  Administrative module to edit pageimages or create new ones

*/

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

    $b_NotFinished = true;
    $b_ErrorOccured = 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;
    }

    // Check action
    $pgiEdtAction = "new";
    if (isset ($_POST['pgi_EdtAction']) == true)
    {
        if ($_POST['pgi_EdtAction'] == "edit")
            $pgiEdtAction = "edit";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "edit")
            $pgiEdtAction = "edit";
    }

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

    if ($pgiEdtAction == "edit")  // Edit -> load it
    {
        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_pageimgedt_errorload') . ', ID: ' . $pgiId);
            return;
        }
    }

    // Pageimage edit cancelled?
    if (isset ($_POST['pgi_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGEIMAGES_SITE);
        return;
    }

    // Check for errors and if none, store the pageimages values
    if (isset ($_POST['pgi_Save']))
    {
        if (isset($_POST['pgi_Title']) == false || $_POST['pgi_Title'] == "")
        {
            $errorString = get_translation('admin_pageimgedt_errortitle') . '!';
            $b_ErrorOccured = true;
        }
        else if (isset($_POST['pgi_Description']) == false || $_POST['pgi_Description'] == "")
        {
            $errorString = get_translation('admin_pageimgedt_errordescription') . '!';
            $b_ErrorOccured = true;
        }
        // Uploads
        if ($b_ErrorOccured == false)
        {
            if ($pgiEdtAction == "new")    // New -> create Id first
            {
                $pgiPageImage->CreatePageImage(get_user_name());
            }

            // Image file
            if ($b_ErrorOccured == false && ($pgiEdtAction == "new" || !(empty ($_FILES['pgi_ImageFile']['name']))))
            {
                // Check extension
                $fileExt = strtolower(retrieve_extension($_FILES['pgi_ImageFile']['name']));
                if (check_image_extension($_FILES['pgi_ImageFile']['name']) == false)
                {
                    $errorString = get_translation('admin_pageimgedt_errortype') . '! : ' . $fileExt;
                    $b_ErrorOccured = true;
                }
                else
                {
                    if ($pgiEdtAction == "edit")
                        unlink($pgiPageImage->GetImageFile());  // delete old one first

                    $destName = $PAGEIMAGES_FOLDER . $pgiPageImage->GetId() . $fileExt;
//                    if (@ copy ($_FILES['pgi_ImageFile']['tmp_name'], $destName) == false)
                    if (resize_image($_FILES['pgi_ImageFile']['name'],
                                     $_FILES['pgi_ImageFile']['tmp_name'],
                                     $destName,
                                     $SITE_MAXIMGWIDTH, $SITE_MAXIMGHEIGHT,
                                     ($SITE_IMAGERESTRICTION != true), false, false, $SITE_IMAGEBGCOLOR) == false)
                    {
                        $errorString = get_translation('admin_pageimgedt_errorupload') . ': ' . $_FILES['pgi_ImageFile']['name'] . ', ' . $destName. '!';
                        $b_ErrorOccured = true;
                    }
                    else
                    {
                        $sizes = getimagesize($destName);
                        $pgiPageImage->SetWidth($sizes[0]);
                        $pgiPageImage->SetHeight($sizes[1]);
                        $pgiPageImage->SetImageFile($destName);
                        $pgiPageImage->SetOriginName($_FILES['pgi_ImageFile']['name']);
                    }
                }
            }
        }

        if ($b_ErrorOccured == false)
        {
            // If done, fill the rest of the parameters
            $pgiPageImage->SetFolder($PAGEIMAGES_FOLDER);
            $pgiPageImage->SetTitle($_POST['pgi_Title']);
            $pgiPageImage->SetDescription($_POST['pgi_Description']);
            if ($pgiPageImage->SavePageImage(get_user_name()) == false)
            {
                if ($pgiEdtAction == "new")
                    $errorString = get_translation('admin_pageimgedt_errorcreate') . '!';
                else
                    $errorString = get_translation('admin_pageimgedt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                // Update the imagelist .js-file for editor
                generate_pageimage_list();
                if ($pgiEdtAction == "edit")
                {
                    // Pageimage is saved.
                    log_message ('User ' . get_user_name() . ' saved pageimage ' . $pgiPageImage->GetId());
                    header ('Location: ' . $ADMIN_LIST_PAGEIMAGES_SITE);
                    return;
                }
                else
                    log_message ('User ' . get_user_name() . ' created pageimage ' . $pgiPageImage->GetId());

                $b_NotFinished = false;
            }
        }
    }

    // Display the input dialog for edit pageimage
    $titleString = get_translation('admin_pageimgedt_newhead');
    if ($pgiEdtAction == "edit")  // Edit
        $titleString = get_translation('admin_pageimgedt_edithead');
    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 ($b_NotFinished == false)
    {
        // Pageimage is created. Display form to create another or go back to list
        echo_r('<p>' . get_translation('admin_pageimgedt_imagecreated') . ', ID : ' . $pgiPageImage->GetId() . '.<br></p>');
        print_bottom_links("pgi_New", get_translation('admin_pageimgedt_createanother'),
                           "pgi_Cancel", get_translation('admin_ok'), true);
        return;
    }

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    if ($b_NotFinished == true)
    {
        echo_r('<table width="100%">');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_maxuploadsize') . ':</td>');
        echo_r('    <td><b>' . ini_get("upload_max_filesize") . '</b></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_sizerestriction') . ':</td>');
        if ($cConfig->GetParameter("site_imagerestriction") == "1")
            echo_r('    <td><b>' . $cConfig->GetParameter("site_maximgwidth") . ' x ' . $cConfig->GetParameter("site_maximgheight") . ' ' . get_translation('admin_sizerestrictionnpixels') . '</b></td>');
        else
            echo_r('    <td><b>' . get_translation('admin_sizerestrictionnone') . '</b></td>');
        echo_r('  </tr>');
        if ($pgiEdtAction == "edit")  // Edit
        {
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_pageimg_id') . ':</td>');
            echo_r('    <td><b>' . $pgiPageImage->GetId() . '</b>');
            echo_r('      <input type="hidden" value="' .  $pgiPageImage->GetId() . '" name="pgi_id">  ');
            echo_r('    </td>');
            echo_r('  </tr>');
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_pageimg_created') . ':</td>');
            echo_r('    <td><b>' . $pgiPageImage->GetDate() . ' (' . $pgiPageImage->GetCreatedBy() . ')</b></td>');
            echo_r('  </tr>');
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_pageimg_modified') . ':</td>');
            echo_r('    <td><b>' . $pgiPageImage->GetModified() . ' (' . $pgiPageImage->GetAuthor() . ')</b></td>');
            echo_r('  </tr>');
        }
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_pageimg_title') . ':</td>');
        if (isset($_POST['pgi_Title']))
            echo_r('    <td><input type="text" name="pgi_Title" size="60" value="' . $_POST['pgi_Title'] . '"></td>');
        else
            echo_r('    <td><input type="text" name="pgi_Title" size="60" value="' .  $pgiPageImage->GetTitle() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_pageimg_description') . ':</td>');
        if (isset($_POST['pgi_Description']))
            echo_r('    <td><input type="text" name="pgi_Description" size="80" value="' . $_POST['pgi_Description'] . '"></td>');
        else
            echo_r('    <td><input type="text" name="pgi_Description" size="80" value="' .  $pgiPageImage->GetDescription() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo  ('    <td width="30%">' . get_translation('admin_pageimg_filename') . ':</td>');
        echo  ('    <td>');
        if ($pgiEdtAction == "edit")  // Edit
        {
            echo ('<input type="text" size="60" value="' . $pgiPageImage->GetImageFile() . '" readonly><br>');
            echo ('<a href="' . $pgiPageImage->GetImageFile() . '" target="_blank">' . get_imagezoomed($pgiPageImage->GetImageFile(), $pgiPageImage->GetWidth(), $pgiPageImage->GetHeight(), $PAGEIMAGES_PREVIEW_WIDTH, $PAGEIMAGES_PREVIEW_WIDTH, "", "") . '</a><br>');
        }
        echo_r('<input type="file" name="pgi_ImageFile" size="60" value=""></td>');
        echo_r('  </tr>');
        echo_r('</table>');

        if ($pgiEdtAction == "edit")  // Edit
            print_bottom_links("pgi_Save", get_translation('admin_save'),
                               "pgi_Cancel", get_translation('admin_cancel'), false);
        else  // New
            print_bottom_links("pgi_Save", get_translation('admin_create'),
                               "pgi_Cancel", get_translation('admin_cancel'), false);

    }
    echo_r('<input type="hidden" name="pgi_EdtAction" value="' . $pgiEdtAction . '">');
    echo_r('</form>');

    print_admin_footer();
?>