<?php
$idAtiva = isset($_POST['itgm_id']) ? $_POST['itgm_id'] : $_GET['idAtiva'];
$tpLayout = 1;
$formAction = isset($_POST["formAction"]) ? $_POST["formAction"] : (isset($_GET["formAction"]) ? $_GET["formAction"] : 0);
$janW = 590;
$janH = 482;
$nomeJanela = "IntegAtvMetodoParam";
$itgc_id = isset($_GET['itgc_id']) ? $_GET['itgc_id'] : $_POST['itgc_id'];
$itgm_id = isset($_GET['itgm_id']) ? $_GET['itgm_id'] : $_POST['itgm_id'];
$itgm_id_pai = isset($_FORMS['itgm_id_pai']) ? $_FORMS['itgm_id_pai'] : 0;
if (($formAction > 0) & & ($formAction < = FORM_SAVE)) {
$jsPage[] = "admin/js/pbxAjax.js";
$fieldsForm = array('itgm_id' => '1', 'formAction' => '1', 'itgm_nome' => '1', 'itgm_tipo' => '1',
'itgm_comando' => '1', 'itgm_retorno' => '1', 'evento' => '0', 'evento_descricao' => '1', 'opcao' => '0', 'opcao_descricao' => '1',
'acao' => '0', 'acao_descricao' => '1', 'stored_params' => '1', 'stored_params_desc' => '1');
//, 'itgp_nome' => '1', 'itgp_param_disponiveis' => '1', 'itgp_default' => '1'
$fields = array('itgm_id' => '0', 'formAction' => '0', 'itgm_nome' => '', 'itgm_tipo' => '0', 'itgm_comando' => '', 'itgm_retorno' => '0', 'evento' => '0', 'opcao' => '0', 'stored_params' => '');
$nomeTpl = "cadastros/integAtiva/integAtivaMetodoCad.tpl";
if (IsPostBack())
GetDadosPost($fields, $_POST);
if ($formAction == FORM_NEW) {
$fields['formAction'] = FORM_INSERT;
}
if ($formAction == FORM_INSERT) {
$valRetorno = $fields['itgm_retorno'] ? '1' : '0';
$opcao = trim($fields['opcao']);
$evento = trim($fields['evento']);
$msg = '';
if (!$fields['itgm_nome']) {
$msg = 'Informe o nome do parametro!';
} else if (!$fields['itgm_comando']) {
$msg = 'Informe o comando!';
} else if (($valRetorno) & & (!$opcao)) {
$msg = 'Quando o comando retorna um "Retorna ResultSet" <EFBFBD> necess<EFBFBD> rio informar uma "Op<EFBFBD> <EFBFBD> o"';
} else if ((!$valRetorno) & & ($opcao)) {
$msg = 'Quando o comando tem uma Op<EFBFBD> <EFBFBD> o" deve retornar um "Retorna ResultSet"';
} else if (($valRetorno) & & ($evento)) {
$msg = 'Quando o comando estiver associado a um "Evento" n<EFBFBD> o pode retornar um "Retorna ResultSet"!';
} else {
$opcao = IsNullQuoted($opcao);
$evento = IsNullQuoted($evento);
$storedParams = IsNullQuoted($fields['stored_params']);
$result = pg_query($dbcon, 'begin');
$query = "insert into pbx_integracao_metodo(itgc_id, itgm_nome, itgm_comando, itgm_retorno, opcao, evento, stored_params)values(%s,%s,%s,%s,%s,%s,%s)";
$query = sprintf($query, QuotedStr($itgc_id), QuotedStr($fields['itgm_nome']), QuotedStr($fields['itgm_comando']), QuotedStr($valRetorno), $opcao, $evento, $storedParams);
$result = $result & & pg_query($dbcon, $query);
if (!$itgm_id_pai) {
/*
* Cria eventos Abandonada e Atendida para o m<EFBFBD> todo criado.
*/
$itgm_id = GetItgmId($dbcon);
$query = "insert into pbx_integracao_metodo(itgc_id, itgm_nome, itgm_comando, itgm_retorno, opcao, evento, stored_params, itgm_id_pai)values(%s,%s,%s,%s,%s,%s,%s,%s)";
$query = sprintf($query, QuotedStr($itgc_id), QuotedStr('NOEXEC'), QuotedStr('NOEXEC'), '0', 'null', 3, 'null', $itgm_id);
$result = $result & & pg_query($dbcon, $query);
$query = "insert into pbx_integracao_metodo(itgc_id, itgm_nome, itgm_comando, itgm_retorno, opcao, evento, stored_params, itgm_id_pai)values(%s,%s,%s,%s,%s,%s,%s,%s)";
$query = sprintf($query, QuotedStr($itgc_id), QuotedStr('NOEXEC'), QuotedStr('NOEXEC'), '0', 'null', 2, 'null', $itgm_id);
$result = $result & & pg_query($dbcon, $query);
}
if ($result) {
pg_query($dbcon, 'commit');
if ($itgm_id_pai)
$itgm_id = GetItgmId($dbcon);
$msg = 'Registro inserido com sucesso!';
$jsStartup[] = "window.opener.ResetForm();";
$jsJQuery[] = "window.close();";
}
else {
pg_query($dbcon, 'rollback');
$msg = 'N<EFBFBD> o foi poss<EFBFBD> vel realizar a opera<EFBFBD> <EFBFBD> o!';
}
}
}
if ($formAction == FORM_UPDATE) {
$fields['formAction'] = FORM_SAVE;
$query = "select * from pbx_integracao_metodo where itgm_id = $itgm_id";
$query = sprintf($query, QuotedStr($itgm_id));
$result = pg_query($dbcon, $query);
$dados = pg_fetch_array($result);
foreach ($dados as $key => $values) {
if (!is_numeric($key))
$fields[$key] = $values;
}
}
if ($formAction == FORM_SAVE) {
$valRetorno = $fields['itgm_retorno'] ? '1' : '0';
$opcao = trim($fields['opcao']);
$evento = trim($fields['evento']);
$msg = '';
if (!$fields['itgm_nome']) {
$msg = 'Informe o nome do parametro!';
} else if (!$fields['itgm_comando']) {
$msg = 'Informe o comando!';
} else if (($valRetorno) & & (!$opcao)) {
$msg = 'Quando o comando retorna um "Retorna ResultSet" <EFBFBD> necess<EFBFBD> rio informar uma "Op<EFBFBD> <EFBFBD> o"';
} else if ((!$valRetorno) & & ($opcao)) {
$msg = 'Quando o comando tem uma Op<EFBFBD> <EFBFBD> o" deve retornar um "Retorna ResultSet"';
} else if (($valRetorno) & & ($evento)) {
$msg = 'Quando o comando estiver associado a um "Evento" n<EFBFBD> o pode retornar um "Retorna ResultSet"!';
} else {
$opcao = IsNullQuoted($opcao);
$evento = IsNullQuoted($evento);
$storedParams = IsNullQuoted($fields['stored_params']);
$query = "update pbx_integracao_metodo set itgm_nome = %s, itgm_comando = %s, itgm_retorno = %s, opcao = %s, stored_params = %s where itgm_id = %s";
$query = sprintf($query, QuotedStr($fields['itgm_nome']), QuotedStr($fields['itgm_comando']), QuotedStr($valRetorno), $opcao, $storedParams, QuotedStr($itgm_id));
$result = pg_query($dbcon, $query);
if ($result) {
$msg = 'Registro atualizado com sucesso!';
$jsStartup[] = "window.opener.ResetForm();";
$jsJQuery[] = "window.close();";
} else {
$msg = 'N<EFBFBD> o foi poss<EFBFBD> vel realizar a opera<EFBFBD> <EFBFBD> o!';
}
}
}
$fields['evento_descricao'] = GetItegEvento($fields['evento']);
$fields['opcao_descricao'] = GetOpcaoAlt($dbcon, $fields['opcao']);
$fields['stored_params_desc'] = GetParamFromCmd($fields['stored_params']);
foreach ($fields as $key => $value) {
if ($fieldsForm[$key])
$smarty->assign($key, $value);
}
$smarty->assign("itgc_id", $itgc_id);
$smarty->assign("itgm_id", $itgm_id);
$smarty->assign("itgm_id_pai", $itgm_id_pai);
$smarty->assign("evento", $fields['evento']);
GetTemplate($smarty, $nomeTpl);
if ($msg)
$jsStartup[] = "alert('$msg');";
$jsJQuery[] = "\n$('#evento').change( function(){\$$('opcao').selectedIndex = 0; \$$('itgm_retorno').checked = false;})";
$jsJQuery[] = "\n$('#itgp_param_disponiveis').change(
function ()
{
if ($('#itgp_param_disponiveis').val() != 5)
{
$('#itgp_default').attr('disabled', true);
$('#itgp_default').val('');
}
else
{
$('#itgp_default').attr('disabled', false);
}
}
)";
}
else {
if ($formAction == FORM_DELETE) {
$idDel = $_GET["id"];
/*
* Recupera o id da configura<EFBFBD> <EFBFBD> o antes de apagar o metodo
*/
$query = "select itgc_id from pbx_integracao_metodo where itgm_id = '$idDel'";
$result = pg_query($dbcon, $query);
$row = @pg_fetch_array($result);
$itgc_id = $row[0];
$result = pg_query($dbcon, 'begin');
if ($itgm_id_pai) {
$query = "update pbx_integracao_metodo set itgm_nome = 'NOEXEC', itgm_comando = 'NOEXEC', stored_params = '' where itgm_id = '$idDel' and coalesce(itgm_id_pai, 0) < > 0";
$result = $result & & pg_query($dbcon, $query);
} else {
$query = "delete from pbx_integracao_metodo where itgm_id_pai = '$idDel'";
$result = $result & & pg_query($dbcon, $query);
$query = "delete from pbx_integracao_metodo where itgm_id = '$idDel'";
$result = $result & & pg_query($dbcon, $query);
}
if ($result) {
pg_query($dbcon, 'commit');
$jsJQuery[] = $result ? "alert('Registro apagado com sucesso!');" : "alert('Erro ao tentar remover registro!');";
} else {
pg_query($dbcon, 'rollback');
//$erros = error_get_last();
$msg = 'A opera<EFBFBD> <EFBFBD> o n<EFBFBD> o pode ser realizada!'; // Erro: ' . $erros['message'];
$jsJQuery[] = "alert('$msg');";
}
}
$colspan = 6;
$linha = ' < tr >
< td align = "right" > %s< / td >
< td align = "left" nowrap > %s< / td >
< td width = "30" align = "center" > %s< / td >
< td width = "35" align = "center" > %s< / td >
< td width = "35" align = "center" > %s< / td >
< td width = "35" align = "center" > %s< / td >
%s
< / tr > ';
$linhaEmpty = "< tr > < td align = \"center\" colspan = \"$colspan\" > %s< / td > < / tr > ";
$insert = $itgm_id_pai ? '< img src = "imgSite/novoUserBloq.png" align = "absmiddle" border = "0" / > ' : GetLinkFormInsert($nomeJanela, $imagem = "", $addParam = "&itgc_id=$itgc_id&itgm_id=0");
$query = "select itgm_id, itgc_id, itgm_nome, itgm_tipo, itgm_comando, itgm_retorno, itgm_status, evento from pbx_integracao_metodo where itgc_id=$itgc_id and coalesce(itgm_id_pai,0) = $itgm_id_pai order by itgm_id";
//if(IsAdmin()) echo $query;
$result = pg_query($dbcon, $query);
$linhas = '';
$update = '';
$delete = '';
$retorno = '';
$inc = 0;
while ($dados = pg_fetch_array($result)) {
$id = $dados['itgm_id'];
$retorno = sprintf('< img src = "imgSite/%s" align = "absmiddle" border = "0" / > ', $dados['itgm_retorno'] ? 'button_ok16.png' : 'fecha16.png');
$update = GetLinkFormUpdate("& itgc_id=$itgc_id& itgm_id=$id& itgm_id_pai=$itgm_id_pai", $nomeJanela);
$idDelete = "$id|$itgm_id_pai";
$delete = GetLinkFormDelete($idDelete, 'DeletaIntegAtivaMetodo');
$imgEvt = '< img src = "imgSite/evento.png" align = "absmiddle" border = "0" / > ';
$evtLink = sprintf("javaScript:NovaJanela('index.php?idProg=191& formAction=0& itgc_id=%s& itgm_id_pai=%s', 'IntegAtivaMetodo', '500', '340', 'resizable=NO,scrollbars=NO');", $itgc_id, $id);
$evtLink = $itgm_id_pai ? '' : sprintf('< td width = "35" align = "center" > %s< / td > ', sprintf('< a href = "%s" > %s< / a > ', $evtLink, $imgEvt));
$nomeEvt = '';
if ($itgm_id_pai) {
$nomeEvt = '-';
$nomeEvt .= ($dados['evento'] == 3) ? "Atendimento" : "Encerramento";
}
$linhas .= sprintf($linha, $id, $dados['itgm_nome'] . $nomeEvt, $retorno, $insert, $update, $delete, $evtLink);
$inc++;
}
if (!$inc) {
$linhas = sprintf($linhaEmpty, "Nenhum parametro cadastrado! " . $insert);
}
$nomeTpl = "cadastros/integAtiva/integAtivaMetodo.tpl";
$smarty->assign("linhas", $linhas);
$smarty->assign("itgc_id", $itgc_id);
$smarty->assign("itgm_id_pai", $itgm_id_pai);
$smarty->assign("itgm_colspan", $itgm_id_pai ? 3 : 4);
GetTemplate($smarty, $nomeTpl);
}
function GetParamFromCmd($cmd) {
$params = explode('@', $cmd);
$ret = "";
$sel = 'selected="selected"';
$ini = '';
foreach ($params as $param) {
$paramValue = explode('|', $param);
if (!$ini)
$ini = $param;
$nome = $paramValue[2];
$ret .= sprintf('< option value = "%s" $ sel > %s< / option > %s', $param, $nome, "\n");
$sel = '';
}
$GLOBALS['jsStartup'][] = "SetParamInteg('$ini')";
return $ret;
}
function GetItgmId($dbcon) {
$query = "select currval('pbx_integracao_metodo_itgm_id_seq')";
$result = pg_query($dbcon, $query);
$row = @pg_fetch_row($result);
return $row[0];
}
?>