require_once("mysql.transact.class.php");
class multiTable extends MySQL_Transact
{
var $tabelas, $nometabelas, $relacionamentos;
function multiTable($conn, $tablenames, $relations = null)
{ //construtor
$this->conn = $conn;
$this->nometabelas = array();
$this->nometabelas = $tablenames;
$this->relacionamentos = $relations;
//die($this->odbc_conn);
$this->tabelas = array();
foreach ($this->nometabelas as $nome){
$this->tabelas[$nome] = array();
}
}
//formata a data do pt_br para ingles
function formataDataEn($dt=''){
if($dt == '') return '';
list($dia,$mes,$ano)=split("/", $dt,3);
$retorno = "".$ano."-".$mes."-".$dia;
return $retorno;
}
// formata a data do mysql pro formato brasileiro
function formataDataPt($dt=''){
if($dt == '') return '';
list($ano,$mes,$dia)=split("-", $dt,3);
$retorno = "".$dia."/".$mes."/".$ano;
return $retorno;
}
function set($tabela, $campo, $val){
if($this->checkcampo($tabela, $campo)){
$this->tabelas[$tabela][$campo] = $val;
return true;
}
else return false;
}
function checkcampo($tabela, $campo){
$this->query="SHOW COLUMNS FROM ".$tabela;
$id = $this->execute($this->conn, $this->query);
while ($linha = mysql_fetch_assoc($id)){
if($campo == $linha['Field']){
mysql_free_result($id);
return true;
}
}
mysql_free_result($id);
return false;
}
function get($tabela, $campo){
if(isset($this->tabelas[$tabela][$campo]))
return $this->tabelas[$tabela][$campo];
else
return null;
}
function insert($tipo_func){
die("
Inserção Desabilitada");
}
function update(){
die(" Update Desabilitado!");
}
function delete($tabelas, $sql){
if($this->TestDelete()){
$this->query = "DELETE * FROM $tabelas WHERE $sql";
return $this->execute($showerror=true, $debug=true);
}
//die(" Deleção desabilitada!");
}
function TestDelete(){
return true;
}
function TestInsert(){
return true;
}
function TestUpdate(){
return true;
}
function reset(){
unset($this->tabelas);
// EXECUTAR ESSE CÓDIGO PARA CADA TABELA
/*if(sizeof($this->tabela)){
foreach($this->tabela as $campo => $valor){
unset($this->tabela[$campo]);
}
}*/
}
function listar($campos = "*", $usatabelas = null, $orderby = null){
$this->query = "";
$first = true;
$usada = array();
foreach($this->nometabelas as $index => $nome){
$usada[$nome] = false;
}
//$usada[$this->nometabelas[0]] = true;
if($usatabelas != null)
foreach ($usatabelas as $indtab => $nometab)
$usada[$nometab] = true;
//while(list($key,$val) = each($table))
// echo "\n$key=>$val";
//die("querie".$this->query);
$tables = array();
foreach ($usada as $nometab => $val){
if($usada[$nometab]) $tables[sizeof($tables)] = $nometab;
}
if(sizeof($this->tabelas)){
foreach ($this->tabelas as $nome => $tabela) {
foreach ($tabela as $campo => $valor){
if(!is_null($this->tabelas[$nome][$campo])){
if(!empty($this->tabelas[$nome][$campo])){
if(!$usada[$nome]){
$usada[$nome] = true;
$tables[sizeof($tables)] = $nome;
}
if($first){
$first = false;
$this->query .= " WHERE $nome.$campo = '".$valor."'";
}
else
$this->query .= " AND $nome.$campo = '".$valor."'";
}
}
}
}
}
for ($i=0; $inometabelas);$i++){
for ($j=$i+1; $jnometabelas);$j++){
if($usada[$this->nometabelas[$i]] && $usada[$this->nometabelas[$j]] && $this->relacionamentos[$this->nometabelas[$i]][$this->nometabelas[$j]] != null){
$this->query .= " AND ".$this->nometabelas[$i].".".$this->relacionamentos[$this->nometabelas[$j]][$this->nometabelas[$i]]." = ".$this->nometabelas[$j].".".$this->relacionamentos[$this->nometabelas[$i]][$this->nometabelas[$j]];
}
}
}
if(!is_null($orderby))
$this->query .= " ORDER BY ".$orderby;
//echo sizeof($tables).";;";
return $this->vt_recordset($campos, $tables, $this->query);
}
function consultar() {
$this->query = "";
$first = true;
$usada = array();
foreach($this->nometabelas as $index => $nome){
$usada[$nome] = false;
}
//while(list($key,$val) = each($table))
// echo "\n$key=>$val";
//die("querie".$this->query);
$tables = array();
//$tables[0] = $this->nometabelas[0];
//$usada[$this->nometabelas[0]] = true;
if(sizeof($this->tabelas)){
foreach ($this->tabelas as $nome => $tabela) {
foreach ($tabela as $campo => $valor){
if(!is_null($this->tabelas[$nome][$campo])){
if(!empty($this->tabelas[$nome][$campo])){
if(!$usada[$nome]){
$usada[$nome] = true;
$tables[sizeof($tables)] = $nome;
}
if($first){
$first = false;
$this->query .= " WHERE $nome.$campo = '".$valor."'";
}
else
$this->query .= " AND $nome.$campo = '".$valor."'";
}
}
}
}
}
for ($i=0; $inometabelas);$i++){
for ($j=$i+1; $jnometabelas);$j++){
if($usada[$this->nometabelas[$i]] && $usada[$this->nometabelas[$j]]){
$this->query .= " AND ".$this->nometabelas[$i].".".$this->relacionamentos[$this->nometabelas[$j]][$this->nometabelas[$i]]." = ".$this->nometabelas[$j].".".$this->relacionamentos[$this->nometabelas[$i]][$this->nometabelas[$j]];
}
}
}
$qry = $this->query;
$qry2 = "";
foreach($this->nometabelas as $index => $nome){
if($usada[$nome]){
$this->query = "DESCRIBE $nome";
$rs = $this->execute();
while($row=mysql_fetch_assoc($rs)){
if($qry2 == "")
$qry2 = "$nome.".$row['Field']." AS '$nome::".$row['Field']."'";
else
$qry2 .= ", $nome.".$row['Field']." AS '$nome::".$row['Field']."'";
}
}
}
$this->query = "SELECT $qry2 FROM ";
for($i=0; $i< sizeof($tables); $i++){
$this->query .= $tables[$i];
if($i < sizeof($tables)-1) $this->query .= ",";
}
$this->query .= $qry;
//die($this->query);
$rs = $this->execute($showerror=true, $debug=true);
$row_rs = mysql_fetch_assoc($rs);
$total_linhas = mysql_num_rows($rs);
$indexnomes = array_flip($this->nometabelas);
if ($total_linhas>0){
foreach ($row_rs as $campo => $valor){
$nomes = split("::", $campo);
$this->tabelas[$this->nometabelas[$indexnomes[$nomes[0]]]][$nomes[1]] = $valor;
}
$bl_aux = true;
}else $bl_aux = false;
mysql_free_result($rs);
return $bl_aux;
//die("função de consulta desabilitada.");
}
function show($tabela, $campo){
if(isset($this->tabelas[$tabela][$campo]))
return true;
else
return false;
}
}
?>