<?php

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

File:         edit_youtubegallery.php
Description:  Administrative module to edit youtube galleries

*/

    include "../modules/class_youtubegallery.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 youtube gallery
    $pagFolder = $PAGES_FOLDER;
    $galFolder = $GALLERIES_FOLDER;

    $pagPage = new cPage;
    $pagPage->SetFolder($pagFolder);
    $galGallery = new cYouTubeGallery;
    $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_youtubegalleryedt_errorload') . ', ID: ' . $galId);
        return;
    }
    // Then load youtube gallery
    $galId = $pagPage->m_cYouTubeGallery->GetId();
    if ($galGallery->LoadYouTubeGalleryFromId($galId) == false)
    {
        print_error_message(get_translation('admin_youtubegalleryedt_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 youtube gallery values
    if (isset ($_POST['gal_Save']))
    {
        for ($i = 0; $i < $galGallery->GetMaxYVideos(); $i ++)
        {
             // Do some extraction of the link...
             if (isset($_POST['gal_YouTubeLink' . $i]) == true && $_POST['gal_YouTubeLink' . $i] != "")
             {
                 $linkArray = explode("watch?v=", $_POST['gal_YouTubeLink' . $i]);
                 if (isset($linkArray[1]) == true)
                 {
                     $linkArray = explode("&", $linkArray[1]);
                     $_POST['gal_YouTubeLink' . $i] = "http://www.youtube.com/v/" . $linkArray[0];
                 }
             }
             $galGallery->SetYouTubeLink($i, $_POST['gal_YouTubeLink' . $i]);
             $galGallery->SetDescription($i, $_POST['gal_Description' . $i]);
        }
        $galGallery->SetSortOrder($_POST['gal_Sort']);
        $galGallery->SetShowDescriptions($_POST['gal_ShowDescriptions']);
        $galGallery->SetShowRelated($_POST['gal_ShowRelated']);
        if ($galGallery->SaveYouTubeGallery() == false)
        {
            $errorString = get_translation('admin_youtubegalleryedt_errorsave') . '!';
            $b_ErrorOccured = true;
        }
        else
        {
            // Gallery is saved.
            log_message ('User ' . get_user_name() . ' saved youtube gallery ' . $galGallery->GetId());
            header ('Location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
            return;
        }
    }

    // Display the input dialog for edit youtube gallery
    print_admin_header(get_translation('admin_youtubegalleryedt_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%">');
    // 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_youtubegallery_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>');
    // Show related
    echo_r('  <tr>');
    if (isset($_POST['gal_ShowRelated']) == true)
        $showRelated= isset($_POST['gal_ShowRelated']);
    else
        $showRelated = $galGallery->ShowRelated();
    echo_r('    <td>' . get_translation('admin_youtubegallery_showrelated') . ':</td>');
        echo('    <td><input type="checkbox" name="gal_ShowRelated" value="1"');
    if ($showRelated == true)
        echo(' checked');
    echo_r('></td>');
    echo_r('  </tr>');
    // Youtube videos
    for ($i = 0; $i < $galGallery->GetMaxYVideos(); $i ++)
    {
        echo_r('  <tr>');
        echo_r('  <td colspan="2"><hr></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_youtubegallery_link') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="gal_YouTubeLink' . $i . '" value="' . $galGallery->GetYouTubeLink($i) . '" size="65"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_youtubegallery_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('</table>');

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

    print_admin_footer();
?>