<?

class dbMy{

    var $link;

    function dbMy(){
        if (empty($this->link)){
            include "config/config.inc.php";
            $this->link = @mysql_connect($host , $user , $pass );
			//mysql_set_charset("utf8", $this->dbl);
			//mysql_set_charset("iso-8859-1", $this->link);
			//mysql_query("SET NAMES 'iso-8859-1'");
			//mysql_query("SET CHARACTER SET 'iso-8859-1'");
            mysql_select_db($database, $this->link );
        }
    }
    
    function get($sql, $offset="", $limit=""){
        //echo $sql."<br/>";
		
		$result =  mysql_query( $sql, $this->link );

		$numrows=@mysql_num_rows( $result);
		for ($i=0; $i<$numrows; $i++)
            $row[$i]=mysql_fetch_assoc($result);

        return $row;
    }

    function set($sql){
        //echo $sql."<br/>";
		
        mysql_query( $sql, $this->link );
    }
    
    function getCampos($table){
		
		if (strstr($table, ","))
			$mytable=explode(",", $table);
		else
			$mytable[0]=$table;
		
		foreach ($mytable as $table){	
        	$res = mysql_query("SHOW COLUMNS FROM $table", $this->link);
        	while ($row = mysql_fetch_array($res)) $colNames[]=$row[0];
        }
		return $colNames;
    }
    
    function getId(){
        return mysql_insert_id($this->link);
        //Select @@Identity as ID
        //SELECT @@IDENTITY as last_insert_id
    }
	
	/////////////////////////////////////////////////////////////////////////////////////////
	function getLinha($table, $id){
		$id=mysql_real_escape_string($id);
        $sql="select * from $table where id='$id'";
		
        $linha=$this->get($sql);
        return $linha[0];
		
	}
	
	function getLinhaCampo($table, $campo, $valor){
        $sql="select * from $table where $campo='$valor'";
        $linha=$this->get($sql);
        return $linha[0];
	}
	
	function setLinhaCampo($table, $id, $campo, $valor){
        $sql="update $table set $campo='$valor' where id='$id'";
        $linha=$this->set($sql);
	}
	
	function getLista($table, $idCat, $campos, $campo="ordem", $ordem=1, $offset=0, $limit=50, $procurar="", $filtro=""){
		$camposSql=($campos == '*')?$campos:implode(',',$campos);
            
	   	if ( $ordem == 1 ) { $sorti="asc" ;} else { $sorti="desc" ;};
	   	if (empty($campo)) $campo='ordem';
   	    if (empty($offset)) $offset=0;
        if (empty($limit)) $limit=50;
        
        $where=$this->makeWhere($table, $idCat,$procurar, $filtro);
	   
        $sql="select $camposSql from $table $where order by $campo $sorti limit $offset, $limit";
        $matrix=$this->get($sql);
        return $matrix;

	}
	
	function getListaComposta($tables, $idCat, $camposSql, $campo="ordem", $ordem=1, $offset=0, $limit=50, $procurar="", $filtro="", $whereSql=""){
            
	   	if ( $ordem == 1 ) { $sorti="asc" ;} else { $sorti="desc" ;};
	   	if (empty($campo)) $campo='ordem';
   	    if (empty($offset)) $offset=0;
        if (empty($limit)) $limit=50;
        
        $where=$this->makeWhere($tables, $idCat,$procurar, $filtro);
		if ($whereSql) $where.=$where?"and " . $whereSql:"where " . $whereSql;
	   
        $sql="select $camposSql from $tables $where order by $campo $sorti limit $offset, $limit";
                
        $matrix=$this->get($sql);
        return $matrix;
	}
	


	function getListaCategorias($table, $campos, $campo="id", $ordem=1){
		$camposSql=($campos == '*')?$campos:implode(',',$campos);

	   if ( $ordem == 1 ) { $sorti="asc" ;} else { $sorti="desc" ;};
	  
        $sql="select $camposSql from $table order by $campo $sorti";

        $matrix=$this->get($sql);
        return $matrix;

	}

	function getTotal($table, $idCat, $procurar="", $filtro=""){
		       
		$where=$this->makeWhere($table, $idCat, $procurar, $filtro);
        
        $sql="select count(*) as total from $table $where";
        //echo $sql;
        $total=$this->get($sql);
        return $total[0]['total'];
	}
	
	function tipo($idCat=1){

        $sql="select * from categorias where idCat=$idCat";
        $matrix=$this->get($sql);
        return $matrix[0]['idCat'];

	}

	function getDistinct($table, $idCat, $campo){
		$sql="select distinct $campo from $table where idCat=$idCat order by $campo";
		//echo $sql;
		$matrix=$this->get($sql);
        return $matrix;
	}
	
	function makeWhere($table, $idCat="", $procurar="", $filtro=""){
		//print "<br><br>$idCat<br><br>";
		$where="where ";
		
		if (($idCat!=0) && ($idCat!="") )  {
			$idCat=mysql_real_escape_string($idCat);
			$where.="idCat='$idCat' ";
		}
		
		$camposBD=$this->getCampos($table) ;
	    
		if ($procurar){
		  if (strlen($where)>6) $where .= "and ";
          $where .="(";
          $i=0;
		  
		  //explode por espacos
		  $aprocurar=explode(" ",$procurar);
		  // for
		  foreach ($aprocurar as $procurar1){
			  $procurar1=mysql_real_escape_string($procurar1);
			  foreach ($camposBD as $c){
				if ($i) $jun=" or " ;
				$where.=" $jun $c like '%$procurar1%'";
				$i++;
			  }
		  }
		  
          $where.=" )";
        }
		

        if (is_array($filtro)){
			foreach ($filtro as $c=>$v){		
				if ($v!=""){
					if (strlen($where)>6) $where .= " and ";
					$v=mysql_real_escape_string($v);
					$where.="$c='$v' ";	
				} 
			}           	
		}
		
	//		$datafiltro=$_REQUEST['datafiltro'];
//		if (($datafiltro[inicio]) && ($datafiltro[fim])){
//			if (strlen($where)>6) $where .= " and ";
//			$where.= " data >='" .$datafiltro['inicio']. "' and data <='" .$datafiltro['fim'] ."'";
//		}


		if ($_REQUEST["filtrodata"]){
			$data_inicio = $_REQUEST["data_inicio"];
			$data_fim = $_REQUEST["data_fim"];
			if($data_inicio){
				if (strlen($where)>6) $where .= "and ";
					$where .= " data >= '$data_inicio'";
			}
			if($data_fim){
				if (strlen($where)>6) $where .= "and ";
					$where .= " data <= '$data_fim'";
			}
		}
		
		if ($_REQUEST['notnull']){
			if (strlen($where)>6) $where .= "and ";
			$where.= $_REQUEST['notnull'] . " !=''";
		}
		
        return (strlen($where)>6)?$where:"";
	}
	
	function setContagem($table, $id){
        $sql="update $table set contagem=contagem + 1 where id='$id'";
        $linha=$this->set($sql);
	}
	
	function getContagem($table){
        $sql="select * from $table order by contagem desc limit 0,5";
        return $this->get($sql);
	}
	
	function setVoto($table, $id, $voto, $ip){
        $sql="insert into voto (tipo, ida, voto, ip) values('$table','$id', '$voto', '$ip')";
        $linha=$this->set($sql);
	}
	
	function getVoto($table, $id){
        $sql="select AVG(voto) as media, COUNT(voto) as contagem from voto where tipo='$table' and ida='$id'";
        return $this->get($sql);
	}
	
	function getVotoMais($table){
        $sql="select AVG(voto) as media, COUNT(voto) as contagem, ida from voto where tipo='$table' group by ida order by contagem desc limit 0,5";
        return $this->get($sql);
	}

	function getComentarioMais($table){
        $sql="select COUNT(id) as contagem, idArtigo from comentario group by idArtigo order by contagem desc limit 0,5";
        return $this->get($sql);
	}
	
}
?>
