<?php
/*************************************************************
* The MyDB librairy and applications are product of SQLFusion
* It may be used and/or distributed under the terms of the Q Public
* License (QPL) version 1.0, enclosed in the file licence.txt.
****************************************************************/
/** MyDataLib Version 0.7 **/
/** Author Philippe Lewicki **/
/*
* Libreport.inc.php3
* Authors : Philippe et David pour SQLFusion
* Version 1.1.017
*/
/* SavedQuery
* Class used to get and do a query saved in the database.
*
*
*/
class SavedQuery {
var $id ;
var $table ;
var $qwhere ;
var $tbl_query = "savedquery";
var $qname ;
var $query ;
var $qorder ;
var $qpos ;
var $max_rows ;
function getquery($dbc) {
$query = "select * from $this->tbl_query where id".$this->tbl_query."='$this->id'" ;
$dbc->sql_query = $query ;
$dbc->sql_order = "";
$dbc->pos = 0 ;
$rquery = $dbc->query() ;
$infoquery = $dbc->fetch($rquery) ;
if(strlen($this->qname)==0) { $this->qname = $infoquery->qname ; }
if(strlen($this->query)==0) { $this->query = $infoquery->query ; }
if(strlen($this->qorder)==0) { $this->qorder = $infoquery->qorder ; }
if ($this->qpos==0) { $this->qpos = $infoquery->qpos ;}
return $infoquery ;
}
function doquery($dbc) {
/* Récupération du query à partir du numéro id du raport */
$infoquery = $this->getquery($dbc) ;
/* Execution du query sauvegardé */
$dbc->sql_query = $infoquery->query ;
while (ereg('\[([^\[]*)\]', $dbc->sql_query, $matches)) {
$field = $matches[1] ;
global $$field ;
$dbc->sql_query = eregi_replace('\['.$field.'\]', strval($$field), $dbc->sql_query) ;
}
if (strlen($this->qwhere) > 0 ) {
if (eregi("where", $dbc->sql_query)) {
$dbc->sql_query .= " AND ".$this->qwhere;
} else {
$dbc->sql_query .= " WHERE ".$this->qwhere;
}
}
$dbc->sql_order = $this->qorder ;
if ($this->qpos) {
$dbc->pos = $this->qpos ;
}
if ($this->max_rows) {
$dbc->max_rows = $this->max_rows ;
}
$reportdata = $dbc->query() ;
$infofield = mysql_fetch_field($reportdata);
$this->table = $infofield->table ;
return $reportdata ;
}
function gettablefield($dbc,$reportdata=false) {
if(!$reportdata) {
$table_def = mysql_db_query($dbc->db, "SHOW FIELDS FROM $this->table", $dbc->id);
for ($i=0;$i<mysql_num_rows($table_def);$i++) {
$row_table_def = mysql_fetch_array($table_def);
$field[$i] = $row_table_def["Field"];
}
reset($field) ;
return $field ;
} else {
for ($i=0;$i<mysql_num_fields($reportdata);$i++) {
$field[$i] = mysql_field_name($reportdata,$i);
}
reset($field) ;
return $field ;
}
}
}
/*
* Object Report
* Used to diplay the content of a query using the template report
* and a query id
*/
class Report {
var $id; /* unique id in the database of the report */
var $field ; /* array with all the field names */
var $idquery ; /* unique id in the database of the query */
var $header ;
var $row ;
var $footer ;
var $recprow = 0 ; /* Number of record per rows */
var $max_rows = 0 ; /* Numbers of rows to display */
var $caract ; /* variable temporaire en attendant la classe execregistry */
var $reg ; /* Object with all the value of the registry */
var $tbl_report = "report";
var $tbl_registry = "registry";
/* Get the report data from the database using the id
*
*/
function getreport($dbc) {
if (!($this->id)) { echo "Error Object Report need its id "; return ""; }
$query = "select * from $this->tbl_report where idreport='$this->id'" ;
$dbc->sql_query = $query ;
$dbc->sql_order = "";
$dbc->pos = "" ;
$rreport = $dbc->query();
$oreport = $dbc->fetch($rreport) ;
if (strlen($this->header)=="") { $this->header = $oreport->header; } //DS//
if (strlen($this->row) == "") { $this->row = $oreport->row ; } //DS//
if (strlen($this->footer) == "") { $this->footer = $oreport->footer; } //DS//
if ($this->recprow == 0) { $this->recprow = $oreport->recprow; }
if ($this->max_rows == 0) { $this->max_rows = $oreport->numrow; }
return $oreport ;
}
function getfield($template) {
while (ereg('\[([^\[]*)\]', $template, $fieldmatches)) {
// echo "\n<br>Dans boucle : ";
$field[] = $fieldmatches[1];
$template = str_replace($fieldmatches[0], "", $template) ;
}
return $field ;
}
/* reportusion
* Merge the string using the result of an array indexed on the attribut field
* It returns an array.
*/
function reportfusion($dbc, $row, $newrow) {
if (!(is_array($row))) {
$row = array(1=>"", 2=>"") ;
}
$nbrfield = count($this->field) ;
for($i=0; $i<$nbrfield; $i++) {
$field = $this->field[$i] ;
$reportdata = explode(":", $field) ;
$nbrdata = count($reportdata);
if ($nbrdata == 1) {
$replacedata = $this->reg->applyregistry($this->reg->table, $reportdata[0], "", $row[$reportdata[0]], $dbc) ;
$newrow = str_replace('['.$reportdata[0].']', $replacedata , $newrow) ;
// echo "<br><br>".$reportdata[0].": contenue : <br>".$newrow ;
} elseif ($nbrdata == 2) {
list ($namefield, $r) =explode(":", $field) ;
$replacedata = $this->reg->applyregistry($this->reg->table, $namefield, "", $row[$r][$namefield], $dbc) ;
$newrow = str_replace('['.$field.']', $replacedata , $newrow) ;
} else {
// echo "fonction: ".$field."<br>" ;
// echo "- ".$reportdata[0]($reportdata, $row, $dbc) ;
$newrow = str_replace('['.$field.']', $reportdata[0]($reportdata, $row, $dbc), $newrow) ;
}
}
$newrow = stripslashes($newrow) ;
// $newrow = str_replace('\\]', ']', $newrow) ;
return $newrow ;
}
function display($dbc) {
$htmloutput = "";
$oreport = $this->getreport($dbc) ;
if ($this->idquery>0) { $report_query_id = $this->idquery; } else { $report_query_id = $oreport->idquery; }
/** Get and do the query saved in the saved query **/
$squery = new SavedQuery;
$squery->id = $report_query_id ;
if ($oreport->numrow) { $dbc->max_rows = $this->max_rows; }
$rquery = $squery->doquery($dbc) ;
$this->field = $squery->gettablefield($dbc,$rquery) ;
/** Get the informations in the registry **/
$this->reg = new ExecRegistry;
$this->reg->table = $squery->table;
$this->reg->db_registry = $dbc->db;
$this->reg->getreg($dbc) ;
$htmloutput .= $this->reportfusion($dbc, 0, $this->header) ;
// while($record = $this->reg->fetch($rquery, $dbc) ) {
while($record = $dbc->fetcharray($rquery) ) {
if (is_array($record)) {
$htmloutput .= $this->reportfusion($dbc, $record, $this->row) ;
}
}
$htmloutput .= $this->reportfusion($dbc, 0, $this->footer) ;
return $htmloutput ;
} /* end function display */
} /* end class report */
class ReportTable extends Report {
var $pos; /* position of the record in the query, usefull for the navbarr */
var $numrows ; /* Total numbers of record result from the query */
var $cfgNavbarr = true ;
var $useSavedQuery = true ; /* Utilisation des savedquery si c'est a false utilise parametre dbc */
/* Nav bar string to show */
var $strPrevious = "-Précedant-";
var $strStart = "Debut-";
var $strEnd = "-Fin";
var $strNext = "-Suivant-" ;
function display($dbc) {
global $PHP_SELF, $REQUEST_URI ;
$file = basename($PHP_SELF) ;
list($fullfile, $vars) = explode("?", $REQUEST_URI) ;
//echo $vars ;
if (strlen($vars)>0) {
$vars = ereg_replace("&pos=[0-9]+", "", $vars) ;
$vars = "?".$vars."&" ;
} else { $vars = "?"; }
//echo "<br>".$vars ;
$htmloutput = "";
$oreport = $this->getreport($dbc) ;
if ($this->idquery>0) { $report_query_id = $this->idquery; } else { $report_query_id = $oreport->idquery; }
/** Get and do the query saved in the saved query **/
if ($this->useSavedQuery && $report_query_id) {
$squery = new SavedQuery;
$squery->id = $report_query_id ;
if ($this->max_rows) { $squery->max_rows = $this->max_rows; }
if ($this->pos) { $squery->qpos = $this->pos ; }
$rquery = $squery->doquery($dbc) ;
$querytable = $squery->table ;
} else {
$rquery = $dbc->result ;
$querytable = $dbc->table ;
}
$this->numrows = $dbc->getnum_rows() ;
// $this->field = $squery->gettablefield($dbc) ;
/** Get the informations in the registry **/
$this->reg = new ExecRegistry;
$this->reg->table = $querytable;
$this->reg->db_registry = $dbc->db;
$this->reg->getreg($dbc) ;
$this->field = $this->getfield($this->header) ;
$htmloutput .= $this->reportfusion($dbc, 0, $this->header) ;
// echo $this->recprow ;
$this->field = $this->getfield($this->row) ;
if($this->recprow) {
$numrec = 1 ;
$dispnumrows = 1;
$multiplrecord = array() ;
$anewfield = array() ;
$stilrecords = 1 ;
while($stilrecords && $this->max_rows > $dispnumrows) {
for ($r=0 ; $r<$this->recprow; $r++) {
if( $record[$numrec] = $dbc->fetcharray($rquery) ) {
$stilrecords = 1 ;
} else {
$stilrecords = 0 ;
}
$numrec++ ;
$dispnumrows++;
}
$htmloutput .= $this->reportfusion($dbc, $record, $this->row) ;
$numrec = 1 ;
}
} else {
while($record = $dbc->fetcharray($rquery) ) {
if (is_array($record)) {
$htmloutput .= $this->reportfusion($dbc, $record, $this->row) ;
}
}
}
$this->field = $this->getfield($this->footer) ;
if ($this->cfgNavbarr) {
$localstart = $vars."pos=0" ;
$prevnum = $this->pos - $this->max_rows ;
if ($prevnum <0) { $prevnum = 0; }
$localprev = $vars."pos=".$prevnum ;
$nextnum = $this->pos + $this->max_rows ;
$localnext = $vars."pos=".$nextnum ;
// $localend = $vars."pos=".$this->numrows ;
if ($this->pos >= $this->max_rows) {
$navbarr = "<a href=\"".$file.$localstart."\">".$this->strStart."</a>" ;
$navbarr .= "<a href=\"".$file.$localprev."\">".$this->strPrevious."</a>" ;
}
if ($this->max_rows <= $this->numrows) {
$navbarr .= "<a href=\"".$file.$localnext."\">".$this->strNext."</a>" ;
// $navbarr .= "<a href=\"".$file.$localend."\">".$this->strEnd."</a>" ;
}
$footerdata = array ( datanavbar => $navbarr ) ;
}
$this->recprow = 0;
$htmloutput .= $this->reportfusion($dbc, $footerdata, $this->footer) ;
return $htmloutput ;
} /* end function displayTable */
} /* end class ReportTable */
/*******************************************************
* The report functions
* *****************************************************
* This is a liste of functions that are used bye the report
* to execute some php code inside the report
*******************************************************
*/
/********
* Function Sub uses ReportTable as default report.
* We should find a solution for the choise of the report Table
*/
function sub($reportdata, $row, $dbc) {
// $recurs = new Report;
$recurs = new ReportTable ;
$sub = $reportdata ;
$recurs->id = $sub[1];
for ($i=2; $i<count($sub); $i++) {
$ruptfield = $sub[$i] ;
if (strlen($ruptfield)>0) {
global $$ruptfield;
$$ruptfield = $row[$ruptfield] ;
//echo $$ruptfield ;
}
}
$subreport = $recurs->display($dbc) ;
return $subreport ;
}
function noreg($reportdata, $row, $dbc) {
if ($reportdata[2]) {
$noregdata = $row[$reportdata[2]][$reportdata[1]] ;
} else {
$noregdata = $row[$reportdata[1]] ;
}
return $noregdata ;
}
function globalvar($reportdata, $row, $dbc) {
$sub = $reportdata; ;
// echo $sub[1] ;
if(!isset($$sub[1])) {
global $$sub[1];
}
// echo "--".$$sub[1] ;
$returnvalue = $$sub[1] ;
return $returnvalue ;
}
function pluriel($reportdata, $row, $dbc) {
if ($reportdata[2]) {
if($row[$reportdata[2]][$reportdata[1]] > 1) {
$returnvalue = $reportdata[3] ;
} else {
$returnvalue = "";
}
} else {
if($row[$reportdata[1]] > 1) {
$returnvalue = $reportdata[3] ;
} else {
$returnvalue = "";
}
}
return $returnvalue ;
}
function currencyvar($reportdata, $row, $dbc) {
$c = new currency;
$sub = $reportdata ;
$currencyname= ereg_replace ('\]', "", $sub[1] );
$curfield = ereg_replace ('\]', "", $sub[2] );
$c->name = $currencyname;
$c->getcurrency($dbc) ;
$printcur = $c->printcurrency($dbc, $row[$curfield]) ;
return $printcur ;
// $newrow = ereg_replace('\[currency:.+\:]', $printcur, $newrow) ;
}
/****
* Fonction d'insertion d'enregistrement d'une variable dans la session
* depuis un report
****/
function setglobal($reportdata, $row, $dbc) {
global $$reportdata[1] ;
if (strlen($reportdata[2])>0) {
$$reportdata[1] = $row[$reportdata[2]] ;
} else {
$$reportdata[1] = $row[$reportdata[1]] ;
}
$returnempty = "" ;
return $returnempty ;
}
/******************************************************
* End of the report's functions
*****************************************************/