<?
include_once "include/utils.inc.php";
include_once "db/dbMy.class.php";
//include_once "include/images/images.class.php";


class dbElemento extends dbMY
{  

	function setInsert($table, $idCat, $data){
      
		$strip=get_magic_quotes_gpc();
	 
		$camposBD=$this->getCampos($table) ;
		if (empty($data['id'])) array_shift($camposBD);

		if (empty($data['data'])) $data['data']=date("Y-m-d");
		if (empty($data['hora'])) $data['hora']=date("H:i");

		$sep="";
        foreach ($camposBD as $c){
			if ($strip) stripslashes($data[$c]);
			$data[$c]=mysql_real_escape_string($data[$c]);			
			$cam.="$sep $c ";
			$val.="$sep '".$data[$c]."'";			
			if ($sep=="") $sep=",";
        }
            
        $sql="insert into $table (".$cam.") values (".$val.") ";

        $this->set($sql);
        $id= $this->getId();
        $this->setOrder($table, $idCat, $id);
		$this->setDateMod($table, $id);
		updateLog("Inseriu registo",$id,$table);
		
		return $id;
	}	

	function setOrder($table, $idCat, $id){
		
		$sql="select max(id) as TEMP from $table where idCat='$idCat'";
	   	$arrTemp=$this->get($sql);
	   	$tempOrdem=$arrTemp[0]['TEMP'];
		$tempOrdem++;
	    
		$sql="update $table set ordem=$tempOrdem where id=$id";
		
        $this->set($sql);
        return $tempOrder;
	}
	
	function setDateMod($table, $id){
		$data=date("Y-m-d");
		$hora=date("H:i");
		$por=$_SESSION['authdata']['login'];
		$sql="update $table set dataModificacao='$data', horaModificacao='$hora', por='$por' where id=$id";
		@$this->set($sql);
	}
	
	
	
	function setUpdate($table, $idcat, $data, $id){
        
		$strip=get_magic_quotes_gpc();
		
		$camposBD=$this->getCampos($table) ;
		array_shift($camposBD);

		$sql="update $table set ";
		$sep="";
        foreach ($camposBD as $c){
        	if ($c=="ordem") continue;
        	if ($c=="contagem") continue;
			if ($strip) stripslashes($data[$c]);
			$data[$c]=mysql_real_escape_string($data[$c]);
			
			$sql.="$sep $c = '".$data[$c]."'";
			if ($sep=="") $sep=",";
        }
        $sql.=" where id=$id";
		
        $this->set($sql);
        $this->setDateMod($table, $id);
		updateLog("Actualização de registo",$id,$table);
	}
	
	function setDelete($table, $id){
	   $sql="delete from $table where id=$id";
	   $this->set($sql);
	   updateLog("Apagou registo",$id,$table);
	}
	
	function setCopiar($table, $id, $idCat, $newIdCat){
	   	$linha=$this->getLinha($table, $id);
	   	$linha['idCat']=$newIdCat;
	   	$linha['id']="";
	   	updateLog("Copiou registo",$id,$table);
	   	return $this->setInsert($table, $newIdCat, $linha); 
	}
	
	function setMover($table, $id, $idCat, $newIdCat){
		updateLog("Moveu registo",$id,$table);
		$this->setLinhaCampo($table, $id, 'idCat', $newIdCat);
	}
	
	function setAbaixo($table, $idCat,  $id){
		$sql="select ordem from $table where id=$id ";
	   	$arrTemp=$this->get($sql);
	   	$outroOrdem=$arrTemp[0]['ordem'];

	   	$sql="select min(ordem) as TEMP from $table 
		   where idCat='$idCat' and ordem > $outroOrdem";
		   
		$arrTemp=$this->get($sql);
	   	$ordem=$arrTemp[0]['TEMP'];
	   	
	   	$sql="select id from $table 
		   where idCat='$idCat' and ordem= $ordem";
	   	$arrTemp=$this->get($sql);
	    $outroId=$arrTemp[0]['id'];

		$sql="update $table set ordem=$outroOrdem where id=$outroId";
        $this->set($sql);

        $sql="update $table set ordem=$ordem where id=$id";
        $this->set($sql);

        return $tempOrder;
	}
	
	function setAcima($table, $idCat,  $id){
		$sql="select ordem from $table where id=$id ";
	   	$arrTemp=$this->get($sql);
	   	$outroOrdem=$arrTemp[0]['ordem'];
	   	$sql="select max(ordem) as TEMP from $table 
		   where idCat='$idCat' and ordem < $outroOrdem";
		   
		$arrTemp=$this->get($sql);
	   	$ordem=$arrTemp[0]['TEMP'];
	   	
	   	$sql="select id from $table 
		   where idCat='$idCat' and ordem= $ordem";
	   	$arrTemp=$this->get($sql);
	    $outroId=$arrTemp[0]['id'];

		$sql="update $table set ordem=$outroOrdem where id=$outroId";
        $this->set($sql);
		
        $sql="update $table set ordem=$ordem where id=$id";
        $this->set($sql);
        	   	
        return $tempOrder;
	}
	
		
}

?>
