forked from SimplesIP/pabx-app
douglas.strappasson
1 year ago
8 changed files with 428 additions and 233 deletions
@ -1,115 +1,179 @@
|
||||
<?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); |
||||
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'); |
||||
} |
||||
|
||||
/* |
||||
* Atualiza templates templates |
||||
*/ |
||||
foreach ($fieldsForm as $key => $value) { |
||||
$smarty->assign($key, $value); |
||||
} |
||||
@pg_query($dbcon, 'begin'); |
||||
|
||||
GetTemplate($smarty, $templateName); |
||||
foreach ($field as $idUser) { |
||||
if (AgenteON($idUser)) { |
||||
throw new Exception('Não é possível editar um agente conectado!'); |
||||
} |
||||
|
||||
function AtualizaLicenca($dbcon, $field, $status) { |
||||
$cont = 0; |
||||
|
||||
try { |
||||
|
||||
if($status == 1){ |
||||
$query = "select count(*) as num_licenca from pbx_usuarios where pa_fixo = 1;"; |
||||
$result = pg_query($dbcon, $query); |
||||
$re = pg_fetch_assoc($result)['num_licenca']; |
||||
$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); |
||||
|
||||
if($re >= $_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!'); |
||||
$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('O tempo limite para edição se esgotou, tente novamente mais tarde!'); |
||||
} |
||||
} |
||||
|
||||
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};"; |
||||
|
||||
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); |
||||
$cont++; |
||||
$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!'); |
||||
} |
||||
} |
||||
|
||||
$result = $result && pg_query($dbcon, 'commit'); |
||||
$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); |
||||
|
||||
//print_r($query); |
||||
|
||||
if (!pg_query($dbcon, $query)) { |
||||
throw new Exception('A operação não pode ser realizada!'); |
||||
} |
||||
|
||||
if (!$result) { |
||||
$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!'); |
||||
} |
||||
} |
||||
|
||||
$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)); |
||||
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 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); |
||||
function AtualizaLicencaX($dbcon, $field, $status) { |
||||
$cont = 0; |
||||
|
||||
$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"); |
||||
try { |
||||
|
||||
while (($row = @pg_fetch_array($result))) { |
||||
$sel = ($codSel == $row["id"]) ? "selected" : ""; |
||||
$lista .= "<option value=\"{$row["id"]}\" $sel>{$row["apelido"]}</option>\n"; |
||||
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!'); |
||||
} |
||||
} |
||||
$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); |
||||
if (!$field) { |
||||
fmtMsg('Selecione um usuário!'); |
||||
throw new Exception('Selecione um usuário'); |
||||
} |
||||
|
||||
$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"); |
||||
@pg_query($dbcon, 'begin'); |
||||
|
||||
while (($row = @pg_fetch_array($result))) { |
||||
$sel = ($codSel == $row["apelido"]) ? "selected" : ""; |
||||
$lista .= "<option value=\"{$row["id"]}\" $sel>{$row["apelido"]}</option>\n"; |
||||
foreach ($field as $id) { |
||||
$query = "update pbx_usuarios set pa_fixo = {$status} where id = {$id};"; |
||||
$result = @pg_query($dbcon, $query); |
||||
$cont++; |
||||
} |
||||
$lista .= "</select>\n"; |
||||
return $lista; |
||||
|
||||
$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; |
||||
} |
||||
|
||||
?> |
||||
|
Loading…
Reference in new issue