Location: PHPKode > scripts > Report Generator > report-generator/Reporte.class.php
<?php
/********************************************************************************************
** 	Autor		:	Andrés Darío Gutiérrez Poveda.			          																*
** 	Fecha  	:	Septiembre 8 de 2004.	         				   	          													*
** 	Versión	:	0.0.1. 	                    			   	          															*
**	Empresa	:	Atila Servicios S.A. 			                      															*
**																																													*
*********************************************************************************************
**	Proposito:																																							*
**																																													*
** 		Esta clase se ocupa de hacer un formulario de una tabla, teniendo en cuenta los tipos	*
**		de cada campo de ésta.	Además es capaz de hacer la validación de dichos campos.			*
**		Realiza las adiciones y actualizaciones en dichas tablas.															*
**																																													*
*********************************************************************************************
**	Version 1.0.1  -  																																			*
** 	Cambios para la versión 1.0.1																														*
**																																													*
** 		- 																																										*
********************************************************************************************/  

class Reporte{

	/*******************************************************************************************
 	** 	ATTRIBUTES																																						**
  *******************************************************************************************/
	var $reporte_nombre;
	var $reporte_link;
	var $reporte_tabla;
	var $reporte_campos;
	var $reporte_condicion;
	var $reporte_ordenar;
	var $reporte_encabezados;
	var $reporte_normalizado;
	var $reporte_colores;
	var $reporte_feedback;

	/*******************************************************************************************
 	** 	CONSTANTS																																							**
  *******************************************************************************************/
	  
  
  /*******************************************************************************************
 	** 	METHODS																																								**
  *******************************************************************************************/
  /*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_nombre attribute.														**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_nombre(){
		return $this->reporte_nombre;
	} 
	
	function set_reporte_nombre($value){
		$this->reporte_nombre = $value;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_link attribute.															**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_link(){
		return $this->reporte_link;
	} 
	
	function set_reporte_link($value){
		$this->reporte_link = $value;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_tabla attribute.														**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_tabla(){
		return $this->reporte_tabla;
	} 
	
	function set_reporte_tabla($value){
		$this->reporte_tabla = $value;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_campos attribute.														**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_campos(){
		return $this->reporte_campos;
	} 
	
	function set_reporte_campos($value){
		$this->reporte_campos = $value;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_condicion attribute.												**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_condicion(){
		return $this->reporte_condicion;
	} 
	
	function set_reporte_condicion($value){
		$this->reporte_condicion = $value;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_ordenar attribute.													**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_ordenar(){
		return $this->reporte_ordenar;
	} 
	
	function set_reporte_ordenar($value){
		$this->reporte_ordenar = $value;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_encabezados attribute.											**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_encabezados(){
		return $this->reporte_encabezados;
	} 
	
	function set_reporte_encabezados($campos,$encabezados){
		if(sizeof($campos) != sizeof($encabezados)) 
    	return 0;
    
    for($i = 0;$i < sizeof($campos); $i++)	    
    	$this->reporte_encabezados[$campos[$i]] = $encabezados[$i];
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_colores attribute.													**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- top: Color for the name of the report.																**
 	**	-	middle: Color for the header of the table.														**
 	**	- bottom: Color for the left panel of the table. Not ready yet!!				**
 	**	- font: Color for the font of the header of the table.									**
 	*****************************************************************************/
	function get_reporte_colores(){
		return $this->reporte_colores;
	} 
	
	function set_reporte_colores($top,$middle,$bottom,$font){
		$this->reporte_colores['top'] = $top;
		$this->reporte_colores['middle'] = $middle;
		$this->reporte_colores['bottom'] = $bottom;
		$this->reporte_colores['font'] = $font;
	}
	
	
	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	SET and GET for the reporte_feedback attribute.													**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Methods to set and get the value of the attribute.											**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	**	- value: value to set.																									**
 	*****************************************************************************/
	function get_reporte_feedback(){
		return $this->reporte_feedback;
	} 
	
	function set_reporte_feedback($value){
		$this->reporte_feedback = $value;
	}
	
	
	/*****************************************************************************
  ** 	NAME:																																		**
  **																																					**
  ** 	normalizar_referencia ($campo_referecia, $tabla, $llave_primaria, 			**
  **													$campo_contenido,$ordenar = "value ASC")				**
  **																																					**
  ******************************************************************************
  ** 	OBJETIVE:																															 	**
  **																																					**	
  ** 	Normalized data for the table.																					**
  **																																					**
  ******************************************************************************
  ** 	PARAMS:			 																														**
  **																																					**
  **	- campo_referencia: Name of the field that references other table.			**
  **	- tabla: Name of the table that it's being referenced.									**
  **	- llave_primaria:	Primary Key of the referenced table.									**
  **	- campo_contenido: Field in the referenced table to show.								**
  **	-	ordenar: Way to sort data.																						**
  *****************************************************************************/
  function normalizar_referencia($campo_referencia,$tabla,$llave_primaria,$campo_contenido,$ordenar = "value ASC"){
    $this->reporte_normalizado[$campo_referencia]['nombre'] = $campo_referencia;	
    $this->reporte_normalizado[$campo_referencia]['tabla'] = $tabla;	
    $this->reporte_normalizado[$campo_referencia]['llave'] = $llave_primaria;	
    $this->reporte_normalizado[$campo_referencia]['contenido'] = $campo_contenido;	
    $this->reporte_normalizado[$campo_referencia]['ordenar'] = $ordenar;	
  }
  
  
 	/*****************************************************************************
 	** 	NAME:																																		**
  **																																					**
 	** 	Reporte ()																															**
  **																																					**
  ******************************************************************************
 	** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Constructor.																														**
 	**																																					**
 	******************************************************************************
 	**	PARAMS:																																	**
 	**																																					**
 	*****************************************************************************/  
 	function Reporte(){
    if(stristr(getenv("HTTP_USER_AGENT"),"Mozilla/4") && !stristr(getenv("HTTP_USER_AGENT"),"compatible"))
      $this->NS4 = true; 
    else 
    	$this->NS4 = false;  
  }
  
  
  /*****************************************************************************
  ** 	NAME:																																		**
  **																																					**
  ** 	_putLabel ($campo,$encabezado,$focus)																		**
  **																																					**
  ******************************************************************************
  ** 	OBJETIVE:																															 	**
  **																																					**	
  ** 	This function puts the label of each field.														 	**
  **																																					**
  ******************************************************************************
  ** 	PARAMS:			 																														**
  **																																					**
  ** 	- campo: Name of the field.																						 	**
  **	- encabezado: Label for the field.																			**
  **	- focus: Flag in case of focus.																					**
  *****************************************************************************/
  function _colocar_encabezado($campo,$encabezado,$focus){
    if($focus) 
    	$onclick = " onClick=\"forms['$this->reporte_nombre'].$campo.select();\"";
    if(!$this->NS4)
    	return "<label for=\"$campo\"$onclick><font size=2>".$encabezado."</font></label>";
    else
    	return "<label for=\"$campo\"><font size=2>".$encabezado."</font></label>";
  }
  
  
  /*****************************************************************************
  ** 	NAME:																																		**
  **																																					**
  ** 	_get_campo_datos ($campo)																								**
  **																																					**
  ******************************************************************************
  ** 	OBJETIVE:																															 	**
  **																																					**
  ** 	Retrieve the data from another table and field.													**
  **																																					**
  ******************************************************************************
  ** 	PARAMS: 																																**
  **																																					**
  **	- campo: Name of the field.																							**
  *****************************************************************************/
  function _get_campo_datos($campo){
    $sql = "SELECT "
    				.$this->reporte_normalizado[$campo]['llave']
    				." AS llavep, "
    				.$this->reporte_normalizado[$campo]['contenido']
    				." AS valor FROM "
    				.$this->reporte_normalizado[$campo]['tabla']
    				." ORDER BY "
    				.$this->reporte_normalizado[$campo]['ordenar'];
                  
    $result = @mysql_query($sql,$this->reporte_link);
    if(@mysql_error() != "") die ("No fue posible normalizar los datos de '".$this->reporte_normalizado[$campo]['tabla']."'.");
      
    $num_registros = @mysql_num_rows($result);
    $this->reporte_normalizado[$campo]['registros'] = $num_registros;
      
    for($i=0; $i<$num_registros; $i++){
      $row = @mysql_fetch_assoc($result);
      $this->reporte_normalizado[$campo]['llaves'][$i] = $row['llavep'];
      $this->reporte_normalizado[$campo]['valor'][$i] = $row['valor'];
    }  
  }
  
  
  /*****************************************************************************
  ** 	NAME:																																		**
  **																																					**
  ** 	get_prereporte ()																												**
  **																																					**
  ******************************************************************************
  ** 	Objective:																														 	**
  **																																					**
  ** 	This function creates the form with the choices for the report.					**
  **																																					**
  ******************************************************************************
  ** 	PARAMS:			 																														**
  **																																					**
  *****************************************************************************/
  function get_prereporte(){
  	$prereporte  = "<form name='$this->reporte_nombre' method='POST'>";
    $prereporte .= "<table class='grid' align=center>";
    
    foreach($this->reporte_normalizado as $campos){
    	$campo = $campos['nombre'];
    	
    	if(isset($this->reporte_encabezados[$campo])) 
	      $encabezado = $this->reporte_encabezados[$campo];
	    else 
	      $encabezado = $campo;
	    
	    $prereporte .= "<tr><td>";
	    $prereporte .= $this->_colocar_encabezado($campo,$encabezado,false);
	    $prereporte .= "</td><td>";
	    $this->_get_campo_datos($campo);
	    $prereporte .= "<select name='".$campo."' id='".$campo."' size=1 class='select'>";
	    $prereporte .= "<option value='0'>Todos</option>";
	    for($i = 0;$i < $this->reporte_normalizado[$campo]['registros']; $i++)
	    	$prereporte .= "<option value='".$this->reporte_normalizado[$campo]['llaves'][$i]."'>"
	    								.$this->reporte_normalizado[$campo]['valor'][$i]."</option>";
	    $prereporte .= "</select>";
	    $prereporte .= "</td></tr>";
		}
		
		$prereporte .= "<tr align=center><td colspan=2><input type='submit' name='submit' value='Enviar' class='button'></td></tr></table></form>";
		
		return $prereporte;
  }
  
  
  /*****************************************************************************
  ** 	NAME:																																		**
  **																																					**
  ** 	_arreglar_sentencia ()																									**
  **																																					**
  ******************************************************************************
  ** 	Objective:																														 	**
  **																																					**
  ** 	This function creates the sql sentence to retrieve the data requested.	**
  **																																					**
  ******************************************************************************
  ** 	PARAMS:			 																														**
  **																																					**
  *****************************************************************************/
  function _arreglar_sentencia(){
  	$sql = "SELECT ".$this->reporte_campos." FROM ".$this->reporte_tabla." WHERE ".$this->reporte_condicion;
  	foreach($_POST as $key=>$value){
  		if(($key != "submit") && ($value > 0)) 
	    	$sql .= " AND ".$key." = ".$value;
  	}
  	$sql .= " ORDER BY ".$this->reporte_ordenar;
  	
  	return $sql;
  }
  
  
  /*****************************************************************************
  ** 	NAME:																																		**
  **																																					**
  ** 	get_reporte ()																													**
  **																																					**
  ******************************************************************************
  ** 	Objective:																														 	**
  **																																					**
  ** 	This function creates the report with the conditions selected in the 		**
  **	prereporte.																															**
  **																																					**
  ******************************************************************************
  ** 	PARAMS:			 																														**
  **																																					**
  *****************************************************************************/
  function get_reporte(){
  	$sql = $this->_arreglar_sentencia();
  	$result = mysql_query($sql,$this->reporte_link);
  	
  	if(mysql_num_rows($result) > 0){
  		$ya = false;
  		$camposA = explode(",",$this->reporte_campos);
	  	
	  	$tablaini  = "<p align=center><font size='+2' color=".$this->reporte_colores['top']."><strong>".$this->reporte_nombre."</strong></font></p>";
	  	$tablaini .= "<table align=center border=1 bordercolor=".$this->reporte_colores['middle']." cellspace=1 class='grid'>";
	  	while($row = mysql_fetch_array($result)){
	  		$cuerpo .= "<tr>";
	  		
	  		foreach($camposA as $nombre){
	  			if(!$ya){
	  				$encabezado .= "<td align=center bgcolor=".$this->reporte_colores['middle']."><font color=".$this->reporte_colores['font']."><strong>";
		  			if(isset($this->reporte_encabezados[$nombre])) 
				      $encabezado .= $this->reporte_encabezados[$nombre];
				    else 
				      $encabezado .= $campo;
				    $encabezado .= "</strong></font></td>";
				  }
	  			$cuerpo .= "<td>".$row[$nombre]."</td>";
	  		}
	  		
	  		$ya = true;
	  		$cuerpo .= "</tr>";
	  	}
	  	$tablafin = "</table>";
	  	
	  	$reportefinal = $tablaini.$emp2."<tr>".$encabezado."</tr>".$cuerpo.$tablafin;
	  }else
	  	$this->reporte_feedback = "No se encontró nada!";
	  	
	  return $reportefinal;
  }
}
?>
Return current item: Report Generator