Skip to content

Instantly share code, notes, and snippets.

@lastk
Created February 14, 2010 01:08
Show Gist options
  • Save lastk/303774 to your computer and use it in GitHub Desktop.
Save lastk/303774 to your computer and use it in GitHub Desktop.
function pesquisarUsuario($usuarioTo, $numPagina=null, $qtdRegistros=null) {
$sql = '';
$select = 'SELECT CD_USUARIO, CD_FUNC_ORIG, NOME, SOBRENOME, CPF, EMAIL, SENHA, DT_NASCIMENTO, SEXO, FL_RECEBE_EMAIL, FL_CONFIAVEL, CD_SITE_FUNC, FL_ATIVO, DH_CADASTRO, DH_ULTIMO_ACESSO ';
$selectCount = 'SELECT COUNT(*) ';
$from = ' FROM TB_USUARIO ';
$limit = '';
if(isset($numPagina) && isset($qtdRegistros)){
$numPag = ($numPagina - 1) * $qtdRegistros;
$limit = " LIMIT " . $numPag . ", " . $qtdRegistros;
}
$where = array();
$valores = array();
if($usuarioTo->getCodigo()){
$where[] = "CD_USUARIO = ?";
$valores[] = $usuarioTo->getCodigo();
}
if($usuarioTo->getUsuarioTO()){
$where[] = "CD_FUNC_ORIG = ?";
$valores[] = $usuarioTo->getUsuarioTO();
}
if($usuarioTo->getNome()){
$where[] = "UPPER(NOME) LIKE UPPER(?)";
$valores[] = $usuarioTo->getNome() . '%';
}
if($usuarioTo->getSobrenome()){
$where[] = "UPPER(SOBRENOME) LIKE UPPER(?)";
$valores[] = $usuarioTo->getSobrenome() . '%';
}
if($usuarioTo->getCpf()){
$where[] = "CPF = ?";
$valores[] = $usuarioTo->getCpf();
}
if($usuarioTo->getEmail()){
$where[] = "UPPER(EMAIL) LIKE UPPER(?)";
$valores[] = $usuarioTo->getEmail() . '%';
}
if($usuarioTo->getDataNascimento()){
$where[] = "DT_NASCIMENTO = ?";
$valores[] = $usuarioTo->getDataNascimento();
}
if($usuarioTo->getSexo()){
$where[] = "SEXO = ?";
$valores[] = $usuarioTo->getSexo();
}
if($usuarioTo->isFlagRecebeEmail()){
$where[] = "FL_RECEBE_EMAIL = ?";
$valores[] = $usuarioTo->isFlagRecebeEmail();
}
if($usuarioTo->isFlagConfiavel()){
$where[] = "FL_CONFIAVEL = ?";
$valores[] = $usuarioTo->isFlagConfiavel();
}
if($usuarioTo->getCodSiteFuncionario()){
$where[] = "CD_SITE_FUNC = ?";
$valores[] = $usuarioTo->getCodSiteFuncionario();
}
if($usuarioTo->isAtivo()){
$where[] = "FL_ATIVO = ?";
$valores[] = $usuarioTo->isAtivo();
}
if($usuarioTo->getTelefonesTO()){
$telefones = $usuarioTo->getTelefonesTO();
$subquery = $this->montaSubqueryTelefone($telefones[0]);
$where[] = "CD_USUARIO IN (" . $subquery["query"] . ")";
foreach ($subquery["valores"] as $key) {
$valores[] = $key;
}
}
if($usuarioTo->getEnderecoTO()){
$endereco = $usuarioTo->getEnderecoTO();
$subquery = $this->montaSubqueryEndereco($endereco);
$where[] = "CD_USUARIO IN (" . $subquery["query"] . ")";
foreach ($subquery["valores"] as $key) {
$valores[] = $key;
}
}
$sql = $select . $from . (sizeof($where) > 0 ? ' WHERE ' . implode(" AND ", $where) : '') . $limit;
$query = $this->db->query($sql, $valores);
$lista = array();
if($query->num_rows > 0) {
foreach ($query->result() as $rs){
$lista[] = $this->ArrayToUsuario($rs);
}
}
return $lista;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment