= 1) { $query = "SELECT dpto_nome FROM rma_depto_ramais WEHRE nome='$telefone' AND dpto_id='$depto_id'"; $result = pg_query($dbcon, $query); if (pg_num_rows($result) >= 1) { $mes = "Ja existe registro com essas informacoes!"; } } else { /* * VERIFICA QUAL TABELA ESTA O RAMAL */ if ($tipoTable == "SIP") { $tabela = "pbx_sip_ramais"; $campoRamal = "nome"; $campoNome = "callerid"; $telefone = $ramal; } elseif ($tipoTable == "IAX") { $tabela = "pbx_iax_pbx"; $campoRamal = "nome"; $campoNome = "identificador"; $telefone = $ramal; } elseif ($tipoTable == "DGV") { $tabela = "pbx_dgv"; $campoRamal = "nome"; $campoNome = "callerid"; $telefone = $ramal; } elseif ($tipoTable == "PBX") { $tabela = "pbx_ramais_pbx"; $campoRamal = "numero"; $campoNome = "identificador"; $telefone = $ramal; } elseif ($tipoTable == "DAHDI") { $tabela = "pbx_dahdi"; $campoRamal = "numero"; $campoNome = "identificador"; $telefone = $ramal; } elseif ($tipoTable == "KHOMP") { $tabela = "pbx_khomp"; $campoRamal = "numero"; $campoNome = "identificador"; $telefone = $ramal; } elseif ($tipoTable == "AGD") { $tabela = "pbx_ramais_agenda"; $campoRamal = "rma_telefone"; $campoNome = "rma_nome"; $campoTelefone = ", rma_telefone='$telefone'"; } else { $tabela = ""; $campoRamal = ""; $campoNome = ""; } /* * VERIFICA SE RAMAL JA ESTÁ RELACIONADO COM ALGUM DEPARTAMENTO */ $query = "SELECT nome FROM rma_depto_ramais WHERE nome='$ramal'"; $result = pg_query($dbcon, $query); if (pg_num_rows($result) >= 1) { /* * SE O PARAMETRO DO DEPARTAMENTO RECEBIDO FOR IGUAL A 0(zero) DELETA REGISTRO NA TABELA DE RELACIONAMENTO */ if ($depto_id == '0') { $delete = "DELETE FROM rma_depto_ramais WHERE nome='$telefone'"; $result = pg_query($dbcon, $delete); } /* * ATUALIZA DEPARTAMENTO DO RAMAL */ $update = "UPDATE rma_depto_ramais SET dpto_id='$depto_id' ,nome='$telefone', data_reg = now() WHERE nome='$ramal'"; $result = pg_query($dbcon, $update); /* * VERIFICA SE DEPARTAMENTO FOI ATUALIZADO COM SUCESSO */ $query = "SELECT dpto_id,nome FROM rma_depto_ramais WHERE dpto_id='$depto_id' AND nome='$telefone'"; $result = pg_query($dbcon, $query); if (pg_num_rows($result) < 1) { $mes = "Erro ao alterar o departmaneto!"; } } else { /* * SE RAMAL NÃO ESTIVER RELACIONADO COM NENHUM DEPARTAMENTO,SISTEMA FAZ A INSERÇÃO DO RAMAL NA TABELA */ $insert = "INSERT INTO rma_depto_ramais (dpto_id,nome) VALUES ('$depto_id','$telefone')"; $result = pg_query($dbcon, $insert); $query = "SELECT dpto_id,nome FROM rma_depto_ramais WHERE dpto_id='$depto_id' AND nome='$telefone'"; $result = pg_query($dbcon, $query); if (pg_num_rows($result) < 1) { $mes = "Erro ao inserir o departmaneto!"; } } /* * EXECUTA UPDATE NA TABELA */ $update = "UPDATE $tabela SET $campoNome='$nome' $campoTelefone WHERE $campoRamal='$ramal'"; $query = pg_query($dbcon, $update); /* * ATUALIZA DDD DO RAMAL */ $update = "UPDATE pbx_ramais_agenda SET ddd='$ddd' WHERE rma_telefone='$ramal'"; $result = pg_query($dbcon, $update); /* * VERIFICA SE UPDATE FOI REALIZADO COM SUCESSO! */ $query = "SELECT callerid FROM pbx_ramais_mesa WHERE callerid='$nome' AND nome='$telefone'"; $result = pg_query($dbcon, $query); if (pg_num_rows($result) >= 1) { if ($tipoTable != 'AGD') { __GravaAstDb($dbcon, $ramal, $nome); } $mes = "Registro alterado com sucesso!"; } else { $mes = "Não foi possivel alterar registro!"; } } } echo ""; } /* * LOOP DE DEPARTAMENTOS */ $buscaDep = "SELECT dpto_nome,dpto_id FROM rma_departamentos WHERE dpto_status='1'"; $resultDep = pg_query($dbcon, $buscaDep); while ($rowDep = pg_fetch_array($resultDep)) { $nomeDep = $rowDep['dpto_nome']; $idDep = $rowDep['dpto_id']; $optionDep .= ""; } $optionDep .= ""; function TrataValor($nome, $ddd, $telefone, $trDDD) { $img = ""; if (empty($nome)) { return " $img Por favor preencha o campo nome!"; } if (empty($telefone)) { return " $img Por favor preencha o campo telefone!"; } if (!is_numeric($telefone)) { $erro = 2; return " $img Preencha o campo Telefone corretamente,permitido somente numero!"; } if ($erro != 2) { return 1; } } if (TrataValor($nome, $ddd, $telefone, $trDDD) != 1) { $mes = TrataValor($nome, $ddd, $telefone, $trDDDD); } if ($tipoTable == "AGD") { $trDDD = true; } else { $trDDD = false; } $smarty->assign('nome', $nome); $smarty->assign('idRamal', $ramal); $smarty->assign('ramal', $telefone); $smarty->assign('tipoAgenda', $tipoTable); $smarty->assign('mes', $mes); $smarty->assign('optionDep', $optionDep); $smarty->assign('trDDD', $trDDD); $smarty->assign('ddd', $ddd); $smarty->display("agendaRamais/ramais/ramaisUpdate.html"); function __GravaAstDb($dbcon, $ramal, $nome) { include_once('admin/funcoes.php'); CommandAsterisk2($dbcon, 'BINA-NOME', $ramal, $nome); } ?>