Location: PHPKode > scripts > flashPash > flashpash/flashPash.firefly.php
<?php

/*==-==-==-==-==-==-==-==-==-==-==-==-==-==-==+
 |       flashPash PHP firefly include        |
 +==-==-==-==-==-==-==-==-==-==-==-==-==-==-==+
 |              version 1.0 *RC1*             |
 |                                            |
 | last update on 2003-06-17 by si*sshnug.com |
 |                                            |
 |   Copyright (c) 2002-2003, Simon Mckenna   |
 |            All rights reserved             |
 +==-==-==-==-==-==-==-==-==-==-==-==-==-==-==*/

/* static properties for firefly. The only ones cybersage use in XML seem to be field attributes */
define( 'FP_FIREFLY_DATASET_MAP',          'map' );
define( 'FP_FIREFLY_DATASET_FIELD',        'field' );
define( 'FP_FIREFLY_FIELD_INDEX',          'index' );
define( 'FP_FIREFLY_FIELD_NAME',           'name' );
define( 'FP_FIREFLY_FIELD_DISPLAY_FORMAT', 'displayFormat' );
define( 'FP_FIREFLY_FIELD_DATA_FORMAT',    'dataFormat' );  
define( 'FP_FIREFLY_FIELD_DEFAULT_VALUE',  'defaultValue' );
define( 'FP_FIREFLY_FIELD_PATH',           'path' );
define( 'FP_FIREFLY_FIELD_TYPE',           'type' );           // string, numeric, money, boolean, date, dateTime, time
/* static properties for firefly dataset used for doMeta method */
define( 'FP_FIREFLY_FIELD_LABEL',          'label' );
define( 'FP_FIREFLY_FIELD_CONTROL_SYMBOL', 'controlSymbol' );  // FxEditBoxSymbol, FxComboBoxSymbol, FxLookupBoxSymbol, FxCheckBoxSymbol...more to come
define( 'FP_FIREFLY_FIELD_SIZE',           'size' );
define( 'FP_FIREFLY_FIELD_DISPLAY_WIDTH',  'displayWidth' );
define( 'FP_FIREFLY_FIELD_READ_ONLY',      'readOnly' );       // true, false
define( 'FP_FIREFLY_FIELD_ALIGNMENT',      'alignment' );      // left, center, right
define( 'FP_FIREFLY_FIELD_KIND',           'kind' );           // 0=Data, 1=calculated, 2=Lookup
define( 'FP_FIREFLY_FIELD_PRECISION',      'precision' );
/* static properties for firefly field types */
define( 'FP_FIREFLY_FIELD_TYPE_STRING',    'string' );
define( 'FP_FIREFLY_FIELD_TYPE_NUMERIC',   'numeric' );
define( 'FP_FIREFLY_FIELD_TYPE_MONEY',     'money' );
define( 'FP_FIREFLY_FIELD_TYPE_BOOLEAN',   'boolean' );
define( 'FP_FIREFLY_FIELD_TYPE_DATE_TIME', 'dateTime' );
define( 'FP_FIREFLY_FIELD_TYPE_DATE',      'date' );
define( 'FP_FIREFLY_FIELD_TYPE_TIME',      'time' );
/* static properties for firefly field controls */
define( 'FP_FIREFLY_CONTROL_SYMBOX_BOX',       'FxEditBoxSymbol' );
define( 'FP_FIREFLY_CONTROL_SYMBOX_COMBO',     'FxComboBoxSymbol' );
define( 'FP_FIREFLY_CONTROL_SYMBOX_LOOKUP',    'FxLookupBoxSymbol' );
define( 'FP_FIREFLY_CONTROL_SYMBOX_CHECK_BOX', 'FxCheckBoxSymbol' );

function _buildFireflyMetaResult( &$fp, &$tree, &$dataPacketElement )
{
	$mapElement =& $dataPacketElement->addChild( FP_FIREFLY_DATASET_MAP );

	/* loop through all fields in first row to build field map */
	$fieldCount = $fp->_result->FieldCount();
	if ( $fieldCount == 0 )
		return $fp->_logError( FP_ERROR_META, 'no fields returned from query', true );

	for ( $colPos = 0; $colPos < $fieldCount; $colPos++ )
	{
		$fieldObject = $fp->_result->FetchField( $colPos );
		$fp->_debugLog( 'meta-data', $fieldObject );

		/* have to extract table and field names from meta-data, extraction depends on db meta abilities */
		if ( strpos( strtoupper( $fp->_SQL[ $fp->_qri ] ), FP_SQL_ALIAS ) === false )
		{
			if ( !empty( $fieldObject->table ))
				$tableName = $fieldObject->table;
			else
				return $fp->_logError( FP_ERROR_META, 'unable to extact table name from field', true, $fieldObject->name );
			$fieldName = $fieldObject->name;
		}
		else
		{
			$tableName = substr( $fieldObject->name, 0, strpos( strtoupper( $fieldObject->name ), FP_SQL_ALIAS ));
			$fieldName = substr( $fieldObject->name, strpos( strtoupper( $fieldObject->name ), FP_SQL_ALIAS ) + strlen( FP_SQL_ALIAS ));
		}

		$attributes = array();

		$attributes[ FP_FIREFLY_FIELD_INDEX ] = $colPos + 1; /* Firefly index starts from 1, not 0 */
		$attributes[ FP_FIREFLY_FIELD_NAME ]  = $fieldName;
		$attributes[ FP_FIREFLY_FIELD_LABEL ] = $fieldName;

		/* To-Do: auto-increment type fields set to read only? */
		$attributes[ FP_FIREFLY_FIELD_READ_ONLY ] = 'false';

		/* To-Do: displayWidth could be calculated by fieldLength * ??? */
		$attributes[ FP_FIREFLY_FIELD_DISPLAY_WIDTH ]  = $fieldObject->max_length;

		switch ( $fp->_result->MetaType( $fieldObject->type ))
		{
			case FP_ADODB_CHARACTER : case FP_ADODB_CLOB      : 
				/* To-Do should we only set defaultValue when not_null in flags found? */
				$attributes[ FP_FIREFLY_FIELD_CONTROL_SYMBOL ] = FP_FIREFLY_CONTROL_SYMBOX_BOX;
				$attributes[ FP_FIREFLY_FIELD_DEFAULT_VALUE ]  = '?';
				$attributes[ FP_FIREFLY_FIELD_ALIGNMENT ]      = 'left';
				$attributes[ FP_FIREFLY_FIELD_TYPE ]           = FP_FIREFLY_FIELD_TYPE_STRING;
				$attributes[ FP_FIREFLY_FIELD_SIZE ]           = $attributes[ FP_FIREFLY_FIELD_DISPLAY_WIDTH ];
				break;

			/* ::To-Do:: should we only set defaultValue when not_null in flags found? */
			/* ::To-Do:: we should also set default value based upon precision */
			case FP_ADODB_NUMERIC :
				$attributes[ FP_FIREFLY_FIELD_CONTROL_SYMBOL ] = FP_FIREFLY_CONTROL_SYMBOX_BOX;
				$attributes[ FP_FIREFLY_FIELD_DEFAULT_VALUE ]  = '0';
				$attributes[ FP_FIREFLY_FIELD_PRECISION ]      = '2';     // Needs to reflect numeric type and flags
				$attributes[ FP_FIREFLY_FIELD_ALIGNMENT ]      = 'right';
				$attributes[ FP_FIREFLY_FIELD_TYPE ]           = FP_FIREFLY_FIELD_TYPE_NUMERIC;
				$attributes[ FP_FIREFLY_FIELD_SIZE ]           = 0;
				break;

			case FP_ADODB_INTEGER :
				$attributes[ FP_FIREFLY_FIELD_CONTROL_SYMBOL ] = FP_FIREFLY_CONTROL_SYMBOX_BOX;
				$attributes[ FP_FIREFLY_FIELD_DEFAULT_VALUE ]  = '0';
				$attributes[ FP_FIREFLY_FIELD_PRECISION ]      = '0';     // Needs to reflect numeric type and flags
				$attributes[ FP_FIREFLY_FIELD_ALIGNMENT ]      = 'right';
				$attributes[ FP_FIREFLY_FIELD_TYPE ]           = FP_FIREFLY_FIELD_TYPE_NUMERIC;
				$attributes[ FP_FIREFLY_FIELD_SIZE ]           = 0;
				break;

			case FP_ADODB_BOOLEAN : /* Logical field (boolean or bit-field) */
				$attributes[ FP_FIREFLY_FIELD_CONTROL_SYMBOL ] = FP_FIREFLY_CONTROL_SYMBOX_CHECK_BOX;
				$attributes[ FP_FIREFLY_FIELD_TYPE ]           = FP_TYPE_BOOLEAN;
				$attributes[ FP_FIREFLY_FIELD_DISPLAY_FORMAT ] = 'false,true';
				$attributes[ FP_FIREFLY_FIELD_DATA_FORMAT ]    = '0,1';
				$attributes[ FP_FIREFLY_FIELD_SIZE ]           = 0;
				break;

			case FP_ADODB_DATE : /* */
				$attributes[ FP_FIREFLY_FIELD_TYPE ]           = FP_FIREFLY_FIELD_TYPE_DATE;
				$attributes[ FP_FIREFLY_FIELD_DISPLAY_FORMAT ] = 'DD-MM-YYYY'; /* should this reflect user or o/s or adodb setting? */
				$attributes[ FP_FIREFLY_FIELD_DATA_FORMAT ]    = 'YYYY-MM-DD';
				$attributes[ FP_FIREFLY_FIELD_SIZE ]           = 0;
				break;

			case FP_ADODB_TIMESTAMP :
				/* To-Do: need to work out from default if it's datetime or just time */
				$attributes[ FP_FIREFLY_FIELD_TYPE ]           = FP_FIREFLY_FIELD_TYPE_DATE_TIME;
				$attributes[ FP_FIREFLY_FIELD_DISPLAY_FORMAT ] = 'DD-MM-YYYY HH:NN:SS'; /* should this reflect user or o/s or adodb setting? */
				$attributes[ FP_FIREFLY_FIELD_DATA_FORMAT ]    = 'YYYY-MM-DD HH:NN:SS'; 
				$attributes[ FP_FIREFLY_FIELD_SIZE ]           = 0;
				break;
		}

		if ( strtolower( $fp->createNodes ) == 'true' )
			$pathDelimiter = "/";
		else
			$pathDelimiter = "/@";

		$attributes[ FP_FIREFLY_FIELD_PATH ] = "*/" . $tableName . $pathDelimiter . $fieldName;

		/* This will always be 0 (Data) */
		$attributes[ FP_FIREFLY_FIELD_KIND ] = '0';

		/* To-Do: Implement primary key attribute to make updates/deletes snazzy */
		if (( isset( $fieldObject->primary_key ) && ( $fieldObject->primary_key )) ||
			  ( isset( $fieldObject->multiple_key ) && ( $fieldObject->multiple_key )))
			$attributes[ FP_FIELD_KEY ] = 'true'; // Note that we are currently using flashPash static property, this should change if cybersage introduce a key property

		$fieldElement =& $mapElement->addChild( FP_FIREFLY_DATASET_FIELD, NULL, $attributes );
	}
	return true;
}

function _convertFireflyFieldType( &$fp, $type )
{
	switch ( $type )
	{
		/* To-Do: Maybe we have to be smarter and work out later on the field length to determine if strings should be Char or CLOB */
		case FP_FIREFLY_FIELD_TYPE_STRING    : return FP_ADODB_CHARACTER;
		case FP_FIREFLY_FIELD_TYPE_NUMERIC   : return FP_ADODB_NUMERIC;
		case FP_FIREFLY_FIELD_TYPE_MONEY     : return FP_ADODB_NUMERIC;
		case FP_FIREFLY_FIELD_TYPE_BOOLEAN   : return FP_ADODB_BOOLEAN;
		case FP_FIREFLY_FIELD_TYPE_DATE_TIME : return FP_ADODB_TIMESTAMP;
		case FP_FIREFLY_FIELD_TYPE_TIME      : return FP_ADODB_TIMESTAMP;
		case FP_FIREFLY_FIELD_TYPE_DATE      : return FP_ADODB_DATE;
		default : return $fp->_logError( FP_ERROR_PARSING_XML, 'invalid XML', true, 'unknown firefly field type ' . $type );
	}
}

?>
Return current item: flashPash