<?php

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

File:         edit_linklist.php
Description:  Administrative module to edit link lists

*/

    include "../modules/class_linklist.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 link list
    $pagFolder = $PAGES_FOLDER;
    $lilFolder = $GALLERIES_FOLDER;

    $pagPage = new cPage;
    $pagPage->SetFolder($pagFolder);
    $lilLinkList = new cLinkList;
    $lilLinkList->SetFolder($lilFolder);

    if (isset ($_POST['lil_id']) == true)
        $lilId = $_POST['lil_id'];
    else if (isset ($_GET['id']) == true)
        $lilId = $_GET['id'];
    // Load hosting page
    if ($pagPage->LoadPageFromId($lilId) == false)
    {
        print_error_message(get_translation('admin_linklistedt_errorload') . ', ID: ' . $lilId);
        return;
    }
    // Then load link list
    $lilId = $pagPage->m_cLinkList->GetId();
    if ($lilLinkList->LoadLinkListFromId($lilId) == false)
    {
        print_error_message(get_translation('admin_linklistedt_errorload') . ', ID: ' . $lilId);
        return;
    }

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

    // Check for errors and if none, store the link list values
    if (isset ($_POST['lil_Save']))
    {
        for ($i = 0; $i < $lilLinkList->GetMaxLinks(); $i ++)
        {
             $lilLinkList->SetLink($i, $_POST['lil_Link' . $i]);
             $lilLinkList->SetDescription($i, $_POST['lil_Description' . $i]);
        }
        $lilLinkList->SetSortOrder($_POST['lil_Sort']);
        $lilLinkList->SetShowDescriptions($_POST['lil_ShowDescriptions']);
        $lilLinkList->SetOpenInNewWindow($_POST['lil_OpenInNewWindow']);
        if ($lilLinkList->SaveLinkList() == false)
        {
            $errorString = get_translation('admin_linklistedt_errorsave') . '!';
            $b_ErrorOccured = true;
        }
        else
        {
            // Link list is saved.
            log_message ('User ' . get_user_name() . ' saved link list ' . $lilLinkList->GetId());
            header ('Location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
            return;
        }
    }

    // Display the input dialog for edit link list
    print_admin_header(get_translation('admin_linklistedt_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="lil_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_linklist_id') . ':</td>');
    echo_r('    <td><b>' . $lilLinkList->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['lil_Sort']) == true)
        $galSort = $_POST['lil_Sort'];
    else
        $galSort = $lilLinkList->GetSortOrder();
    echo_r ('      <td><select name="lil_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['lil_ShowDescriptions']) == true)
        $showDescriptions = isset($_POST['lil_ShowDescriptions']);
    else
        $showDescriptions = $lilLinkList->ShowDescriptions();
    echo_r('    <td>' . get_translation('admin_showdescriptions') . ':</td>');
        echo('    <td><input type="checkbox" name="lil_ShowDescriptions" value="1"');
    if ($showDescriptions == true)
        echo(' checked');
    echo_r('></td>');
    echo_r('  </tr>');
    // Open in new window
    echo_r('  <tr>');
    if (isset($_POST['lil_OpenInNewWindow']) == true)
        $openInNewWindwo = isset($_POST['lil_OpenInNewWindow']);
    else
        $openInNewWindwo = $lilLinkList->OpenInNewWindow();
    echo_r('    <td>' . get_translation('admin_linklist_openinnewwindow') . ':</td>');
        echo('    <td><input type="checkbox" name="lil_OpenInNewWindow" value="1"');
    if ($openInNewWindwo == true)
        echo(' checked');
    echo_r('></td>');
    echo_r('  </tr>');
    // Links
    for ($i = 0; $i < $lilLinkList->GetMaxLinks(); $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_linklist_link') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="lil_Link' . $i . '" value="' . $lilLinkList->GetLink($i) . '" size="65"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_linklist_description') . ' ' . ($i + 1) . ':</td>');
        echo_r('    <td><input type="text" name="lil_Description' . $i . '" value="' . $lilLinkList->GetDescription($i) . '" size="70"></td>');
        echo_r('  </tr>');
    }
    echo_r('</table>');

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

    print_admin_footer();
?>