<?php

include 'daos/DAONoticia.class.php';

/**
 * Description of ControladorNoticia
 *
 * @author Eticons
 */
class ControladorNoticia {

    private $dao;

    function __construct() {
        $this->dao = new DAONoticia();
    }

    public function buscaNoticias($valor, $data) {
        $query = "SELECT n.id,n.titulo,n.subtitulo,n.texto,n.imagemUrl,DATE_FORMAT(n.dataNoticia, '%d/%m/%Y %Hh%im') AS dataNoticia, u.nome AS autor FROM tb_noticia n INNER JOIN tb_usuario u ON u.id = n.autor WHERE ";
        if ($valor != "" && $data != "") {
            $query.=" CAST(n.dataNoticia AS DATE) = '$data' AND (n.titulo LIKE '%$valor%' OR n.subtitulo LIKE '%$valor%' OR n.texto LIKE '%$valor%')";
        } else if ($valor != "") {
            $query.=" n.titulo LIKE '%$valor%' OR n.subtitulo LIKE '%$valor%' OR n.texto LIKE '%$valor%'";
        } else if ($data != "") {
            $query.=" n.titulo LIKE '%_%' AND CAST(n.dataNoticia AS DATE) = '$data' ";
        } else {
            $query.=" n.titulo LIKE '%_%'";
        }
        $query .= " ORDER BY n.dataNoticia DESC";
        return $this->dao->buscaNoticias($query);
    }

    public function getNoticias() {
        return $this->dao->getNoticias();
    }

}

?>
