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.
 
 
 
 
 
 

177 lines
6.3 KiB

<?php
require('funcoesFormulario.php');
$templateName = 'seguranca/licencaUsuarioFixo.tpl';
//define tipo de formulario
$tpLayout = 1;
$fieldsForm = IsPostBack() ? $_POST : $_GET;
/**
* INFORMAÇÕES DO HTML DO SELECT
*/
$infos = [
'size' => 8,
'name' => 'licencafixo',
'width' => '200',
'disable' => '0',
'multiple' => '1'
];
if (isset($fieldsForm['btInclui'])) {
AtualizaLicenca($dbcon, $fieldsForm['licencafixoinc'], 1);
} else if (isset($fieldsForm['btRemove'])) {
AtualizaLicenca($dbcon, $fieldsForm['licencafixorm'], 0);
}
$fieldsForm['listagrupos'] = GetUser($dbcon, 1, $infos);
$fieldsForm['gruposinc'] = GetGrupoUser($dbcon, 1, $infos);
/*
* Atualiza templates templates
*/
foreach ($fieldsForm as $key => $value) {
$smarty->assign($key, $value);
}
GetTemplate($smarty, $templateName);
function AtualizaLicenca($dbcon, $field, $status) {
try {
if (!$field) {
//fmtMsg('Selecione um usuário!');
throw new Exception('Selecione um usuário');
}
@pg_query($dbcon, 'begin');
foreach ($field as $idUser) {
if (AgenteON($idUser)) {
throw new Exception('Não é possível editar um agente conectado!');
}
$query = sprintf("SELECT id_user, last_update, limit_update, NOW() as current_time FROM pbx_licenca_pa_fixo WHERE id_user = '%s' ", $idUser);
$result = pg_query($dbcon, $query);
if ($result && pg_num_rows($result) > 0) {
$result = pg_fetch_assoc($result);
$limitUpdate = strtotime($result['limit_update']);
$freeUpadate = strtotime($result['last_update'] . '+24 hours');
$currentTime = strtotime($result['current_time']);
if ($currentTime > $limitUpdate && $currentTime < $freeUpadate) {
throw new Exception('Não é possível realizar a alteração neste momento, tente novamente mais tarde!');
}
}
if ($status == 1) {
$query = "SELECT COUNT(*) AS qtd_pa_fixo FROM pbx_licenca_pa_fixo WHERE id_user IS NOT NULL;";
$result = pg_query($dbcon, $query);
$result = pg_fetch_assoc($result);
if ($result['qtd_pa_fixo'] >= $_SESSION['prm_max_licenca_fixo']) {
//fmtMsg('A licença de PA Fixo foi excedida!');
throw new Exception('A licença de PA Fixo foi excedida!');
}
}
$query = sprintf("UPDATE pbx_licenca_pa_fixo SET id_user = %s, last_update = NOW(), limit_update = NOW() + INTERVAL '5 MINUTE' ", $status == 1 ? "'" . $idUser . "'" : 'NULL');
$status == 1 ? $query .= "WHERE id = (SELECT id FROM pbx_licenca_pa_fixo WHERE id_user IS NULL ORDER BY id LIMIT 1)" :
$query .= sprintf("WHERE id_user = '%s' ", $idUser);
if (!pg_query($dbcon, $query)) {
throw new Exception('A operação não pode ser realizada!');
}
$query = "update pbx_usuarios set pa_fixo = {$status} where id = {$idUser};";
if (!pg_query($dbcon, $query)) {
throw new Exception('A operação não pode ser realizada!');
}
}
if (!@pg_query($dbcon, 'commit')) {
throw new Exception('A operação não pode ser realizada!');
}
} catch (Exception $e) {
@pg_query($dbcon, 'rollback');
fmtErro($e->getMessage(), pg_last_error($dbcon));
}
}
function AtualizaLicencaX($dbcon, $field, $status) {
$cont = 0;
try {
if ($status == 1) {
$query = "SELECT COUNT(*) AS qtd_pa_fixo FROM pbx_licenca_pa_fixo WHERE matricula IS NOT NULL;";
$result = pg_query($dbcon, $query);
$result = pg_fetch_assoc($result);
if ($result >= $_SESSION['prm_max_licenca_fixo']) {
fmtMsg('A licença de PA Fixo foi excedida!');
throw new Exception('A licença de PA Fixo foi excedida!');
}
}
if (!$field) {
fmtMsg('Selecione um usuário!');
throw new Exception('Selecione um usuário');
}
@pg_query($dbcon, 'begin');
foreach ($field as $id) {
$query = "update pbx_usuarios set pa_fixo = {$status} where id = {$id};";
$result = @pg_query($dbcon, $query);
$cont++;
}
$result = $result && @pg_query($dbcon, 'commit');
if (!$result) {
throw new Exception('A operação não pode ser realizada!');
}
$field['erro'] = $cont > 1 ? fmtMsg('Status removidos com sucesso!') : fmtMsg('Status removido com sucesso!');
} catch (Exception $e) {
@pg_query($dbcon, 'rollback');
fmtErro($e->getMessage(), pg_last_error($dbcon));
}
}
function GetGrupoUser($dbcon, $codSel, $infos) {
$sel = "";
$infos['name'] = $infos['name'] . "rm";
$query = "SELECT * FROM pbx_usuarios WHERE pa_fixo = 1 ORDER BY apelido;";
$result = @pg_query($dbcon, $query);
$lista = '<select name="%s%s" id="%s" size="%s" style="width:%spx;" %s>%s';
$lista = sprintf($lista, $infos['name'], ($infos['multiple'] ? "[]" : ""), $infos['name'], $infos['size'], $infos['width'], ($infos['multiple'] ? 'multiple="multiple"' : ''), "\n");
while (($row = @pg_fetch_array($result))) {
$sel = ($codSel == $row["id"]) ? "selected" : "";
$lista .= "<option value=\"{$row["id"]}\" $sel>{$row["apelido"]}</option>\n";
}
$lista .= "</select>";
return $lista;
}
function GetUser($dbcon, $codSel, $infos) {
$sel = "";
$infos['name'] = $infos['name'] . "inc";
$query = "SELECT * FROM pbx_usuarios WHERE pa_fixo = 0 AND id NOT IN(342,341,356,357,466) ORDER BY apelido;";
$result = @pg_query($dbcon, $query);
$lista = '<select name="%s%s" id="%s" size="%s" style="width:%spx" %s >%s';
$lista = sprintf($lista, $infos['name'], ($infos['multiple'] ? "[]" : ""), $infos['name'], $infos['size'], $infos['width'], ($infos['multiple'] ? 'multiple="multiple"' : ''), "\n");
while (($row = @pg_fetch_array($result))) {
$sel = ($codSel == $row["apelido"]) ? "selected" : "";
$lista .= "<option value=\"{$row["id"]}\" $sel>{$row["apelido"]}</option>\n";
}
$lista .= "</select>\n";
return $lista;
}
?>