$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); if ($ret['id'] && !$id) { $isImport = importIdTables($dbcon, $ret['id']); if (!$isImport) { throw new Exception("Não foi possível importar tabelas das organizações!"); } criarGruposDefault($dbcon, $ret['id']); addUserOrganizacaoGrupo($dbcon, $ret['id']); } 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 MIN($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 LIMIT 1; \n"; $query .= "ALTER TABLE t{$table} DROP COLUMN $column; \n"; $query .= "UPDATE {$table} SET org_id = $organizacao WHERE $column = $id;"; $query .= "INSERT INTO {$table} SELECT * FROM t{$table} LIMIT 1; \n"; $result = pg_query($dbcon, $query); if (!$result) { throw new Exception("Não foi possível criar a tabela default [ {$table} ]!"); } pg_query($dbcon, "DROP TABLE IF EXISTS t{$table};"); } return true; } function criarGruposDefault($dbcon, $org_id) { $query = "INSERT INTO pbx_grupo (gp_nome, gp_status, gp_user, gp_system, org_id) SELECT gp_nome, gp_status, gp_user, gp_system, $org_id FROM pbx_grupo WHERE gp_nome IN ('ADMINISTRADOR','SUPERVISOR','AGENTE') AND gp_system = 0 AND org_id = 1 RETURNING gp_id, gp_nome"; $result = pg_query($dbcon, $query); $gps = pg_fetch_all($result); foreach ($gps as $g) { $sql = "INSERT INTO pbx_grupo_funcoes (gp_id, fun_id, org_id) SELECT {$g['gp_id']}, fun_id, {$org_id} FROM pbx_grupo_funcoes WHERE gp_nome = '{$g['gp_nome']}' AND gp_system = 0 AND org_id = 1; "; pg_query($dbcon, $sql); } } function addUserOrganizacaoGrupo($dbcon, $org_id) { $iduser = $_SESSION["SSidUser"]; if (!$iduser) { return; } $query = "INSERT INTO pbx_organizacao_usuarios (id_usuario,id_organizacao,updated_at) VALUES($iduser, $org_id,'NOW()');"; $result = pg_query($dbcon, $query); if ($result) { return true; } return false; } 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); }