<?php
/**
 * Create common frontend 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: ManageFrontCommon.php,v 1.5 2006/08/15 15:52:40 wangcs Exp $
 */
require_once("../Include/Init.php");

/* Get config file and defination. */
!empty($_REQUEST["PageCFG"]) ? $PageCFG = $_REQUEST["PageCFG"] : "";
!empty($PageCFG) ? $Page = pageGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], $PageCFG) : "";

/* Save Page defination. */
if(!empty($_POST["SavePage"]) and !empty($_POST["PageCFG"]))
{
    $Page["Type"]      = "FrontCommon";
    $Page["PhpFile"]   = $_POST["PageCFG"];
    $Page["PageTitle"] = $_POST["PageTitle"];
    
    /* Save to config file. */
    pageSaveConfigFile($_SESSION["UserName"], $_SESSION["ProjectID"], $PageCFG, array2String($Page, "Page", "Page"));

    /* Save to index file. */
    $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");
}

/* Save block defination. */
if(!empty($_POST["SaveBlock"]))
{
    $Block["Caption"]   = $_POST["Caption"];
    $Block["TableName"] = $_POST["TableName"];
    $Block["Fields"]    = $_POST["Fields"];
    $Block["Where"]     = $_POST["Where"];

    $Page["Rows"][$_POST["RowID"]]["Cols"][$_POST["ColID"]]["Blocks"][$_POST["BlockID"]] = $Block;
    $Page["Rows"][$_POST["RowID"]]["Cols"][$_POST["ColID"]]["Layout"] = $_POST["Layout"];

    /* Parse and get the codes. */
    $MyGEN = new Generator($MyTPL, $_CFG["GEN"], $Page, prjGetCodeStyle($_SESSION["UserName"], $_SESSION["ProjectID"]));
    
    $Codes = $MyGEN->parse();
    $MyGEN->saveAll();
    
    /* Save to config file. */
    pageSaveConfigFile($_SESSION["UserName"], $_SESSION["ProjectID"], $PageCFG, array2String($Page, "Page", "Page"));
    
    $MyJS->alert("Successfully saved");
    exit;
}


/* Create row, col and block lists. */
$Rows   = arrayRange(1, 3, "Row");
$Cols   = arrayrange(1, 3, "Col");
$Blocks = arrayrange(1, 3, "Block");

$RowList   = htmlSelect($Rows,   "RowID");
$ColList   = htmlSelect($Cols,   "ColID");
$BlockList = htmlSelect($Blocks, "BlockID");

/* Get demo url. */
$DemoURL = codeGetDemoBaseURL($_SESSION["UserName"], $_SESSION["ProjectID"]) . $Page["PhpFile"];
    
/* Assign and display. */
$MyTPL->assign("Page",      $Page);
$MyTPL->assign("PageCFG",   $PageCFG);
$MyTPL->assign("DemoURL",   $DemoURL);
$MyTPL->assign("RowList",   $RowList);
$MyTPL->assign("ColList",   $ColList);
$MyTPL->assign("BlockList", $BlockList);
$MyTPL->display($_TPL["TplFile"]);
?>