<?php

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

File:         edit_pagecontents.php
Description:  Administrative module to edit pagecontents

*/

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

    $b_ErrorOccured = false;
    $pageId = "";
    $errorString = "";

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


    $pagePage = new cPage;
    $pagePage->SetFolder($PAGES_FOLDER);
    $textFile = new cTextFile;

    if (isset ($_POST['page_id']) == true)
        $pageId = $_POST['page_id'];
    else if (isset ($_GET['id']) == true)
        $pageId = $_GET['id'];
    if ($pagePage->LoadPageFromId($pageId) == false)
    {
        print_error_message(get_translation('admin_pagecontedt_errorload') . ', ID: ' . $pageId);
        return;
    }

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

    // Check for errors and if none, store the page values
    if (isset ($_POST['page_Save']))
    {
        if (isset($_POST['page_Title']) == false || $_POST['page_Title'] == "")
        {
            $errorString = get_translation('admin_pagecontedt_errortitle') . '!';
            $b_ErrorOccured = true;
        }
        if (isset($_POST['page_NaviTitle']) == false || $_POST['page_NaviTitle'] == "")
        {
            $errorString = get_translation('admin_pagecontedt_errornavititle') . '!';
            $b_ErrorOccured = true;
        }
        if (isset($_POST['page_Content']) == false || $_POST['page_Content'] == "")
        {
            $errorString = get_translation('admin_pagecontedt_errorcontent') . '!';
            $b_ErrorOccured = true;
        }

        if ($b_ErrorOccured == false)
        {
            if ($textFile->SaveToFile($pagePage->GetContentFile(), $_POST['page_Content']) == false)
            {
                $errorString = get_translation('admin_pagecontedt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
			if ($b_ErrorOccured == false)
			{
			    $pagePage->SetTitle($_POST['page_Title']);
			    $pagePage->SetNavigationTitle($_POST['page_NaviTitle']);
			}
            if ($pagePage->SavePage(get_user_name()) == false)
            {
                $errorString = get_translation('admin_pagecontedt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                log_message ('User ' . get_user_name() . ' save pagecontent ' . $pagePage->GetId());
                header('location: ' . $ADMIN_LIST_PAGECONTENTS_SITE);
                return;
            }
        }
    }

    // Display the input dialog for edit pagecontents
    $titleString = get_translation('admin_pagecontedt_head');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGECONTENTS_SITE, get_translation('admin_overview'), false);
    // Enable editor?
    if ($ADMIN_USEWYSIGWYG == true)
        print_wysiwyg_header();
    finish_admin_header($titleString, 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%">');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_id') . ':</td>');
    echo_r('    <td><b>' . $pagePage->GetId() . '</b>');
    echo_r('      <input type="hidden" value="' .  $pagePage->GetId() . '" name="page_id">  ');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_created') . ':</td>');
    echo_r('    <td><b>' . $pagePage->GetDate() . ' (' . $pagePage->GetCreatedBy() . ')</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_modified') . ':</td>');
    echo_r('    <td><b>' . $pagePage->GetModified() . ' (' . $pagePage->GetAuthor() . ')</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_viewcount') . ':</td>');
    echo_r('    <td><b>' . $pagePage->GetViewCount() . '</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_page_type') . ':</td>');
    if ($pagePage->GetType() == PAGETYPE_CONTACT)
        echo_r('    <td><b>' . get_translation('admin_page_typecontact') . '</b>');
    else if ($pagePage->GetType() == PAGETYPE_GALLERY)
        echo_r('    <td><b>' . get_translation('admin_page_typegallery') . '</b>');
    else if ($pagePage->GetType() == PAGETYPE_YOUTUBEGALLERY)
        echo_r('    <td><b>' . get_translation('admin_page_typeyoutubegallery') . '</b>');
    else
        echo_r('    <td><b>' . get_translation('admin_page_typeplain') . '</b>');
    echo_r('    </td>');
    echo_r('  </tr>');
    // Title
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_page_title') . ':</td>');
    echo_r('    <td><input type="text" name="page_Title" size="60" value="' .  $pagePage->GetTitle() . '"></td>');
    echo_r('  </tr>');
    // Navigation title
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_page_navtitle') . ':</td>');
    echo_r('    <td><input type="text" name="page_NaviTitle" size="60" value="' .  $pagePage->GetNavigationTitle() . '"></td>');
    echo_r('  </tr>');
    // Content
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_page_content') . ':</td>');
    $pageContent = $textFile->LoadFromFile($pagePage->GetContentFile());
    if (isset($_POST['page_Content']))
        $pageContent = $_POST['page_Content'];
    echo_r('    <td>');
    if ($ADMIN_WRAPEDITOR == true)
        echo_r('      <textarea name="page_Content" id="page_Content" cols="100" rows="25">');
    else
        echo_r('      <textarea name="page_Content" id="page_Content" cols="100" rows="25" wrap="off">');
    echo_r($pageContent);
    echo_r('      </textarea>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    print_bottom_links("page_Save", get_translation('admin_save'),
                       "page_Cancel", get_translation('admin_cancel'), false);
    echo_r('</form>');

    print_admin_footer();
?>