<?php

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

File:         edit_construction.php
Description:  Administrative module to construction

*/

    include "../modules/mod_admin.php";
    include "../modules/class_textfile.php";

    $bErrorOccured = false;
    $errorString = "";
    $cFile = new cTextFile;

    // Check privilege
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_reporter() == true)
    {
        header('location: '. $ADMIN_MAIN_SITE);
        return;
    }

    // Cancel titles?
    if (isset($_POST['construction_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }
    
    // Save construction page?
    if (isset($_POST['construction_Save']))
    {
        // Activate
        $cConfig->SetParameter("site_show_construction", isset($_POST['construction_Activate']) == true);
        // Construction title
        $cFile->SaveToFile('../' . $CONSTRUCTIONTITLE_CONFIG_FILE, $_POST['construction_Title']);
        // Construction image
        if (isset($_POST['construction_NoImage']) == true)
        {
            if (file_exists($cConfig->GetParameter("site_image_construction")) == true)
                unlink($cConfig->GetParameter("site_image_construction"));
        }
        if (!(empty ($_FILES['construction_Image']['name'])))
        {
            if (check_image_extension($_FILES['construction_Image']['name']) == false)  // Check extension
            {
                $errorString = get_translation('admin_construct_errortype') . '! : ' . $_FILES['construction_Image']['name'];
            }
            else
            {
                // Delete old one first!
                if (file_exists($cConfig->GetParameter("site_image_construction")) == true)
                    unlink($cConfig->GetParameter("site_image_construction"));
                $destName = $CONSTRUCTION_IMAGE . retrieve_extension($_FILES['construction_Image']['name']);
                // Is resize necessary? ...otherwise copy
                if(resize_image($_FILES['construction_Image']['name'], $_FILES['construction_Image']['tmp_name'], $destName, $SITE_MAXIMGWIDTH, $SITE_MAXIMGHEIGHT, false) == false)
                {
                    $errorString = get_translation('admin_construct_errorimage') . ': ' . $_FILES['construction_Image']['tmp_name'] . '!';
                }
                else
                {
                    $cConfig->SetParameter("site_image_construction", $destName);
                }
            }
        }
        if ($cConfig->SaveConfigFile() == false)
            $errorString = get_translation('admin_construct_errorconfig') . "!";
        if ($errorString == "")
        {
            log_message ('User ' . get_user_name() . ' saved construction');
            header('location: ' . $ADMIN_MAIN_SITE);
            return;
        }
    }

    // Display
    print_admin_header(get_translation('admin_construct_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-7', true, get_translation('admin_backtomain'), "", "", false);
    // Enable editor?
    if ($ADMIN_USEWYSIGWYG == true)
        print_wysiwyg_header();
    finish_admin_header(get_translation('admin_construct_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-7', true, get_translation('admin_backtomain'), "", "");

    if ($errorString != "")
        print_error_message ($errorString);
        
    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    // Activate construction page
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_construct_activate') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="construction_Activate" value="1"');
    if ($cConfig->GetParameter("site_show_construction") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Construction page image
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_maxuploadsize') . ':</td>');
    echo_r('    <td><b>' . ini_get("upload_max_filesize") . '</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_sizerestriction') . ':</td>');
    if ($cConfig->GetParameter("site_imagerestriction") == "1")
        echo_r('    <td><b>' . $cConfig->GetParameter("site_maximgwidth") . ' x ' . $cConfig->GetParameter("site_maximgheight") . ' ' . get_translation('admin_sizerestrictionnpixels') . '</b></td>');
    else
        echo_r('    <td><b>' . get_translation('admin_sizerestrictionnone') . '</b></td>');
    echo_r('  </tr>');
    echo_r ('  <tr>');
    echo_r('    <td>' . get_translation('admin_construct_image') . ':</td>');
    echo_r ('    <td>');
    if (file_exists($cConfig->GetParameter("site_image_construction")) == true)
    {
        echo_r('     <br><img src="' . $cConfig->GetParameter("site_image_construction") . '" alt="">');
    }
    echo_r('    <br><input type="file" name="construction_Image" size="50">');
    echo_r('    <br><input type="checkbox" name="construction_NoImage" value="1">&nbsp;' . get_translation('admin_construct_noimageordelete') . '');
    echo_r('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Construction page text
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_construct_text') . ':</td>');
    echo_r ('    <td>');
    echo ('      <textarea cols="80" rows="20" id="page_Content" name="construction_Title"');
    if ($ADMIN_WRAPEDITOR == true)
        echo (' wrap="off"');
    echo_r ('>' . $cFile->LoadFromFile('../' . $CONSTRUCTIONTITLE_CONFIG_FILE) . '</textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

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

    print_admin_footer();
?>