<?php
include("../sol/sessao.php");
//Verifica permissao
$nivelnecessario = 0;
if ($_SESSION["univel"] > $nivelnecessario)
{
header('Location:/sol/index.php?erroperm='.$nivelnecessario);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SolBT - <?=$sitenome?></title>
<link rel="StyleSheet" type="text/css" href="../sol/includes/estilos.css" id="estilo" />
<script language='JavaScript' src='../sol/includes/FuncoesTela.js' type="text/javascript"></script>
<script language='JavaScript' type="text/javascript">
function AtivaEdicao(campoID)
{
EsconderForms();
document.getElementById('frm' + campoID).style.display = '';
document.getElementById('a' + campoID).style.display = 'none';
document.getElementById('i' + campoID).style.display = 'none';
document.getElementById('edt' + campoID).value = document.getElementById('a' + campoID).innerHTML;
document.getElementById('edt' + campoID).focus();
}
function EsconderForms()
{
for (var i = 0; i < document.forms.length - 1; i++)
{
document.getElementById('frm' + i).style.display = 'none';
document.getElementById('a' + i).style.display = '';
document.getElementById('i' + i).style.display = '';
}
}
function Esconde(campoID)
{
document.getElementById('frm' + campoID).style.display = 'none';
}
function TrimCampo(campoID)
{
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
document.getElementById('edt' + campoID).value = document.getElementById('edt' + campoID).value.trim();
}
</script>
</head>
<body onload="AlertaExclusao();">
<?php
include("../sol/includes/menus.php");
?>
<table width="100%">
<?php
include("../sol/includes/top.php");
?>
<!-- CONTEUDO DA PAGINA -->
<tr>
<td id="Conteudo">
<br />
<span class="Titulo">Cadastro de departamentos</span><br /> <br />
<p id="avisoexc" style="display:none; color:#FF0000;"><em>Este registro não pode ser excluído pois possui lançamentos vinculados a ele.</em></p>
<table cellspacing="1" cellpadding="3" style="background-color:#666666;">
<tr style="color:#FFFFFF; font-weight:bold;">
<td align="center">ID</td><td>Departamento</td>
</tr>
<?php
//Conecta ao banco e abre query
$dados = new dados();
$dados->get_conexao();
$temfilhos = false;
//Atualiza
if (($_GET["acao"] == "editar") || ($_GET["acao"] == "inserir") || ($_GET["acao"] == "excluir"))
{
$idx = str_replace('#', '', $_GET["id"]);
if ($_GET["acao"] == "editar")
{
if (!empty($_POST["edt".$idx]))
{
$sql = "UPDATE departamentos SET departamento = '".TrataStr($_POST["edt".$idx])."' WHERE id = ".$idx;
mysql_query($sql);
}
}
if ($_GET["acao"] == "excluir")
{
//Andamentos
$sql = "SELECT * FROM andamentos WHERE id_departamento = ".$idx;
$rs = mysql_query($sql);
if (mysql_num_rows($rs) > 0) $temfilhos = true;
//Funcoes
//$sql = "SELECT * FROM funcoes WHERE id_departamento = ".$idx;
//$rs = mysql_query($sql);
//if (mysql_num_rows($rs) > 0) $temfilhos = true;
//Funcoes
$sql = "SELECT * FROM usuarios WHERE id_departamento = ".$idx;
$rs = mysql_query($sql);
if (mysql_num_rows($rs) > 0) $temfilhos = true;
if (!$temfilhos)
{
$sql = "DELETE FROM departamentos WHERE id = ".$idx;
mysql_query($sql);
}
}
else
{
if (!empty($_POST["edtCampo"]))
{
$sql = "INSERT INTO departamentos (departamento) VALUES ('".TrataStr($_POST["edtCampo"])."')";
mysql_query($sql);
}
}
}
$sql = "SELECT * FROM departamentos ORDER BY departamento ASC";
$rs = mysql_query($sql);
$i = 1;
while ($reg = mysql_fetch_array($rs))
{
$valorcampo = utf8_encode($reg["departamento"]);
$y = $reg["id"];
?>
<tr style="background-color:#<? if ($i % 2 == 0) echo "E8E8E8"; else echo "FFFFFF"; ?>;">
<td align="center" valign="middle"><?=($y)?></td>
<td valign="middle">
<!-- CAMPO -->
<a id="a<?=($i - 1)?>" style="cursor:pointer;" onclick="AtivaEdicao(<?=($i - 1)?>);"><?=$valorcampo?></a>
<img src="/sol/includes/excluir.gif" border="0" id="i<?=($i - 1)?>" alt="Excluir..." align="top" onclick="if (confirm('Confirmar exclusão de <?=TrataStrForJS($valorcampo)?>?')) window.open('<?=$_SERVER["PHP_SELF"]?>?acao=excluir&id=<?=($y)?>', '_self');" style="cursor:pointer;" title="Excluir..." />
<form action="departamentos.php?acao=editar&id=<?=$y?>" method="post" id="frm<?=($i - 1)?>" onsubmit="TrimCampo(<?=($i - 1)?>);">
<input type="text" id="edt<?=($i - 1)?>" name="edt<?=$y?>" value="<?=$valorcampo?>" maxlength="50" size="50" onblur="TrimCampo(<?=($i - 1)?>);" />
<input type="submit" value="OK" id="btn<?=($i - 1)?>" style="font-weight:bold;" />
<input type="button" value="Cancelar" onclick="EsconderForms();" />
</form>
<!-- FIM DO CAMPO -->
<script language='JavaScript' type="text/javascript">Esconde(<?=($i - 1)?>);</script>
</td>
</tr>
<?php
$i++;
}
?>
</table>
<br /> <br />
<strong>Inserir novo registro</strong><br /> <br />
<form action="departamentos.php?acao=inserir" method="post" id="frmInserir" onsubmit="TrimCampo('Campo');" onreset="document.getElementById('edtCampo').focus();">
Departamento: <input type="text" id="edtCampo" name="edtCampo" value="" maxlength="50" size="50" onblur="TrimCampo('Campo');" onfocus="EsconderForms();" onchange="document.getElementById('resetar').disabled = '';" onkeypress="document.getElementById('resetar').disabled = '';" />
<input type="submit" value="OK" id="btnCampo" style="font-weight:bold;" onclick="document.getElementById('edtCampo').focus();" />
<input type="reset" value="Cancelar" id="resetar" />
</form>
<br /> <br />
</td>
</tr>
<!-- FIM DE CONTEUDO DA PAGINA -->
<?php
include("../sol/includes/rp.php");
?>
</table>
</body>
<script language='JavaScript' type="text/javascript">
function AlertaExclusao()
{
<?php
if ($temfilhos)
{
?>
document.getElementById('avisoexc').style.display = '';
<?php
}
?>
}
</script>
</html>
<?php
@mysql_close();
?>