You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

326 lines
13 KiB

<?php
$tpLayout = 1;
$plId = $_FORMS["pl_id"];
$valuesForm = array();
$msgErro = "";
$fieldsTable = array("pp_id", "pl_descricao", "pl_data_inicio", "pl_data_fim", "pl_ativa");
$org_id = GetOrganizacao();
if (!IsPostBack()) {
/*
* Quando $plId é passado a opereção é de edição
*/
if ($plId) {
$query = "SELECT pl_id, a.pp_id, pl_data, pl_descricao, pl_data_inicio, pl_data_fim, pl_ativa
FROM pbx_pesquisa_liberacao a
WHERE a.pl_id = $plId";
$result = pg_query($dbcon, $query);
$dados = pg_fetch_array($result);
foreach ($dados as $key => $value) {
$valuesForm[$key] = $value;
}
$valuesForm["pl_data_inicio"] = FormataDataDbDataDisplay($dados["pl_data_inicio"]);
$valuesForm["pl_data_fim"] = FormataDataDbDataDisplay($dados["pl_data_fim"]);
$_SESSION['SS_pp_id'] = $dados['pp_id'];
} else {
$valuesForm["pl_ativa"] = 1;
}
} else {
$valuesUpd["pp_id"] = $_SESSION['SS_pp_id'];
$pp_id = $_SESSION['SS_pp_id']; //$_FORMS["p_id"];
$pl_descricao = $_FORMS["pl_descricao"];
$pl_data_inicio = $_FORMS["pl_data_inicio"];
$pl_data_fim = $_FORMS["pl_data_fim"];
$pl_ativa = isset($_FORMS["pl_ativa"]) ? 1 : 0;
foreach ($fieldsTable as $key) {
$valuesForm[$key] = $_FORMS[$key];
}
if (empty($pl_descricao)) {
$msgErro = "Informe uma descrição!";
} else if ((soNumero($pl_data_inicio) != "") && (!is_date($pl_data_inicio))) {
$msgErro = "Data inicial inválida!";
} else if ((soNumero($pl_data_fim) != "") && (!is_date($pl_data_fim))) {
$msgErro = "Data final inválida!";
}
if (empty($msgErro)) {
$valuesForm["pl_data_inicio"] = FormatDtMssql($pl_data_inicio);
$valuesForm["pl_data_fim"] = FormatDtMssql($pl_data_fim);
$valuesForm["user_reg"] = GetLogin();
if ($plId) {
foreach ($fieldsTable as $key) {
$valuesUpd[$key] = $_FORMS[$key];
}
$valuesUpd["pl_data_inicio"] = FormatDtMssql($pl_data_inicio);
$valuesUpd["pl_data_fim"] = FormatDtMssql($pl_data_fim);
$valuesUpd["pl_ativa"] = $pl_ativa;
$valuesUpd["pp_id"] = $_SESSION['SS_pp_id'];
$valuesForm['pp_id'] = $_SESSION['SS_pp_id'];
$where = " pl_id = '$plId'";
$query = "begin";
$result = pg_query($dbcon, $query);
if ($result) {
$result = UpdateDb($dbcon, 'pbx_pesquisa_liberacao', $valuesUpd, $where);
}
if ($result) {
$result = GravaUra($dbcon, $msgErro);
}
if ($result) {
$query = "commit";
$result = pg_query($dbcon, $query);
$jsStartup[] = "alert('Liberação alterada com sucesso!'); ";
} else {
$msgErro = "Não foi possível alterar a liberação!";
$plId = 0;
$query = "rollback";
$result = pg_query($dbcon, $query);
}
} else {
$msgErro = "";
$query = "begin";
if (!isset($valuesForm['pl_ativa'])) {
$valuesForm['pl_ativa'] = 0;
}
$valuesForm['org_id'] = $org_id;
$result = pg_query($dbcon, $query);
if ($result) {
$result = InsertDb($dbcon, 'pbx_pesquisa_liberacao', $valuesForm, "pbx_pesquisa_liberacao_pl_id_seq");
$plId = $result;
}
if ($result) {
$result = GravaUra($dbcon, $msgErro);
}
if ($result) {
$query = "commit";
$result = pg_query($dbcon, $query);
echo "<script>opener.location.reload();</script>";
$jsStartup[] = "alert('Liberação inserida com sucesso!'); ";
} else {
$msgErro = $plId ? "Não foi possível alterar a liberação!" : "Não foi possível inserir a nova liberação!";
$plId = 0;
$query = "rollback";
$result = pg_query($dbcon, $query);
}
}
$valuesForm["pl_data_inicio"] = FormataDataDbDataDisplay($valuesForm["pl_data_inicio"]);
$valuesForm["pl_data_fim"] = FormataDataDbDataDisplay($valuesForm["pl_data_fim"]);
}
}
if ($msgErro) {
$jsStartup[] = "alert('$msgErro'); ";
}
$jsJQuery[] = "\$('#pl_data_inicio').keypress(function(){formataDataHora(this);}) ";
$jsJQuery[] = "\$('#pl_data_fim').keypress(function(){formataDataHora(this);}) ";
$valuesForm['acaoUser'] = "Gravar";
$valuesForm['pl_id'] = $plId;
foreach ($valuesForm as $key => $value) {
$smarty->assign($key, $value);
}
$smarty->assign("msg", $msgErro);
$smarty->assign("pesquisa", GetPesquisaDisp($dbcon, $valuesForm['pp_id']));
GetTemplate($smarty, 'pesquisa/pesquisaCadLibera.htm');
function GetPesquisaDisp($db, $ppId)
{
$org_id = GetOrganizacao();
$query = "SELECT pp_id, pp_nome, pp_data
FROM pbx_pesquisa
WHERE pp_status = 1
AND org_id = $org_id
ORDER BY pp_nome";
$result = pg_query($db, $query);
$sel = $ppId ? 'selected="selected"' : "";
$ret = "<option value=\"0\" $sel>----------------</option>";
while ($dados = pg_fetch_array($result)) {
$pp_id = $dados["pp_id"];
$pp_nome = $dados["pp_nome"];
$pp_data = FormataDataDbDataDisplay($dados["pp_data"]);
$sel = $ppId == $pp_id ? 'selected="selected"' : "";
$ret .= "<option value=\"$pp_id\" $sel>$pp_nome [$pp_data]</option>";
}
return $ret;
}
function GravaUra($db, &$msgErro)
{
$path = VerificaCharFinal(GetCaminhoProducao()) . "extensions_pesquisa.conf";
$file = fopen($path, 'w+');
$query = GetQueryPesquisa('cabecalio');
$result = pg_query($db, $query);
$ura = array();
while ($dados = pg_fetch_array($result)) {
$plId = $dados['pl_id'];
$ppId = $dados['pp_id'];
$nomeUra = sprintf('psq-%s-%s', $plId, 'ini');
$timeOutDigit = 1;
$timeOutResponse = 10;
/*
* Pega a ura que será redirecionado apos a saudação
*/
$uraQuest = GetQuestoesUra($db, $ppId);
$quest = $uraQuest[0];
$nomeUra1 = sprintf("psq-%s-%s", $plId, $quest["pq_id"]);
$audioSauda = sprintf("customizados/pesquisa_audio_sauda_%s", $ppId);
fwrite($file, sprintf('[%s]%s', $nomeUra, "\n"));
//fwrite($file, sprintf('include => padrao%s', "\n"));
fwrite($file, sprintf('exten => s,1,NoCDR()%s', "\n"));
fwrite($file, sprintf('exten => s,1,Macro(verifica-eventos-dacs)%s', "\n"));
fwrite($file, sprintf('exten => s,n,Answer()%s', "\n"));
fwrite($file, sprintf('exten => s,n,Playback(%s)%s', $audioSauda, "\n"));
fwrite($file, sprintf('exten => s,n,AGI(pbx_pesquisa_ura.php,%s,${IDORIGEM},%s)%s', 'ini', $plId, "\n"));
fwrite($file, sprintf('exten => s,n,Goto(%s,s,1)%s', $nomeUra1, "\n"));
fwrite($file, sprintf('exten => s,n,Hangup()%s', "\n"));
fwrite($file, sprintf('%s', "\n"));
/*
* Inicia a gravação das uras associadas as questoes
*/
$numQuest = count($uraQuest);
for ($i = 0; $i < $numQuest; $i++) {
$questoes = $uraQuest[$i];
$pqId = $questoes["pq_id"];
$nomeUraQuest = sprintf("psq-%s-%s", $plId, $pqId);
$audioQuest = sprintf("customizados/pesquisa_questao_%s_%s", $ppId, $pqId);
fwrite($file, sprintf('[%s]%s', $nomeUraQuest, "\n"));
//fwrite($file, sprintf('include => padrao%s', "\n"));
fwrite($file, sprintf('exten => s,1,Set(LOOPCOUNT=0)%s', "\n"));
//fwrite($file, sprintf('exten => s,n,AGI(pbx_pesquisa_ura.php,ura)%s', "\n"));
fwrite($file, sprintf('exten => s,n,GotoIf($["${CDR(disposition)}" = "ANSWERED"]?inicio)%s', "\n"));
// fwrite($file, sprintf('exten => s,n,Answer()%s', "\n"));
fwrite($file, sprintf('exten => s,n,Wait(1)%s', "\n"));
fwrite($file, sprintf('exten => s,n(inicio),Set(TIMEOUT(digit)=%s)%s', $timeOutDigit, "\n"));
fwrite($file, sprintf('exten => s,n,Set(TIMEOUT(response)=%s)%s', $timeOutResponse, "\n"));
fwrite($file, sprintf('exten => s,n,Background(%s)%s', $audioQuest, "\n"));
fwrite($file, sprintf('exten => s,n,WaitExten()%s', "\n"));
$nomeUraGoto = isset($uraQuest[$i + 1]) ? sprintf("psq-%s-%s", $plId, $uraQuest[$i + 1]["pq_id"]) : sprintf('psq-%s-%s', $plId, 'fim');
$uraOpc = GetOpcUra($db, $pqId);
$numOpc = count($uraOpc);
for ($j = 0; $j < $numOpc; $j++) {
$opc = $uraOpc[$j];
$id = 0;
$opcTecla = $opc["po_tecla"];
$poId = $opc["po_id"];
fwrite($file, sprintf('exten => %s,%s,AGI(pbx_pesquisa_ura.php,${IDORIGEM},%s,%s,%s,%s)%s', $opcTecla, ++$id, $plId, $ppId, $pqId, $poId, "\n"));
fwrite($file, sprintf('exten => %s,%s,Goto(%s,s,1)%s', $opcTecla, ++$id, $nomeUraGoto, "\n"));
fwrite($file, sprintf('exten => %s,%s,Hangup()%s', $opcTecla, ++$id, "\n"));
}
fwrite($file, sprintf('exten => t,1,Goto(loop,1)%s', "\n"));
fwrite($file, sprintf('exten => loop,1,Set(LOOPCOUNT=$[${LOOPCOUNT} + 1])%s', "\n"));
fwrite($file, sprintf('exten => loop,n,GotoIf($[${LOOPCOUNT} > 2]?desliga,1)%s', "\n"));
fwrite($file, sprintf('exten => loop,n,Goto(${CONTEXT},s,inicio)%s', "\n"));
fwrite($file, sprintf('exten => desliga,1,Playback(vm-goodbye)%s', "\n"));
fwrite($file, sprintf('exten => desliga,n,Hangup%s', "\n"));
fwrite($file, sprintf('exten => i,1,Playback(invalid)%s', "\n"));
fwrite($file, sprintf('exten => i,n,Goto(loop,1)%s', "\n"));
fwrite($file, sprintf('%s', "\n"));
}
/*
* Finaliza ura audio de encerramento
*/
$audioEncerra = sprintf("customizados/pesquisa_audio_encerra_%s", $ppId);
fwrite($file, sprintf('[%s]%s', $nomeUraGoto, "\n"));
//fwrite($file, sprintf('include => padrao%s', "\n"));
//fwrite($file, sprintf('exten => s,1,Answer()%s', "\n"));
fwrite($file, sprintf('exten => s,1,AGI(pbx_pesquisa_ura.php,%s,${IDORIGEM},%s)%s', 'fim', $plId, "\n"));
fwrite($file, sprintf('exten => s,2,Playback(%s)%s', $audioEncerra, "\n"));
fwrite($file, sprintf('exten => s,n,Hangup()%s', "\n"));
fwrite($file, sprintf('%s', "\n"));
}
fclose($file);
GetUrl(AST_RELOAD_URA);
return true;
/*
[ura-126]
include => padrao
exten => s,1,Set(LOOPCOUNT=0)
exten => s,n,AGI(pbx_complemento.php,ura)
exten => s,n,GotoIf($["${CDR(disposition)}" = "ANSWERED"]?inicio)
exten => s,n,Answer()
exten => s,n,Wait(1)
exten => s,n(inicio),Set(TIMEOUT(digit)=1)
exten => s,n,Set(TIMEOUT(response)=10)
exten => s,n,WaitExten()
exten => 1,1,AGI(pbx_complemento.php,ura)
exten => 1,n,Goto(ext-ramais,1001,1)
exten => 1,n,Hangup()
exten => t,1,Goto(loop,1)
exten => loop,1,Set(LOOPCOUNT=$[${LOOPCOUNT} + 1])
exten => loop,n,GotoIf($[${LOOPCOUNT} > 2]?desliga,1)
exten => loop,n,Goto(${CONTEXT},s,inicio)
exten => desliga,1,Playback(vm-goodbye)
exten => desliga,n,Hangup
exten => i,1,Playback(invalid)
exten => i,n,Goto(loop,1)
*/
}
function GetQuestoesUra($db, $ppId)
{
$query = sprintf(GetQueryPesquisa("questoes"), $ppId);
$result = pg_query($db, $query);
$quest = array();
while ($dados = pg_fetch_array($result)) {
$quest[] = $dados;
}
return $quest;
}
function GetOpcUra($db, $pqId)
{
$query = sprintf(GetQueryPesquisa("opcoes"), $pqId);
$result = pg_query($db, $query);
$opc = array();
while ($dados = pg_fetch_array($result)) {
$opc[] = $dados;
}
return $opc;
}
function GetQueryPesquisa($tipo)
{
$org_id = GetOrganizacao();
if ($tipo == 'cabecalio') {
return "SELECT distinct a.pl_id, a.pp_id, a.pl_descricao, a.pl_data_inicio, a.pl_data_fim
FROM pbx_pesquisa_liberacao a, pbx_pesquisa b
WHERE b.pp_id = a.pp_id
AND a.pl_ativa = 1
AND a.org_id = $org_id
ORDER BY pl_id";
} else if ($tipo == 'questoes') {
return "SELECT pq_id, pq_display, pq_questao
FROM pbx_pesquisa_questoes
WHERE pp_id = '%s'
AND pq_status = 1
AND org_id = $org_id
ORDER BY pp_id, pq_id";
} else if ($tipo == 'opcoes') {
return "SELECT po_id, po_tecla, po_opcao
FROM pbx_pesquisa_opcoes
WHERE pq_id = '%s'
AND po_status = 1
AND org_id = $org_id
ORDER BY po_tecla ";
}
}