<?php

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

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

*/

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

    $b_NotFinished = true;
    $b_ErrorOccured = false;
    $pgfId = "";
    $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
    $pgfEdtAction = "new";
    if (isset ($_POST['pgf_EdtAction']) == true)
    {
        if ($_POST['pgf_EdtAction'] == "edit")
            $pgfEdtAction = "edit";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "edit")
            $pgfEdtAction = "edit";
    }

    $pgfPageFile = new cPageFile;
    $pgfPageFile->SetFolder($PAGEFILES_FOLDER);

    if ($pgfEdtAction == "edit")  // Edit -> load it
    {
        if (isset ($_POST['pgf_id']) == true)
            $pgfId = $_POST['pgf_id'];
        else if (isset ($_GET['id']) == true)
            $pgfId = $_GET['id'];
        if ($pgfPageFile->LoadPageFileFromId($pgfId) == false)
        {
            print_error_message(get_translation('admin_pagefiledt_errorload') . ', ID: ' . $pgfId);
            return;
        }
    }

    // Pagefile edit cancelled?
    if (isset ($_POST['pgf_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGEFILES_SITE);
        return;
    }

    // Check for errors and if none, store the pagefiles values
    if (isset ($_POST['pgf_Save']))
    {
        if (isset($_POST['pgf_Title']) == false || $_POST['pgf_Title'] == "")
        {
            $errorString = get_translation('admin_pagefiledt_errortitle') . '!';
            $b_ErrorOccured = true;
        }
        else if (isset($_POST['pgf_Description']) == false || $_POST['pgf_Description'] == "")
        {
            $errorString = get_translation('admin_pagefiledt_errordescription') . '!';
            $b_ErrorOccured = true;
        }
        // Uploads
        if ($b_ErrorOccured == false)
        {
            if ($pgfEdtAction == "new")    // New -> create Id first
            {
                $pgfPageFile->CreatePageFile(get_user_name());
            }

            // File
            if ($b_ErrorOccured == false && ($pgfEdtAction == "new" || !(empty ($_FILES['pgf_File']['name']))))
            {
                $fileExt = strtolower(retrieve_extension($_FILES['pgf_File']['name']));
                if ($pgfEdtAction == "edit")
                    unlink($pgfPageFile->GetFile());  // delete old one first
                $destName = $PAGEFILES_FOLDER . $pgfPageFile->GetId() . $fileExt;
                if (@ copy ($_FILES['pgf_File']['tmp_name'], $destName) == false)
                {
                    $errorString = get_translation('admin_pagefiledt_errorupload') . ': ' . $_FILES['pgf_File']['name'] . ', ' . $destName. '!';
                    $b_ErrorOccured = true;
                }
                else
                {
                    $pgfPageFile->SetFile($destName);
                    $pgfPageFile->SetOriginName($_FILES['pgf_File']['name']);
                }
            }
        }

        if ($b_ErrorOccured == false)
        {
            // If done, fill the rest of the parameters
            $pgfPageFile->SetFolder($PAGEFILES_FOLDER);
            $pgfPageFile->SetTitle($_POST['pgf_Title']);
            $pgfPageFile->SetDescription($_POST['pgf_Description']);
            if ($pgfPageFile->SavePageFile(get_user_name()) == false)
            {
                if ($pgfEdtAction == "new")
                    $errorString = get_translation('admin_pagefiledt_errorcreate') . '!';
                else
                    $errorString = get_translation('admin_pagefiledt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                if ($pgfEdtAction == "edit")
                {
                    // Pagefile is saved.
                    log_message ('User ' . get_user_name() . ' saved pagefile ' . $pgfPageFile->GetId());
                    header ('Location: ' . $ADMIN_LIST_PAGEFILES_SITE);
                    return;
                }
                else
                    log_message ('User ' . get_user_name() . ' created pagefoöe ' . $pgfPageFile->GetId());

                $b_NotFinished = false;
            }
        }
    }

    // Display the input dialog for edit pagefile
    $titleString = get_translation('admin_pagefiledt_newhead');
    if ($pgfEdtAction == "edit")  // Edit
        $titleString = get_translation('admin_pagefiledt_edithead');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGEFILES_SITE, get_translation('admin_overview'));
    if ($errorString !="")
        print_error_message($errorString);

    if ($b_NotFinished == false)
    {
        // Pagefile is created. Display form to create another or go back to list
        echo_r('<p>' . get_translation('admin_pagefiledt_filecreated') . ', ID : ' . $pgfPageFile->GetId() . '.<br></p>');
        print_bottom_links("pgf_New", get_translation('admin_pagefiledt_createanother'),
                           "pgf_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('    </td>');
        echo_r('  </tr>');
        if ($pgfEdtAction == "edit")  // Edit
        {
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_pagefil_id') . ':</td>');
            echo_r('    <td><b>' . $pgfPageFile->GetId() . '</b>');
            echo_r('      <input type="hidden" value="' .  $pgfPageFile->GetId() . '" name="pgf_id">  ');
            echo_r('    </td>');
            echo_r('  </tr>');
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_pagefil_created') . ':</td>');
            echo_r('    <td><b>' . $pgfPageFile->GetDate() . ' (' . $pgfPageFile->GetCreatedBy() . ')</b></td>');
            echo_r('  </tr>');
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_pagefil_modified') . ':</td>');
            echo_r('    <td><b>' . $pgfPageFile->GetModified() . ' (' . $pgfPageFile->GetAuthor() . ')</b></td>');
            echo_r('  </tr>');
        }
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_pagefil_title') . ':</td>');
        if (isset($_POST['pgf_Title']))
            echo_r('    <td><input type="text" name="pgf_Title" size="60" value="' . $_POST['pgf_Title'] . '"></td>');
        else
            echo_r('    <td><input type="text" name="pgf_Title" size="60" value="' .  $pgfPageFile->GetTitle() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_pagefil_description') . ':</td>');
        if (isset($_POST['pgf_Description']))
            echo_r('    <td><input type="text" name="pgf_Description" size="80" value="' . $_POST['pgf_Description'] . '"></td>');
        else
            echo_r('    <td><input type="text" name="pgf_Description" size="80" value="' .  $pgfPageFile->GetDescription() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo  ('    <td width="30%">' . get_translation('admin_pagefil_filename') . ':</td>');
        echo  ('    <td>');
        if ($pgfEdtAction == "edit")  // Edit
        {
            echo ('<input type="text" size="60" value="' . $pgfPageFile->GetFile() . '" readonly><br>');
            echo ('<a href="' . $pgfPageFile->GetFile() . '" target="_blank">' . $pgfPageFile->GetDescription() . '</a><br>');
        }
        echo_r('<input type="file" name="pgf_File" size="60" value=""></td>');
        echo_r('  </tr>');
        echo_r('</table>');

        if ($pgfEdtAction == "edit")  // Edit
            print_bottom_links("pgf_Save", get_translation('admin_save'),
                               "pgf_Cancel", get_translation('admin_cancel'), false);
        else  // New
            print_bottom_links("pgf_Save", get_translation('admin_create'),
                               "pgf_Cancel", get_translation('admin_cancel'), false);

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

    print_admin_footer();
?>