<?php
/**
 * Create admin edit page.
 *
 * PHPGEN is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHPGEN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHPGEN; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @copyright   EasySoft R&D Team (C) 2006
 * @author      Chunsheng Wang <wwccss@263.net>
 * @link        http://www.phpgen.com
 * @package     Page
 * @version     $Id: ManageAdminEdit.php,v 1.6 2006/08/15 15:52:40 wangcs Exp $
 */
require_once("../Include/Init.php");

/* Get table defination. */
$PageCFG = "Admin/" . $_SESSION["TableName"] . "/Edit.php";
$Page    = pageGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], $PageCFG);
$Table   = tblGetInfo($_SESSION["UserName"],  $_SESSION["ProjectID"], $_SESSION["TableName"]);
if(empty($Table))
{
    $MyJS->alert(" You must edit the table first.");
    $MyJS->goto("../Table/ManageTable.php");
    exit;
}

/* Set fields to filled. */
if(empty($Page))
{
    foreach($Table["Fields"] as $FieldName => $Field)
    {
        $Page["Fields"][$FieldName]["Label"]      = $Field["Label"];
        $Page["Fields"][$FieldName]["InputParam"] = $Field["InputParam"];
        $Page["Fields"][$FieldName]["InputType"]  = $Field["InputType"];
        $Page["Fields"][$FieldName]["Fill"]       = !empty($Field["Fill"]) ? $Field["Fill"] : "Required";
        $Page["Fields"][$FieldName]["DataRule"]   = $Field["DataRule"];
    }
}

/* User submits post form, create admin page. */
if($_POST["Submit"])
{
    /* Page params. */
    $Page["Type"]      = "AdminEdit";
    $Page["Header"]    = tblGetLabel($_SESSION["UserName"], $_SESSION["ProjectID"], $_SESSION[TableName]) . ": Edit Page";
    $Page["TableName"] = $_SESSION["TableName"];
    $Page["PhpFile"]   = $PageCFG;

    /* Get first Primary key or Unique keys. */
    unset($Page["IdField"]);
    $IdField = current($Table["KeysUNI"]);
    is_array($IdField) ? $Page["IdField"] = $IdField : $Page["IdField"][] = $IdField;

    /* Setting the fields. */
    foreach($_POST["Fill"] as $FieldName => $FieldFill)
    {
        $Page["Fields"][$FieldName]["Fill"]       = $FieldFill;
        $Page["Fields"][$FieldName]["Label"]      = $Table["Fields"][$FieldName]["Label"];
        $Page["Fields"][$FieldName]["InputType"]  = $_POST["InputType"][$FieldName];
        $Page["Fields"][$FieldName]["InputParam"] = $_POST["InputParam"][$FieldName];
        $Page["Fields"][$FieldName]["DataRule"]   = $_POST["DataRule"][$FieldName];
        $Page["Fields"][$FieldName]["Size"]       = $Table["Fields"][$FieldName]["Size"];
        strlen($FieldName) > $Page["FieldMaxLen"] ? $Page["FieldMaxLen"] = strlen($FieldName) : "";
    }

    /* Parse and get the codes. */
    $MyGEN = new Generator($MyTPL, $_CFG["GEN"], $Page, prjGetCodeStyle($_SESSION["UserName"], $_SESSION["ProjectID"]));
    $Codes = $MyGEN->parse();
    $MyGEN->savePHP();
    $MyGEN->saveHTML();

    /* Get the beautified code. */
    $MyBeautify = new Beautify("../Css/Beautify.css");
    foreach($Codes as $Language => $Code)
    {
        $MyBeautify->setLanguage($Language);
        $MyBeautify->setSource($Code);
        $Codes[$Language] = $MyBeautify->parse();
    }

    /* Get demo url. */
    $DemoURL = codeGetDemoBaseURL($_SESSION["UserName"], $_SESSION["ProjectID"]) . $PageCFG;

    /* Save config to file. */
    pageSaveConfigFile($_SESSION["UserName"], $_SESSION["ProjectID"], $Page["PhpFile"], array2String($Page, "Page", "Page"));

    /* Save to pagelist. */
    $PageList   = pageGetList($_SESSION["UserName"], $_SESSION["ProjectID"]);
    !in_array($Page["PhpFile"], $PageList) ? $PageList[] = $Page["PhpFile"] : "";
    pageSaveIndexFile($_SESSION["UserName"], $_SESSION["ProjectID"], array2String($PageList, "PageList", "Pages", true));

    /* Display the tpl. */
    $MyTPL->assign("Codes",   $Codes);
    $MyTPL->assign("DemoURL", $DemoURL);
    $MyTPL->assign("Table",   $Table);
    $MyTPL->display($_TPL["TplFile"]);
    $MyJS->goto("../Project/ListProject.php", "parent.leftfrm");
}
else
{
    /* Judge the code created or not.*/
    $SavedFile = codeGetSavedFiles($_SESSION["UserName"], $_SESSION["ProjectID"], $PageCFG);
    if($SavedFile)
    {
        $MyTPL->assign("PageCFG", $PageCFG);
        $MyTPL->assign("DemoURL", codeGetDemoBaseURL($_SESSION["UserName"], $_SESSION["ProjectID"]) . $PageCFG);
    }

    /* Create the input type list. */
    $FieldList = array_keys($Table["Fields"]);
    foreach($FieldList as $FieldName)
    {
        $InputTypeList[$FieldName] = htmlSelect($_CFG["Elements"], "InputType[$FieldName]", "Reverse", $Page["Fields"][$FieldName]["InputType"]);
        $DataRuleList[$FieldName]  = htmlSelect($_CFG["DataRule"], "DataRule[$FieldName]",  "Reverse", $Page["Fields"][$FieldName]["DataRule"]);
    }

    /* Assign and dispaly. */
    $MyTPL->assign("InputTypeList", $InputTypeList);
    $MyTPL->assign("DataRuleList",  $DataRuleList);
    $MyTPL->assign("Page",          $Page);
    $MyTPL->assign("Table",         $Table);
    $MyTPL->display($_TPL["TplFile"]);
}
?>