<?php

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

File:         edit_gallery.php
Description:  Administrative module to edit galleries

*/

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

    $b_ErrorOccured = false;
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Load the gallery
    $pagFolder = $PAGES_FOLDER;
    $galFolder = $GALLERIES_FOLDER;

    $pagPage = new cPage;
    $pagPage->SetFolder($pagFolder);
    $galGallery = new cGallery;
    $galGallery->SetFolder($galFolder);

    if (isset ($_POST['gal_id']) == true)
        $galId = $_POST['gal_id'];
    else if (isset ($_GET['id']) == true)
        $galId = $_GET['id'];
    // Load hosting page
    if ($pagPage->LoadPageFromId($galId) == false)
    {
        print_error_message(get_translation('admin_galleryedt_errorload') . ', ID: ' . $galId);
        return;
    }
    // Then load gallery
    $galId = $pagPage->m_cGallery->GetId();
    if ($galGallery->LoadGalleryFromId($galId) == false)
    {
        print_error_message(get_translation('admin_galleryedt_errorload') . ', ID: ' . $galId);
        return;
    }

    // Gallery edit cancelled?
    if (isset ($_POST['gal_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
        return;
    }

    // Check for errors and if none, store the gallery values
    if (isset ($_POST['gal_Save']))
    {
        // Try to do the image uploads
        for ($i = 0; $i < $galGallery->GetMaxImages(); $i ++)
        {
            if ($b_ErrorOccured == false)
            {
                // Delete image?
                if (isset($_POST['gal_Delete' . $i]) == true && $_POST['gal_Delete' . $i] == "1")
                    $galGallery->DeleteImage($i);
                // only first image is mandatory
                if (empty ($_FILES['gal_Image' . $i]['name']) == true)
                    continue;
            }
            if ($b_ErrorOccured == false && !(empty ($_FILES['gal_Image' . $i]['name'])))
            {
                // Check main image
                if (check_image_extension($_FILES['gal_Image' . $i]['name']) == false)  // Check extension
                {
                    $errorString = get_translation('admin_galleryedt_errortype') . ': ' .$_FILES['gal_Image' . $i]['name'];
                    $b_ErrorOccured = true;
                }
                // Check thumbnail, if set...
                if ((!empty ($_FILES['gal_ThumbImage' . $i]['name'])) && $b_ErrorOccured == false)
                {
                    if (check_image_extension($_FILES['gal_ThumbImage' . $i]['name']) == false)  // Check extension
                    {
                        $errorString = get_translation('admin_galleryedt_errortype') . ': ' .$_FILES['gal_ThumbImage' . $i]['name'];
                        $b_ErrorOccured = true;
                    }
                }
                if ($b_ErrorOccured == false)
                {
                    $sizeNoResize = false;
                    $sizeForce = false;
                    $sizeExactly = true;
                    $sizePortrait = false;
                    if ($pagPage->GetAdditionalParameter3() == $GALLERYIMAGES_SIZE_NOAUTO)
                    {
                        $sizeNoResize = true;
                    }
                    else if ($pagPage->GetAdditionalParameter3() == $GALLERYIMAGES_SIZE_PORTRAIT)
                    {
                        $sizePortrait = true;
                    }
                    
                    if (file_exists($galGallery->GetImage($i)) == true)
                    {
                        unlink($galGallery->GetImage($i));  // delete old one first
                    }
                    if (file_exists($galGallery->GetThumb($i)) == true)    // Thumbnail image
                    {
                        unlink($galGallery->GetThumb($i));  // delete old one first
                    }
                    $destName = $galFolder . $galGallery->GetId() . '_' . $i . retrieve_extension($_FILES['gal_Image' . $i]['name']);
                    // Is resize necessary? ...otherwise copy
                    if(resize_image($_FILES['gal_Image' . $i]['name'], $_FILES['gal_Image' . $i]['tmp_name'], $destName, $pagPage->GetAdditionalImgWidth(), $pagPage->GetAdditionalImgHeight(), $sizeNoResize, $sizeForce, $sizeExactly, $SITE_IMAGEBGCOLOR, $sizePortrait) == false)
                    {
                        $errorString = get_translation('admin_galleryedt_errorimage') . $_FILES['gal_Image' . $i]['tmp_name'] . '!';
                        $b_ErrorOccured = true;
                    }
                    else
                    {
                        $galGallery->SetImage($i, $destName);
                        // Thumbnail image, if set use it, otherwise use main image, too
                        if (!empty ($_FILES['gal_ThumbImage' . $i]['name']))
                        {
                            $fileName = $_FILES['gal_ThumbImage' . $i]['name'];
                            $tempName = $_FILES['gal_ThumbImage' . $i]['tmp_name'];
                        }
                        else
                        {
                            $fileName = $_FILES['gal_Image' . $i]['name'];
                            $tempName = $_FILES['gal_Image' . $i]['tmp_name'];
                        }
                        $destName = $galFolder . $galGallery->GetId() . '_thumb' . $i . retrieve_extension($fileName);
                        if(resize_image($fileName, $tempName, $destName, $pagPage->GetAdditionalThumbWidth(), $pagPage->GetAdditionalThumbHeight(), $sizeNoResize, $sizeForce, $sizeExactly, $SITE_IMAGEBGCOLOR, $sizePortrait) == false)
                        {
                            $errorString = get_translation('admin_galleryedt_errorimage') . $tempName . '!';
                            $b_ErrorOccured = true;
                        }
                        else
                            $galGallery->SetThumb($i, $destName);
                    }
                }
            }
        }
        // All done? Then fill the descriptions and finally store it
        if ($b_ErrorOccured == false)
        {
            for ($i = 0; $i < $galGallery->GetMaxImages(); $i ++)
            {
                $galGallery->SetDescription($i, $_POST['gal_Description' . $i]);
                $galGallery->SetLink($i, $_POST['gal_Link' . $i]);
            }
            $galGallery->SetSortOrder($_POST['gal_Sort']);
            $galGallery->SetShowDescriptions($_POST['gal_ShowDescriptions']);
            $galGallery->SetEnablePreview($_POST['gal_EnablePreview']);
            $galGallery->SetOpenInNewWindow($_POST['gal_OpenInNewWindow']);
            $galGallery->SetPreviewType($_POST['gal_PreviewType']);
            if ($galGallery->SaveGallery() == false)
            {
                $errorString = get_translation('admin_galleryedt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                // Gallery is saved.
                log_message ('User ' . get_user_name() . ' saved gallery ' . $galGallery->GetId());
                header ('Location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
                return;
            }
        }
    }

    // Display the input dialog for edit gallery
    print_admin_header(get_translation('admin_galleryedt_head'), true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGECONTENTS_SITE, get_translation('admin_overview'));
    if ($errorString != "")
        print_error_message($errorString);

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');

    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>');
    // Ids
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_id') . ':</td>');
    echo_r('    <td>' . $pagPage->GetId());
    echo_r('      <input type="hidden" value="' .  $pagPage->GetId() . '" name="gal_id">  ');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_title') . ':</td>');
    echo_r('    <td><b>' . $pagPage->GetTitle() . '</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_gallery_id') . ':</td>');
    echo_r('    <td><b>' . $galGallery->GetId() . '</b>');
    echo_r('    </td>');
    echo_r('  </tr>');
    // Gallery frontend sort order
    echo_r('    <tr>');
    echo_r('      <td>' . get_translation('admin_sortfrontend') . ':</td>');
    if (isset($_POST['gal_Sort']) == true)
        $galSort = $_POST['gal_Sort'];
    else
        $galSort = $galGallery->GetSortOrder();
    echo_r ('      <td><select name="gal_Sort">');
    echo ('      <option value="1"');
    if ($galSort == "1")
        echo (' selected');
    echo_r('>' . get_translation('admin_sortfrontendnone') . '</option>');
    echo ('      <option value="-1"');
    if ($galSort == "-1")
        echo (' selected');
    echo_r('>' . get_translation('admin_sortfrontendreverse') . '</option>');
    echo ('      <option value="2"');
    if ($galSort == "2")
        echo (' selected');
    echo_r('>' . get_translation('admin_sortfrontenddescriptionasc') . '</option>');
    echo ('      <option value="-2"');
    if ($galSort == "-2")
        echo (' selected');
    echo_r('>' . get_translation('admin_sortfrontenddescriptiondesc') . '</option>');
    echo_r('      </select></td>');
    echo_r('    </tr>');
    // Show descriptions
    echo_r('  <tr>');
    if (isset($_POST['gal_ShowDescriptions']) == true)
        $showDescriptions = isset($_POST['gal_ShowDescriptions']);
    else
        $showDescriptions = $galGallery->ShowDescriptions();
    echo_r('    <td>' . get_translation('admin_showdescriptions') . ':</td>');
        echo('    <td><input type="checkbox" name="gal_ShowDescriptions" value="1"');
    if ($showDescriptions == true)
        echo(' checked');
    echo_r('></td>');
    echo_r('  </tr>');
    // Enable preview
    echo_r('  <tr>');
    if (isset($_POST['gal_EnablePreview']) == true)
        $enablePreview = isset($_POST['gal_EnablePreview']);
    else
        $enablePreview = $galGallery->EnablePreview();
    echo_r('    <td>' . get_translation('admin_galleryedt_enablepreview') . ':</td>');
        echo('    <td><input type="checkbox" name="gal_EnablePreview" value="1"');
    if ($enablePreview == true)
        echo(' checked');
    echo_r('></td>');
    echo_r('  </tr>');
    // Gallery type
    echo_r('    <tr id="gallery_type">');
    echo_r('      <td>' . get_translation('admin_galleryedt_previewtype') . ':</td>');
    if (isset($_POST['gal_PreviewType']) == true)
        $galPreviewType = $_POST['gal_PreviewType'];
    else
        $galPreviewType = $galGallery->GetPreviewType();
    echo_r ('      <td><select name="gal_PreviewType">');
    echo ('      <option value="' . GALLERY_PREVIEW_TYPE_IMAGE . '"');
    if ($galPreviewType == GALLERY_PREVIEW_TYPE_IMAGE)
        echo (' selected');
    echo_r('>' . get_translation('admin_gallery_typeimage') . '</option>');
    echo ('      <option value="' . GALLERY_PREVIEW_TYPE_NEWWINDOW . '"');
    if ($galPreviewType == GALLERY_PREVIEW_TYPE_NEWWINDOW)
        echo (' selected');
    echo_r('>' . get_translation('admin_gallery_typenewwindow') . '</option>');
    echo ('      <option value="' . GALLERY_PREVIEW_TYPE_SAMEWINDOW . '"');  // Lightbox
    if ($galPreviewType == GALLERY_PREVIEW_TYPE_SAMEWINDOW)
        echo (' selected');
    echo_r('>' . get_translation('admin_gallery_typesamewindow') . '</option>');
    echo ('      <option value="' . GALLERY_PREVIEW_TYPE_SAMEWINDOW2 . '"');  // Fancybox
    if ($galPreviewType == GALLERY_PREVIEW_TYPE_SAMEWINDOW2)
        echo (' selected');
    echo_r('>' . get_translation('admin_gallery_typesamewindow') . ' (2)</option>');
    echo ('      <option value="' . GALLERY_PREVIEW_TYPE_SAMEWINDOW3 . '"');  // DaFlashBox
    if ($galPreviewType == GALLERY_PREVIEW_TYPE_SAMEWINDOW3)
        echo (' selected');
    echo_r('>' . get_translation('admin_gallery_typesamewindow') . ' (3)</option>');
    echo_r('      </select></td>');
    echo_r('    </tr>');
    // Open links in new window
    echo_r('  <tr>');
    if (isset($_POST['gal_OpenInNewWindow']) == true)
        $openInNewWindow = isset($_POST['gal_OpenInNewWindow']);
    else
        $openInNewWindow = $galGallery->OpenInNewWindow();
    echo_r('    <td>' . get_translation('admin_galleryedt_openinnewwindow') . ':</td>');
        echo('    <td><input type="checkbox" name="gal_OpenInNewWindow" value="1"');
    if ($openInNewWindow == true)
        echo(' checked');
    echo_r('></td>');
    echo_r('  </tr>');
    // Images
    for ($i = 0; $i < $galGallery->GetMaxImages(); $i ++)
    {
        echo_r('  <tr>');
        echo_r('  <td colspan="2"><hr></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%" style="vertical-align: top">' . get_translation('admin_gallery_image') . ' ' . ($i + 1) . ':');
        if (file_exists($galGallery->GetThumb($i)) == true)
        {
            echo_r ('     <br><a href="' . $galGallery->GetImage($i) . '" target="_blank"><img src="' . $galGallery->GetThumb($i) . '" width="' . $pagPage->GetAdditionalThumbWidth() . '" height="' . $pagPage->GetAdditionalThumbHeight() . '" alt=""></a>');
            echo_r('    <br><input type="checkbox" name="gal_Delete' .$i . '" value="1"> ' . get_translation('admin_delete') . '</td>');
        }
        echo_r('    <td><b>' . get_translation('admin_gallery_image') . ': ' . $galGallery->GetImage($i) . '</b><br>');
        echo_r('    [' . get_translation('admin_imagesizes') . ': ' . $pagPage->GetAdditionalImgWidth() . ' x ' . $pagPage->GetAdditionalImgHeight() . ' ' . get_translation('admin_imagesizespixel') . ']<br>');
        echo_r('    <input type="file" name="gal_Image' .$i . '" size="60"><br><br>');
        echo_r('    <b>' . get_translation('admin_gallery_thumb') . ': ' . $galGallery->GetThumb($i) . '</b><br>');
        echo_r('    [' . get_translation('admin_imagesizes') . ': ' . $pagPage->GetAdditionalThumbWidth() . ' x ' . $pagPage->GetAdditionalThumbHeight() . ' ' . get_translation('admin_imagesizespixel') . ']<br>');
        echo_r('    <input type="file" name="gal_ThumbImage' .$i . '" size="60"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_gallery_description') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="gal_Description' . $i . '" value="' . $galGallery->GetDescription($i) . '" size="70"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_gallery_link') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="gal_Link' . $i . '" value="' . $galGallery->GetLink($i) . '" size="70"></td>');
        echo_r('  </tr>');
    }
    echo_r('</table>');

    print_bottom_links("gal_Save", get_translation('admin_save'),
                       "gal_Cancel", get_translation('admin_cancel'), false);
    echo_r('</form>');

    print_admin_footer();
?>