<?php
/**
 * Manage 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: AjaxFrontCommon.php,v 1.5 2006/08/15 15:52:40 wangcs Exp $
 */
require_once('../Include/Init.php');

/* Cpaint process. */
$MyCP->register($_POST["cpaint_function"]);
$MyCP->start();
$MyCP->return_data();

/**
 * Get defination of a block.
 *
 * @author                     Chunsheng Wang <wwccss@263.net>
 * @global object              Object of Cpaint class.
 * @global array               System configuration.
 * @param  string $PageCFG     Page config file.
 * @param  int    $RowID
 * @param  int    $ColID
 * @param  int    $BlockID
 */
function getBlock($PageCFG, $RowID, $ColID, $BlockID)
{
    global $MyCP, $_CFG;

    /* Get page defination. */
    $Page           = pageGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], $PageCFG);
    $CurrentLayout  = $Page["Rows"][$RowID]["Cols"][$ColID]["Layout"];
    $CurrentTable   = $Page["Rows"][$RowID]["Cols"][$ColID]["Blocks"][$BlockID]["TableName"];
    $CurrentFields  = join(",", $Page["Rows"][$RowID]["Cols"][$ColID]["Blocks"][$BlockID]["Fields"]);
    $CurrentCaption = $Page["Rows"][$RowID]["Cols"][$ColID]["Blocks"][$BlockID]["Caption"];
    $CurrentWhere   = $Page["Rows"][$RowID]["Cols"][$ColID]["Blocks"][$BlockID]["Where"];

    /* Get all tables. */
    $Tables = tblGetList($_SESSION["UserName"], $_SESSION["ProjectID"]);
    foreach($Tables as $TableName => $Table)
    {
        $Tables[$TableName] = $Table["Label"];
    }
    empty($Tables) ? $Tables = dbGetTables(prjConnectDB($_SESSION["UserName"], $_SESSION["ProjectID"])) : "";
    empty($CurrentTable) ? $CurrentTable = key($Tables) : "";
    
    /* Set Root name. */
	$MyCP->set_name("Block");

    /* Set Layout node. */
    $LayoutNode = &$MyCP->add_node("Layout");
	$LayoutNode->set_data(htmlSelect($_CFG["Layout"], "Layout", "Reverse", $CurrentLayout, "style='width:90%'"));

	/* Set Table node. */
	$TableNode = &$MyCP->add_node("TableName");
	$TableNode->set_data(htmlSelect($Tables, "TableName", "Reverse", $CurrentTable, "size=4 multiple=multiple style='width:90%' ondblclick='getFields(this.value)'"));

	/* Set Field node. */
	$FieldListNode = &$MyCP->add_node("Field");
	$FieldListNode->set_data(createFieldList($CurrentTable, $CurrentFields));

	/* Set Caption node. */
	$CaptionNode = &$MyCP->add_node("Caption");
	$CaptionNode->set_data($CurrentCaption);

	/* Set Where node. */
	$WhereNode = &$MyCP->add_node("Where");
	$WhereNode->set_data($CurrentWhere);
}

/**
 * Get fieldlist of a table.
 *
 * @author                     Chunsheng Wang <wwccss@263.net>
 * @global object              Object of Cpaint class.
 * @param  string $TableName
 */
function getFields($TableName)
{
	global $MyCP;

    /* Set Root name. */
	$MyCP->set_name("Block");

	/* Set List node. */
	$FieldListNode = &$MyCP->add_node("Field");
	$FieldListNode->set_data(createFieldList($TableName));
}

/**
 * Create html select of fields of a table.
 *
 * @author                          Chunsheng Wang <wwccss@263.net>
 * @global object                   Object of Cpaint class.
 * @global array                    System configuration.
 * @param  string $TableName
 * @param  string $CurrentFields    Fields to be selected.
 * @return html                     Fields list in <select></select>
 */
function createFieldList($TableName, $CurrentFields)
{
    /* Get Fields of the table. */
	$Table  = tblGetInfo($_SESSION["UserName"], $_SESSION["ProjectID"], $TableName);
	if(!empty($Table))
	{
	    $Fields = $Table["Fields"];
	}
	else
	{
    	$Fields = dbGetFields(prjConnectDB($_SESSION["UserName"], $_SESSION["ProjectID"]), $TableName);
    }

    /* Remove other items. */
    foreach($Fields as $FieldName => $Field)
    {
        unset($Fields[$FieldName]);
        $Fields[$FieldName] = $Field["Label"];
    }
    
    return htmlSelect($Fields, "Fields[]", "Reverse", $CurrentFields, "size=4 multiple=multiple style='width:90%'");
}
?>