<?php
############################################################################
# This code is developed by Guerrieri Luca #
# copyright (C) 2006/2007 hide@address.com
# This code is released under the terms of the GPL v.2 License.
#
# The author is not responsible for data loss, or any kind of trouble that
# results from the use of this software.
# USE IT AT YOUR OWN RISK!
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
############################################################################
/*
* Classe per gestire le query
* attraverso set_query impostiamo il nome della query da eseguire e con get_query restituiamo la query da eseguire
* Richiamo ed esecuzione
*/
class QueryManager {
var $query="";
var $output; //il file da riempire
var $status; //lo stato dello script false=ancora in esecuzione true=finito
var $array_value; //array che contiene il risultato della query
var $placeholders; //array dei segnaposto da sostituire nel template
var $blank=''; //da sostituire se è una query dinamica
var $inizio=''; //il file da riempire
var $query_type; //se la query è dynamic o static
function QueryManager() {
//Costruttore della classe Query
require('config.inc.php'); //Richiede il file di configurazione generale
$this->error= new MSGmanager;
}
function set_query($set_query) {
//Imposta la query da eseguire e controlla se dynamic o static
require ('libquery.inc.php');
if (preg_match("/<!-- dynamic -->/i", $$set_query)){
$this->query_type="dynamic";
$this->query=$$set_query;
}else {
$this->query_type="static";
$this->query=$$set_query;
}
}
function get_query() {
//Restituisce la query eseguita
return $this->query;
}
#### Modifica per le query dinamiche
function set_placeholders ($placeholders){
//imposto i segnaposto da sostituire passandoglielo come array
if ((!is_array($placeholders))||(is_null($placeholders))){
print ($this->error->print_error('placeholders_Querymanager_passed_error'));
}else {
$this->placeholders=$placeholders;
}
}
function set_data($array_value){
//passiamo l'array di valori da sostituire nella query dinamica
if ((!is_array($array_value))||(is_null($array_value))){
print ($this->error->print_error('result_array_Querymanager_passed_error'));
}else {
$this->array_value=$array_value;
}
}
//metodo che sostituisce i valori ad ogni placeholders e ne restituisce la query da eseguire
function put_vars ($result){
if ((!is_array($result))||(is_null($result))){
print ($this->error->print_error('result_put_vars_QueryManager_passed_error'));
}
if (count($result)>0) {
//controlliamo che tipo di query stiamo eseguendo
if ($this->query_type=="dynamic"){
$this->output = eregi_replace("<!-- dynamic -->", $this->blank, $this->get_query());
}else {
$this->output = eregi_replace("<!-- static -->", $this->blank, $this->get_query());
}
foreach ($this->placeholders as $field){
$adjusted_field="<!-- $field -->";
$elem=current ($result);
$this->output = eregi_replace($adjusted_field,$elem,$this->output);
}
array_shift($result);
$this->status=false;
return $this->put_vars($result);
}elseif (count($result)==0) {
//controlla questo punto per la stampa dell'ultimo elemento
$this->status=true;
return $this->output;
}
}
function get_static_query() {
//Restituisce la query eseguita
$this->query = eregi_replace("<!-- static -->", $this->blank, $this->get_query());
return $this->query;
}
function get_dynamic_query(){
//restituisce su modello i risultati.
return $this->put_vars ($this->array_value);
}
}
?>