<?php
/**
 * Mange table.
 *
 * 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     Table
 * @version     $Id: ManageTable.php,v 1.5 2006/08/15 15:54:36 wangcs Exp $
*/
require_once("../Include/Init.php");

/* Get Table defination. */
$Table = tblGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], $_SESSION["TableName"]);
if(empty($Table))
{
    $UserDB           = prjConnectDB($_SESSION["UserName"], $_SESSION["ProjectID"]);
    $Table["Fields"]  = dbGetFields($UserDB, $_SESSION["TableName"]);
}
$Table["Label"]     = tblGetLabel($_SESSION["UserName"], $_SESSION["ProjectID"], $_SESSION["TableName"]);

/* Define language file which will be writed to Languages/Tables/TableName.php. */
$Page["TableName"] = $_SESSION["TableName"];
$Page["Type"]      = "LangTable";
$Page["PhpFile"]   = $_CFG["UserLang"] . "/Tables/" . $_SESSION[TableName] . ".php";

$Page["Table"]["Label"] = $Table["Label"];

/* User submit the post. */
if(!empty($_POST["Submit"]))
{
    foreach($Table["Fields"] as $FieldName => $Field)
    {
        $Table["Fields"][$FieldName]["Label"]      = !empty($_POST["Label"][$FieldName]) ? $_POST["Label"][$FieldName] : $FieldName;
        $Table["Fields"][$FieldName]["InputType"]  = $_POST["InputType"][$FieldName];
        $Table["Fields"][$FieldName]["InputParam"] = $_POST["InputParam"][$FieldName];
        $Table["Fields"][$FieldName]["DataRule"]   = $_POST["DataRule"][$FieldName];
        $Table["Fields"][$FieldName]["Fill"]       = $_POST["Fill"][$FieldName];
        
        /* add the label to Page defination. */
        $Page["Fields"]["$FieldName"] = $Table["Fields"][$FieldName]["Label"];
    }
    
    /* Build the language file of the table. */
    $MyGEN = new Generator($MyTPL, $_CFG["GEN"], $Page, prjGetCodeStyle($_SESSION["UserName"], $_SESSION["ProjectID"]));
    $Codes = $MyGEN->parse($Lang);
    $MyGEN->saveLang();

    /* Get unique keys. */
    $Table["KeysUNI"] = dbGetUniqueKeys(prjConnectDB($_SESSION["UserName"], $_SESSION["ProjectID"]), $_SESSION["TableName"]);
    
    /* Save to file. */
    tblSaveConfigFile($_SESSION["UserName"], $_SESSION["ProjectID"], $_SESSION["TableName"], array2String($Table, "Table", "Table"));
}

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

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