<?php

require_once 'Conexao.class.php';

/**
 * Description of DAOUsuario
 *
 * @author Bruno
 */
class DAOUsuario {

    private $conexao = null;

    function __construct() {
        $this->conexao = new Conexao();
    }

    public function logar($login, $senha) {
        try {
            $con = $this->conexao->getConexao();
            $stmt = $con->query("SELECT * FROM tb_usuario WHERE usuario='$login' AND senha='$senha' AND status='Ativo'");
            $this->conexao->desconectar();
            if (!$result = $stmt->fetchObject(get_class(new Usuario()))) {
                return null;
            } else {
                return $result;
            }
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }

    public function verificaLogin($login) {
        try {
            $con = $this->conexao->getConexao();
			
            $stmt = $con->query("SELECT COUNT(*) AS cont FROM (SELECT * FROM tb_usuario UNION SELECT * FROM tb_usuario_temp) AS t WHERE t.usuario='$login'");
            $this->conexao->desconectar();
            $login = "";
            if ($stmt) {
                $login = $stmt->fetch();
                return $login['cont'];
            } else {
                return "";
            }
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }

    public function verificaUsuario($login, $email) {
        try {
            $con = $this->conexao->getConexao();
            $stmt = $con->query("SELECT * FROM tb_usuario WHERE usuario = '$login' AND email = '$email' AND status = 'Ativo'");
            $this->conexao->desconectar();
            if (!$usuario = $stmt->fetchObject(get_class(new Usuario()))) {
                return null;
            } else {
                return $usuario;
            }
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }

    public function buscarUsuarios($query) {
        try {
            $con = $this->conexao->getConexao();
            $stmt = $con->query($query);
            $this->conexao->desconectar();
            $i = 0;
            $lista = array();
            if ($stmt) {
                while ($row = $stmt->fetchObject(get_class(new Usuario()))) {
                    $lista[$i] = $row;
                    $i++;
                }
                return $lista;
            } else {
                return array();
            }
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }

    public function getInformacoes() {
        try {
            $query = "SELECT COUNT(*) AS solicitantes,(SELECT COUNT(*) FROM tb_sugestoes WHERE situacao = 0) AS sugestoes,
                      (SELECT COUNT(*) FROM tb_sistema WHERE CAST(data AS DATE) = CAST(NOW() AS DATE)) AS versoes,
                      (SELECT COUNT(*) FROM tb_interessados) AS interessados
                      FROM tb_usuario_temp";
            $con = $this->conexao->getConexao();
            $stmt = $con->query($query);
            $this->conexao->desconectar();
            if (!$usuario = $stmt->fetchObject(get_class(new Informacoes()))) {
                return new Informacoes();
            } else {
                return $usuario;
            }
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }

}

?>
