<?php
/**
 * Create all tables' admin list 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: BuildAllAdminList.php,v 1.5 2006/08/15 15:52:40 wangcs Exp $
 */
require_once("../Include/Init.php");

/* Get Table defination. */
$UserDB = prjConnectDB($_SESSION["UserName"], $_SESSION["ProjectID"]);
$Tables = dbGetTables($UserDB);

foreach($Tables as $TableName)
{
    /* Get page defination. */
    $Page  = pageGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], "Admin/"  . $TableName . "/List.php");

    /* If no defination, set by hand. */
    if(empty($Page))
    {
        $Table = tblGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], $TableName);

        if(empty($Table))
        {
            $TablesUndone[] = $TableName;
            continue;
        }
        else
        {
            foreach($Table["Fields"] as $Field)
            {
                $Page["Fields"][$Field["Name"]] = $Field["Label"];
            }
        }
        $Page["Type"]      = "AdminList";
        $Page["Header"]    = "List $TableName";
        $Page["TableName"] = $TableName;
        $Page["PhpFile"]   = "Admin/" . $TableName . "/List.php";
    }

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

    /* Get demo url. */
    $DemoUrlList[] = codeGetDemoBaseURL($_SESSION["UserName"], $_SESSION["ProjectID"]) . $Page["PhpFile"];

    /* Save config 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"));
}

/* Refresh the project tree. */
$MyJS->goto("../Project/ListProject.php", "parent.leftfrm");

/* Assign and display. */
$MyTPL->assign("TablesUndone", $TablesUndone);
$MyTPL->assign("DemoUrlList",  $DemoUrlList);
$MyTPL->display($_TPL["TplFile"]);
?>