forked from SimplesIP/pabx-app
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.
131 lines
4.4 KiB
131 lines
4.4 KiB
<?php |
|
|
|
require_once("util/constantes.php"); |
|
$tpLayout = 1; |
|
|
|
$id = isset($_GET["id"]) ? $_GET["id"] : $_POST["id"]; |
|
$nome = isset($_POST["nome"]) ? $_POST["nome"] : null; |
|
$status = isset($_POST["status"]) ? $_POST["status"] : "checked"; |
|
$master = isset($_POST["master"]) ? "checked" : 0; |
|
|
|
try { |
|
if (isset($_POST["btGravar"])) { |
|
$ret = setOrganizacao($dbcon, ['nome' => $nome, 'status' => $status, 'master' => $master], $id); |
|
} |
|
|
|
if ($id || $ret) { |
|
$dados = getOrganizacaoById($dbcon, $id ? $id : $ret); |
|
list('id' => $id, 'nome' => $nome, 'status' => $status, 'master' => $master) = $dados; |
|
} |
|
|
|
if ($ret) { |
|
$jsStartup[] = "alert('Empresa: $nome foi salva com sucesso!');"; |
|
$jsStartup[] = "window.opener.ResetForm();"; |
|
$jsStartup[] = 'window.close()'; |
|
} |
|
} catch (Exception $e) { |
|
$jsStartup[] = $msg = sprintf("alert('%s');", $e->getMessage()); |
|
} |
|
|
|
$smarty->assign("id", $id); |
|
$smarty->assign("nome", $nome); |
|
$smarty->assign("status", $status ? "checked" : ""); |
|
$smarty->assign("master", $master ? "checked" : ""); |
|
$smarty->assign("acaoUser", $acaoUser); |
|
|
|
GetTemplate($smarty, 'cadastros/organizacao/cadOrganizacao.tpl'); |
|
|
|
function setOrganizacao($dbcon, $dados, $id = null) |
|
{ |
|
$isExists = ExisteRegistro($dbcon, "pbx_organizacao", "nome", strtoupper($dados['nome']), ($id ? " id <> $id" : null)); |
|
if (!$dados['nome'] || $isExists) { |
|
throw new Exception($isExists ? "O nome da organização já existe!" : "Por favor, preencha o campo nome!"); |
|
} |
|
|
|
if ($id) { |
|
$query = "UPDATE pbx_organizacao |
|
SET nome = %s, |
|
status = %s, |
|
master = %s |
|
WHERE id = %s "; |
|
$query = sprintf($query, QuotedStr($dados['nome']), $dados['status'], $dados['master'], $id); |
|
} else { |
|
$query = "INSERT INTO pbx_organizacao (nome, status, master) VALUES(%s, %s, %s) RETURNING id"; |
|
$query = sprintf($query, QuotedStr($dados['nome']), $dados['status'], $dados['master']); |
|
} |
|
|
|
$result = pg_query($dbcon, $query); |
|
if (!$result) { |
|
throw new Exception("Não foi possível gravar as informações da organização!"); |
|
} |
|
$ret = pg_fetch_assoc($result); |
|
return $id == null ? $ret['id'] : $id; |
|
} |
|
|
|
function getOrganizacaoById($dbcon, $id) |
|
{ |
|
$query = "SELECT id, nome, status, master FROM pbx_organizacao WHERE id = $id;"; |
|
$result = pg_query($dbcon, $query); |
|
if (!$result) { |
|
throw new Exception("Não foi possível buscar a organização selecionada!"); |
|
} |
|
$dados = pg_fetch_assoc($result); |
|
return $dados; |
|
} |
|
|
|
function importIdTables($dbcon, $organizacao) |
|
{ |
|
$tables = [ |
|
'pbx_parametros' => 'id', |
|
'pbx_features_featuremap' => 'id', |
|
'pbx_features_general' => 'id', |
|
'pbx_iax_general' => 'id', |
|
'pbx_facilidades' => 'id', |
|
'pbx_voicemail_general' => 'id', |
|
'pbx_sip_general' => 'id', |
|
'pbx_workflow_parametros' => 'wkf_id' |
|
]; |
|
|
|
foreach ($tables as $table => $column) { |
|
if (!$column) { |
|
$column = 'id'; |
|
} |
|
|
|
$sql = "SELECT MAX($column) AS id FROM {$table};"; |
|
$result1 = pg_query($dbcon, $sql); |
|
$resp = pg_fetch_assoc($result1); |
|
|
|
$id = $resp['id']; |
|
if (!$resp['id']) { |
|
$id = 1; |
|
___Gravalog(" [ $sql ] - Não foi possível o ID da tabela {$table};"); |
|
} |
|
|
|
$query = "CREATE TEMPORARY TABLE t{$table} AS SELECT * FROM {$table} WHERE $column = 1;"; |
|
$query .= "ALTER TABLE t{$table} DROP COLUMN $column;"; |
|
$query .= "INSERT INTO {$table} SELECT * FROM t{$table};"; |
|
$query .= "UPDATE {$table} SET org_id = $organizacao WHERE $column = $id;"; |
|
|
|
pg_query($dbcon, $query); |
|
pg_query($dbcon, "DROP TABLE IF EXISTS t{$table};"); |
|
} |
|
} |
|
|
|
function criarGruposDefault($dbcon) |
|
{ |
|
$query = "SELECT gp_id, gp_nome FROM pbx_grupo |
|
WHERE gp_nome IN ('ADMINISTRADOR','SUPERVISOR','AGENTE') |
|
AND gp_system = 0 AND org_id = 1"; |
|
|
|
$query = "SELECT fun_id FROM pbx_grupo_funcoes |
|
WHERE gp_id = 28 |
|
ORDER BY 1"; |
|
} |
|
|
|
function ___Gravalog($message) |
|
{ |
|
$line = "\n----------------------------------------------\n"; |
|
$line .= sprintf("[ %s ] > %s \n", date('Y-m-d H:i:s'), $message); |
|
$line .= "----------------------------------------------"; |
|
file_put_contents('/var/log/asterisk/cadOrganizacao.log', $line, FILE_APPEND); |
|
}
|
|
|