Location: PHPKode > scripts > RFKmap > rfkmap/class.rfkmap.php3
<?php
/*
   RFKmap v1.4 - A PHP extension for mapping database queries to templates 
   Copyright (C) 2001 RFKsolutions <hide@address.com>

   This program 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.

   This program 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.
*/

if( !defined( "_RFKmap_Type_v1_4_" ) ):
	define( "_RFKmap_Type_v1_4_" , 1 );

	class RFKmap {
		var $classname = "RFKmap";
		var $dbs_class = "DBCORE";
		var $dbs;
		var $table_typedefinition = "TypeDefinition";
		var $table_tabledefinition = "TableDefinition";
		var $table_typetemplates = "TemplatesDefinition";
		var $guidinstance;			/* identificador de la instancia */
		var $guid;					/* identificador del objeto */
		var $guid_name;				/* nombre identificador del objeto */
		var $dbunique_table;		/* tabla principal */
		var $dbmultiple_table;		/* primera tabla secundaria de uso 1..N */
		var $dbrelation_table;		/* segunda tabla secundaria de uso 1..N */
		var $dbunique_primary;		/* primary key de todas las tablas */
		var $dbmultiple_select;		/* selector de la primera tabla secundaria */
		var $dbrelation_select;		/* selector de la segunda tabla secundaria */
		var $arr_table_fields;		/* tablas y campos del objeto */
		var $arr_vars;				/* tablas y campos del objeto */

/* ================================================================================
Función: 
	Contructor de la clase
Parametros:
	tpl			- Variable de template del proyecto
	typename	- Nombre del tipo a usar
	arrdefine	- (opcional) Redefinición de los templates de visualización
================================================================================== */
		function RFKmap ( &$tpl, $typename, $arrdefine="" ) {
			eval( "\$this->dbs = new " . $this->dbs_class . ";" );
			$this->guid_name = strtolower($typename);
			$timetmp = microtime();
			$this->guidinstance = trim(strrchr($timetmp," ")). substr($timetmp,2,8);

			$query = "SELECT * FROM ". $this->table_typedefinition ." WHERE typename = '". $this->guid_name. "'";
			$this->dbs->query( $query );
			$this->dbs->next_record();
			$this->guid = $this->dbs->f( "guid" );

			/* v1.3.1 Add-Start*/
			$query = "SELECT * FROM ". $this->table_typetemplates ." WHERE guid = '". $this->guid ."'";
			$this->dbs->query( $query );
			$this->dbs->next_record();

			if (isset($arrdefine["view_list"])) $formato = $arrdefine["view_list"];
			else $formato =  $this->dbs->f("view_list");
			$tpl->define( array( "view_list_" . $this->guid . "_" . $this->guidinstance => stripslashes(strtolower("$formato")) ) );

			if (isset($arrdefine["view_item"])) $formato = $arrdefine["view_item"];
			else $formato = $this->dbs->f("view_item");
			$tpl->define( array( "view_item_" . $this->guid . "_" . $this->guidinstance => stripslashes(strtolower("$formato")) ) );

			if (isset($arrdefine["view_only"])) $formato = $arrdefine["view_only"];
			else $formato = $this->dbs->f("view_only");
			$tpl->define( array( "view_only_" . $this->guid . "_" . $this->guidinstance => stripslashes(strtolower("$formato")) ) );

			/* crea las definiciones y formas de acceso de las tablas de base de datos */
			$query = "SELECT * FROM ". $this->table_tabledefinition ." WHERE guid = '". $this->guid ."'";
			$this->dbs->query( $query );
			$this->dbs->next_record();
			$this->dbunique_table = $this->dbs->f("unique_table");
			$this->dbmultiple_table = $this->dbs->f("multiple_table");
			$this->dbrelation_table = $this->dbs->f("relation_table");
			$this->dbunique_primary = $this->dbs->f("unique_primary");
			$this->dbmultiple_select = $this->dbs->f("multiple_select");
			$this->dbrelation_select = $this->dbs->f("relation_select");

			$this->arr_table_fields = array();
			$this->arr_vars = array();
			/* busco nombre de los campos de $dbunique_table */
			if (strlen($this->dbunique_table)) {
				$query = "SHOW FIELDS FROM ". $this->dbunique_table;
				$this->dbs->query($query);
				while ($this->dbs->next_record()) $this->arr_table_fields[] = array("type"=>"u", "table" => $this->dbunique_table, "field" => $this->dbs->f("Field"));
			}
			/* busco nombre de los campos de $dbmultiple_table */
			if (strlen($this->dbmultiple_table)) {
				$query = "SHOW FIELDS FROM ". $this->dbmultiple_table;
				$this->dbs->query($query);
				while ($this->dbs->next_record()) $this->arr_table_fields[] = array("type"=>"p", "table" => $this->dbmultiple_table, "field" => $this->dbs->f("Field"));
			}
			/* busco nombre de los campos de $dbrelation_table */
			if (strlen($this->dbrelation_table)) {
				$query = "SHOW FIELDS FROM ". $this->dbrelation_table;
				$this->dbs->query($query);
				while ($this->dbs->next_record()) $this->arr_table_fields[] = array("type"=>"r", "table" => $this->dbrelation_table, "field" => $this->dbs->f("Field"));
			}
		}

/* ================================================================================
Función: 
	Crea la query
Parametros:
	id_unique	- Identificador de selección de un item
	id_multiple	- (opcional) Selector de la tabla multiple
	id_relation - (opcional) Selector de la tabla relaciones
	query_plan	- (opcional) Más parámetros de la query
================================================================================== */
		function internal_create_query( $id_unique, $id_multiple, $id_relation, $query_plus ) {
			$dbunique = $this->dbunique_table;
			$dbmultiple = $this->dbmultiple_table;
			$dbrelation = $this->dbrelation_table;
			$dbunique_primary = $this->dbunique_primary;
			$dbmultiple_selection = $this->dbmultiple_select;
			$dbrelation_selection = $this->dbrelation_select;
			$query = "SELECT * FROM $dbunique AS u";
			if ( strlen($dbmultiple) ) $query.= ", $dbmultiple AS p";
			if ( strlen($dbrelation) ) $query.= ", $dbrelation AS r";
			$query.= " WHERE ";
			if ( strlen($dbmultiple) ) $query.= "u.$dbunique_primary = p.$dbunique_primary AND ";
			if ( strlen($dbrelation) ) $query.= "u.$dbunique_primary = r.$dbunique_primary AND ";
			if ( strlen($id_unique) )$query.= "u.$dbunique_primary = '$id_unique' AND ";
			if ( (strlen($dbmultiple_selection)) && (strlen($id_multiple)) ) $query.= "p.$dbmultiple_selection='$id_multiple' AND " ;
			if ( (strlen($dbrelation_selection)) && (strlen($id_relation)) ) $query.= "r.$dbrelation_selection='$id_relation' AND " ;
			$query.= " 1=1 ";
			if ( strlen($query_plus) ) $query.= "$query_plus";
			return $query;
		}

/* ================================================================================
Función: 
	Vista de un sólo tipo
Parametros:
	tpl			- Variable de template del proyecto
	id_unique	- Identificador de selección de un item
	id_multiple	- (opcional) Selector de la tabla multiple
	id_relation - (opcional) Selector de la tabla relaciones
	query_plan	- (opcional) Más parámetros de la query
================================================================================== */
		function view_only ( &$tpl, $id_unique, $id_multiple="", $id_relation="", $query_plus="" ) {
			$query = $this->internal_create_query( $id_unique, $id_multiple, $id_relation, $query_plus );
			$this->dbs->query($query);
			$total = $this->dbs->nf();
			$this->dbs->next_record();
			reset($this->arr_table_fields);
			while (list($key, $val)=each($this->arr_table_fields)) {
				$this->arr_vars[] = strtoupper($this->guid_name . "_" . $val["table"] . "_" . $val["field"] );
				$tpl->assign(
					strtoupper($this->guid_name . "_" . $val["table"] . "_" . $val["field"] . "_" . $this->guidinstance ),
					stripslashes( $this->dbs->f($val["field"]) )
				);
			}
			return $total;
		}

/* ================================================================================
Función: 
	Fetch del objeto para asignación a variable
Parametros:
	tpl		- Variable de template del proyecto
================================================================================== */
		function view_only_fetch( &$tpl ) {
			reset($this->arr_vars);
			while (list($key, $val)=each($this->arr_vars)) {
				$tpl->assign( $val, $tpl->PARSEVARS[ $val."_".$this->guidinstance ] );
			}
			$timetmp = microtime();
			$timetmp_tmp = $this->guid . "_" . trim(strrchr($timetmp," ")). substr($timetmp,2,8);
			$tpl->parse( $timetmp_tmp, ".view_only_" . $this->guid . "_" . $this->guidinstance );
			$internal_var = $tpl->fetch( $timetmp_tmp );
			return $internal_var;
		}

/* ================================================================================
Función: 
	Evalua el objeto para asignación a variable de salida $output
Parametros:
	tpl		- Variable de template del proyecto
	name	- Variable de PARSE opcional
================================================================================== */
		function view_only_eval( &$tpl, $name="EVALPHP" ) {
			reset($this->arr_vars);
			while (list($key, $val)=each($this->arr_vars)) {
				$tpl->assign( $val, $tpl->PARSEVARS[ $val."_".$this->guidinstance ] );
			}
			$timetmp = microtime();
			$timetmp_tmp = $this->guid . "_" . trim(strrchr($timetmp," ")). substr($timetmp,2,8);
			$tpl->parse( $timetmp_tmp, ".view_only_" . $this->guid . "_" . $this->guidinstance );
			eval ( $tpl->fetch( $timetmp_tmp ) );
			$tpl->assign( $name, "$output" );
		}

/* ================================================================================
Función: 
	Parser del objeto
Parametros:
	tpl		- Variable de template del proyecto
	name	- (opcional) Redefinición de la variable de parser
================================================================================== */
		function view_only_parse( &$tpl, $name="" ) {
			if (strlen($name)) $varparser = strtoupper($name);
			else $varparser = strtoupper($this->guid_name);
			reset($this->arr_vars);
			while (list($key, $val)=each($this->arr_vars)) $tpl->assign( $val, $tpl->PARSEVARS[ $val."_".$this->guidinstance ] );
			$tpl->parse( $varparser, ".view_only_" . $this->guid . "_" . $this->guidinstance );
		}

/* ================================================================================
Función: 
	Listado de items
Parametros:
	tpl			- Variable de template del proyecto
	id_unique	- Identificador de selección de un item
	id_multiple	- (opcional) Selector de la tabla multiple
	id_relation - (opcional) Selector de la tabla relaciones
	query_plan	- (opcional) Más parámetros de la query
================================================================================== */
		function view_list ( &$tpl, $id_unique, $id_multiple="", $id_relation="", $query_plus="" ) {
			$query = $this->internal_create_query( $id_unique, $id_multiple, $id_relation, $query_plus );
			$this->dbs->query($query);
			$varparser = strtoupper($this->guid_name . "_" . "LIST");
			$tpl->clear( $varparser );
			$cont_interno = 0;
			while ($this->dbs->next_record()) {
				reset($this->arr_table_fields);
				while (list($key, $val)=each($this->arr_table_fields)) {
					$tpl->assign( strtoupper($this->guid_name . "_" . $val["table"] . "_" . $val["field"]), stripslashes( $this->dbs->f($val["field"]) ) );
				}
				$tpl->assign( strtoupper($this->guid_name . "_ID"), "$cont_interno" );
				$cont_interno++;
				$tpl->parse( $varparser, ".view_item_" . $this->guid . "_" . $this->guidinstance );
			}
			return $cont_interno;
		}

/* ================================================================================
Función: 
	Parser del objeto listado
Parametros:
	tpl		- Variable de template del proyecto
	name	- (opcional) Redefinición de la variable de parser
================================================================================== */
		function view_list_parse( &$tpl, $name="" ) {
			if (strlen($name)) $varparser = strtoupper($name);
			else $varparser = strtoupper($this->guid_name);
			$tpl->parse( $varparser, ".view_list_" . $this->guid . "_" . $this->guidinstance );
		}

/* ================================================================================
Función: 
	Fetch del objeto para asignación a variable
Parametros:
	tpl		- Variable de template del proyecto
================================================================================== */
		function view_list_fetch( &$tpl ) {
			$timetmp = microtime();
			$timetmp_tmp = $this->guid . "_" . trim(strrchr($timetmp," ")). substr($timetmp,2,8);
			$tpl->parse( $timetmp_tmp, ".view_list_" . $this->guid . "_" . $this->guidinstance );
			$internal_var = $tpl->fetch( $timetmp_tmp );
			return $internal_var;
		}

		function get_assigned( &$tpl, $val ) {
			return $tpl->PARSEVARS[ $val."_".$this->guidinstance ] ;
		}

		function get_assigned_unique( &$tpl, $val ) {
			return $tpl->PARSEVARS[ $val ];
		}


	}
endif;
?>
Return current item: RFKmap