<?php

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

File:         list_pagefiles.php
Description:  Administrative overview of all pagefiles

*/

    include "../modules/class_pagefile.php";
    include "../modules/class_pagefilelist.php";
    include "../modules/mod_admin.php";


    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_reporter() == true || isset ($_POST['pagefil_Cancel']))
    {
        header ("location: " . $ADMIN_MAIN_SITE);
        return;
    }

    // Create new pagefile?
    if (isset ($_POST['pgfl_CreatePageFile']))
    {
        header ('Location: ' . $ADMIN_EDIT_PAGEFILES_SITE . '?action=new');
        return;
    }
    // Edit pagefile?
    if (isset ($_POST['pgfl_EditPageFile']))
    {
        header ('Location: ' . $ADMIN_EDIT_PAGEFILES_SITE . '?id=' . $_POST['pgfl_PagFilId'] . '&action=edit');
        return;
    }
    // Delete pagefile?
    if (isset ($_POST['pgfl_DeletePageFile']))
    {
        header ('Location: ' . $ADMIN_MODIFY_PAGEFILES_SITE . '?id=' . $_POST['pgfl_PagFilId'] . '&action=delete');
        return;
    }
    // Lock pagefile?
    if (isset ($_POST['pgfl_LockPageFile']))
    {
        header ('Location: ' . $ADMIN_MODIFY_PAGEFILES_SITE . '?id=' . $_POST['pgfl_PagFilId'] . '&action=lock');
        return;
    }
    // Unlock pagefile?
    if (isset ($_POST['pgfl_UnlockPageFile']))
    {
        header ('Location: ' . $ADMIN_MODIFY_PAGEFILES_SITE . '?id=' . $_POST['pgfl_PagFilId'] . '&action=unlock');
        return;
    }

    // Display
    print_admin_header(get_translation('admin_pagefill_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-3', true, get_translation('admin_backtomain'));
    $pgf = new cPageFile;
    $pgfList = new cPageFileList;
    $pgfFilter = 0;
    if (isset ($_GET['filter']) && $_GET['filter'] != "")
        $pgfFilter = $_GET['filter'];
    else if (isset ($_POST['pgfl_filter']) && $_POST['pgfl_filter'] != "")
        $pgfFilter = $_POST['pgfl_filter'];

    $pgf->SetFolder($PAGEFILES_FOLDER);
    $pgfList->SetFolder($PAGEFILES_FOLDER);
    $pgfList->LoadPageFileList($pgfFilter);

    echo_r ('<form method="post" action="">');
    echo_r ('<table border="0" width="100%">');
    echo_r ('<tr>');
    echo_r ('  <td width="20%"><input type="submit" class="bottom_btn" value="' . get_translation('admin_pagefill_createnew') . '" name="pgfl_CreatePageFile"></td>');
    echo_r ('  <td width="10%">' . get_translation('admin_filter') . '</td>');
    echo_r ('  <td>');
    echo_r ('  <select name="pgfl_filter" onChange="submit();">');
    echo   ('  <option value="' . $FILTER_ALL . '"');
    if ($pgfFilter == $FILTER_ALL)
        echo (' selected');
    echo_r ('>' . get_translation('admin_all') . '</option>');
    echo   ('  <option value="' . $FILTER_UNLOCKED . '"');
    if ($pgfFilter == $FILTER_UNLOCKED)
        echo (' selected');
    echo_r ('>' . get_translation('admin_unlocked') . '</option>');
    echo   ('  <option value="' . $FILTER_LOCKED . '"');
    if ($pgfFilter == $FILTER_LOCKED)
        echo (' selected');
    echo_r ('>' . get_translation('admin_locked') . '</option>');
    echo_r ('  </select>');
    echo_r ('  </td>');
    echo_r ('</tr>');
    echo_r ('</table>');
    echo_r ('</form>');

    $pgfList->SortByTitle(false);
    $listOfPageFiles = $pgfList->GetPageFileList();
    // No entries?
    if (count($listOfPageFiles) < 1)
        echo_r ('<p>&nbsp;' . get_translation('admin_noentries') . '</p>');

    for ($d = 0; $d < count($listOfPageFiles); $d ++)
    {
        $pgf->LoadPageFileFromId($listOfPageFiles[$d]);

        echo_r('<hr>');
        echo_r('<table width="100%" border="0">');
        echo_r('  <tr>');
        echo_r('    <td width="14%">');
        echo  ('      <a href="' . $ADMIN_EDIT_PAGEFILES_SITE . '?id=' . $pgf->GetId() . '&amp;action=edit">');
        echo_r('<img src="' . $FILE_IMAGE . '" title="'. get_translation('admin_edit'). '"></a>');
        if ($pgf->IsLocked() == true)
            echo_r('       <img src="' . $LOCKED_IMAGE . '" title="' . get_translation('admin_locked') . '" alt="">');
        echo_r('    </td>');
        echo_r('    <td width="56%">');
        echo_r('    <table width="100%" border="0">');
        echo_r('      <tr>');
        echo_r('        <td width="50%">');
        echo_r('       <b>' . get_translation('admin_pagefil_title') . ': ' . $pgf->GetTitle() . '</b><br>');
        echo_r('       ' . get_translation('admin_pagefil_orginname') . ': ' . $pgf->GetOriginName());
        echo_r('        </td>');
        echo_r('        <td width="50%">');
        echo_r('       ' . get_translation('admin_pagefil_id') . ': ' . $pgf->GetId() . '<br>');
        echo_r('       ' . get_translation('admin_pagefil_created') . ': ' . $pgf->GetDate() . '<br>');
        echo_r('        </td>');
        echo_r('      </tr>');
        echo_r('      <tr>');
        echo_r('        <td width="100%" colspan="2">');
        echo_r('       ' . get_translation('admin_pagefil_filename') . ': <input type="text" size="60" value="' . $pgf->GetFile() . '" readonly><br>');
        echo_r('        </td>');
        echo_r('      </tr>');
        echo_r('    </table>');

        echo_r('    </td>');
        echo_r('    <td width="30%">');
        echo_r('    <form method="post" action="">');
        echo_r('    <input type="hidden" value="' . $pgf->GetId() . '" name="pgfl_PagFilId">  ');
        if (user_is_reporter() == false)
        {
            echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_edit') . '" name="pgfl_EditPageFile">  ');
            if ($pgf->IsLocked() == true)
            {
                echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_unlock') . '" name="pgfl_UnlockPageFile">  ');
            }
            else
            {
                echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_lock') . '" name="pgfl_LockPageFile">  ');
            }
        }
        if (user_is_admin() == true)
            echo_r('    <input type="submit"  class="side_btn" value="' . get_translation('admin_delete') . '" name="pgfl_DeletePageFile">  ');
        echo_r('    </form>');
        echo_r('    </td>');
        echo_r('  </tr>');
        echo_r('</table>');
    }
    print_bottom_links("pagefil_Cancel", get_translation('admin_ok'));

    print_admin_footer();
?>