<?
require("acesso.php");
$menu = "conteudos";
$tipo_de_conteudo = "conteudos";
$acao = $_GET[acao];
#Se acao for igual a apagar, apaga o registro
if($acao=="apagar") {
	#Pega o ID do conte&uacute;do pelo endere&ccedil;o da p&aacute;gina
	$id = $_GET[id];
	#Executa a Query q vai apagar o registro
	$query_deleta = "DELETE FROM conteudos WHERE id='$id'";
	$deleta = mysql_query($query_deleta);
	if (!$deleta) {
	   header("Location: conteudos.php?info=falhou&causa=".urlencode('Ocorreu um Erro!'));
	   exit;
	}
	else {
	   header("Location: conteudos.php?info=ok&causa=".urlencode('Conte&uacute;do apagado com sucesso!'));
	   exit;
	}
#Verifica se acao é igual a salvar, se for verdadeira salva o registro
} elseif ($acao=="salvar") {
	#Pega o ID do conte&uacute;do pelo hidden da p&aacute;gina
	$id = $_POST[id];
	if ($id!="") {
		$titulo = $_POST['titulo'];
		$conteudo = $_POST['conteudo'];
		if ($titulo) {
			$query = "UPDATE conteudos SET titulo='$titulo', conteudo='$conteudo' WHERE id='$id'";
		} else {
			$query = "UPDATE conteudos SET conteudo='$conteudo' WHERE id='$id'";
		}
		$run = mysql_query($query);
		if (!$run) {
			header("Location: conteudos.php?info=falhou&causa=".urlencode('Ocorreu um Erro na Atualiza&ccedil;&atilde;o'));
			exit;
		}
		else {
			header("Location: conteudos.php?info=ok&causa=".urlencode('Conte&uacute;do salvo com sucesso!'));
			exit;
		}
	#Salvar
	} else {
		$titulo = $_POST['titulo'];
		$conteudo = $_POST['conteudo'];
		$query = "INSERT INTO conteudos VALUES (NULL, '$titulo', '$conteudo')";
		$run = mysql_query($query);
		if (!$run) {
			header("Location: conteudos.php?info=falhou&causa=".urlencode('Ocorreu um Erro na Inclus&atilde;o!'));
			exit;
		}
		else {
			header("Location: conteudos.php?info=ok&causa=".urlencode('Conte&uacute;do salvo com sucesso!'));
			exit;
		}
	}
#Verifica se acao é igual a editar, assim abre o formul&aacute;rio de edi&ccedil;ao
} elseif ($acao=="adicionar") {
include("topo.php");
if($_COOKIE["nivel"] == "1") {
#Inicia o Editor
include_once("fckeditor/fckeditor.php");
?>
	<script src="js/conteudos.js" type="text/javascript"></script> 
    <form name="ficha" method="post" enctype="multipart/form-data" action="conteudos.php?acao=salvar" onSubmit="return enviardados();">
	<table width="775" border="0" cellspacing="0" cellpadding="0" align="center" style="margin-top:25px; background:url(images/fundo_painel.jpg) bottom right no-repeat;">
		<tr>
			<td>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:3px #F2F2F2 double; padding:5px 10px 0px 10px; background-color:#FFFFFF;">
				  <tr>
					<td><img src="images/icones/conteudos.jpg" align="absmiddle" /><span class="titulo">Adicionar Conte&uacute;do do Site</span></td>
					<td width="100" align="center" class="texto"><input name="Salvar" type="image" src="images/icones/salvar.jpg" /><br />Salvar</a></td>
					<td width="100" align="center" class="texto"><a href="conteudos.php"><img src="images/icones/conteudos.jpg" border="0" /><br />Voltar</a></td>
				  </tr>
				</table>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto" style="margin-top:20px;">
				  <tr>
					<td height="27" colspan="2" align="left" style="padding:10px;">
						<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border:3px #F2F2F2 double;">
						  <tr>
							<td width="43"><img src="images/icones/info.jpg" width="33" height="33" /></td>
							<td>Adicione um conte&uacute;do de texto no site. Ap&oacute;s a inclus&atilde;o ter sido salva neste painel, as informa&ccedil;&otilde;es j&aacute; estar&atilde;o vigorando no conteudos acess&iacute;vel para o p&uacute;blico em geral.</td>
						  </tr>
						</table>
					</td>
				  </tr>
				  <tr>
					<td width="130" height="27" align="right">T&iacute;tulo: </td>
					<td width="645" align="left"><input name="titulo" id="titulo" class="inputbox" type="text" /></td>
				  </tr>
				  <tr>
					<td width="130" height="27" align="right">Conte&uacute;do: </td>
					<td width="645" align="left">
						<?php
						$oFCKeditor = new FCKeditor('conteudo');
						$oFCKeditor->BasePath = 'fckeditor/';
						$oFCKeditor->Value = $conteudo;
						$oFCKeditor->Create();
						?>
					</td>
				  </tr>
				  <tr>
					<td width="130" align="left">&nbsp;</td>
					<td width="645" align="left" align="left" style="padding-top:15px;"><input name="Salvar" type="image" src="images/icones/salvar.jpg" /></td>
				  </tr>
			  </table>
			</td>
		</tr>
	</table>
	</form>
<? } else { ?>
	<table width="775" border="0" cellspacing="0" cellpadding="0" align="center" style="margin-top:25px; background:url(images/fundo_painel.jpg) bottom right no-repeat;">
		<tr>
			<td>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:3px #F2F2F2 double; padding:5px 10px 0px 10px; background-color:#FFFFFF;">
				  <tr>
					<td><img src="images/icones/conteudos.jpg" align="absmiddle" /><span class="titulo">Adicionar Conte&uacute;do do Site</span></td>
					<td width="100" align="center" class="texto">&nbsp;</td>
					<td width="100" align="center" class="texto"><a href="conteudos.php"><img src="images/icones/conteudos.jpg" border="0" /><br />Voltar</a></td>
				  </tr>
				</table>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto" style="margin-top:20px;">
				  <tr>
					<td height="27" colspan="2" align="left" style="padding:10px;">
						<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border:3px #F2F2F2 double;">
						  <tr>
							<td width="43"><img src="images/icones/aviso.jpg" width="36" height="33" /></td>
							<td>Voc&ecirc; n&atilde;o tem permiss&atilde;o para acessar esta &aacute;rea.</td>
						  </tr>
						</table>
					</td>
				  </tr>
			  </table>
			</td>
		</tr>
	</table>
	</form>
<?
}
#Verifica se acao é igual a editar, assim abre o formul&aacute;rio de edi&ccedil;ao
} elseif ($acao=="editar") {
	#Pega o ID do conte&uacute;do pelo endere&ccedil;o da p&aacute;gina
	$id = $_GET['id'];
	#Faz a sele&ccedil;&atilde;o no banco para a listagem do conte&uacute;do que deseja editar
	$query = "SELECT * FROM conteudos WHERE id='$id'";
	$resultado = mysql_query($query);
	$monta_select = mysql_fetch_array($resultado);
	#Faz a cria&ccedil;ao das var&iacute;aveis pelo array do SELECT acima
	$titulo = $monta_select["titulo"];
	$conteudo = $monta_select["conteudo"];
include("topo.php");
#Inicia o Editor
include_once("fckeditor/fckeditor.php");
?>
	<script src="js/conteudos.js" type="text/javascript"></script> 
    <form name="ficha" method="post" enctype="multipart/form-data" action="conteudos.php?acao=salvar" onSubmit="return enviardados();">
    <table width="775" border="0" cellspacing="0" cellpadding="0" align="center" style="margin-top:25px; background:url(images/fundo_painel.jpg) bottom right no-repeat;">
        <tr>
            <td>
                <table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:3px #F2F2F2 double; padding:5px 10px 0px 10px; background-color:#FFFFFF;">
                  <tr>
                    <td><img src="images/icones/conteudos.jpg" align="absmiddle" /><span class="titulo">Editar Conte&uacute;do do Site</span></td>
                    <td width="100" align="center" class="texto"><input name="Salvar" type="image" src="images/icones/salvar.jpg" /><br />Salvar</a></td>
                    <td width="100" align="center" class="texto"><a href="conteudos.php"><img src="images/icones/conteudos.jpg" border="0" /><br />Voltar</a></td>
                  </tr>
                </table>
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto" style="margin-top:20px;">
                  <tr>
                    <td height="27" colspan="2" align="left" style="padding:10px;">
                        <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border:3px #F2F2F2 double;">
                          <tr>
                            <td width="43"><img src="images/icones/info.jpg" width="33" height="33" /></td>
                            <td>Edite o conte&uacute;do da p&aacute;gina selecionada. Ap&oacute;s a altera&ccedil;&atilde;o ter sido salva neste painel, as mesmas j&aacute; estar&atilde;o vigorando no conteudos acess&iacute;vel para o p&uacute;blico em geral.</td>
                          </tr>
                        </table>
                    </td>
                  </tr>
                  <tr>
                    <td width="130" height="27" align="right">T&iacute;tulo: </td>
                    <td width="645" align="left">
                    	<? if($_COOKIE["nivel"] == "1") { ?>
                    	<input name="titulo" id="titulo" class="inputbox" value="<?php echo $titulo; ?>" type="text" /><input name="id" type="hidden" value="<?php echo $id; ?>">
                        <? } else { ?>
                    	<strong><?php echo $titulo; ?></strong><input name="titulo" id="titulo" value="<?php echo $titulo; ?>" type="hidden" /><input name="id" type="hidden" value="<?php echo $id; ?>">
                        <? } ?>
					</td>
                  </tr>
                  <tr>
                    <td width="130" height="27" align="right">Conte&uacute;do: </td>
                    <td width="645" align="left">
						<?php
                        $oFCKeditor = new FCKeditor('conteudo');
                        $oFCKeditor->BasePath = 'fckeditor/';
                        $oFCKeditor->Value = $conteudo;
                        $oFCKeditor->Create();
                        ?>
					</td>
                  </tr>
                  <tr>
                    <td width="130" align="left">&nbsp;</td>
                    <td width="645" align="left" align="left" style="padding-top:15px;"><input name="Salvar" type="image" src="images/icones/salvar.jpg" /></td>
                  </tr>
              </table>
            </td>
        </tr>
    </table>
    </form>
<?php
#Se acao estiver vazio faz a lista do conte&uacute;do (nao sendo verdaderio em nenhum dos if e elseif acima)
} else {
	#Faz a sele&ccedil;ao no banco para a listagem de TODO o conteudo
	$query = "SELECT * FROM conteudos ORDER BY id ASC";
	$resultado = mysql_query($query);
	$monta_select = mysql_fetch_array($resultado);
include("topo.php");
?>
	<script src="js/conteudos.js" type="text/javascript"></script> 
	<table width="775" border="0" cellspacing="0" cellpadding="0" align="center" style="margin-top:25px; background:url(images/fundo_painel.jpg) bottom right no-repeat;">
		<tr>
			<td>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:3px #F2F2F2 double; padding:5px 10px 0px 10px; background-color:#FFFFFF;">
				  <tr>
					<td><img src="images/icones/conteudos.jpg" align="absmiddle" /><span class="titulo">Administrar Conte&uacute;dos do Site</span></td>
					<? if($_COOKIE["nivel"] == "1") { ?>
                    <td width="100" align="center" class="texto"><a href="conteudos.php?acao=adicionar"><img src="images/icones/add.jpg" border="0" /><br />Cadastrar Conte&uacute;do</a></td>
                    <? } ?>
				  </tr>
				</table>
				<?
				if($_GET['info'] == "falhou") {
					$erro = $_GET['causa'];
				?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding:15px;">
				  <tr>
					<td align="center"><img src="images/icones/aviso.jpg" align="absmiddle"/><span class="erro"><? echo $erro; ?></span></td>
				  </tr>
				</table>
				<? } 
				if($_GET['info'] == "ok") {
					$ok = $_GET['causa'];
				?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding:15px;">
				  <tr>
					<td align="center"><img src="images/icones/aviso.jpg" align="absmiddle"/><span class="ok"><? echo $ok; ?></span></td>
				  </tr>
				</table>
				<? } ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto" style="border:1px solid #747474; margin-top:10px;">
				  <tr>
					<td align="center" height="30" width="60" <? if($_COOKIE["nivel"] == "1") { ?>colspan="2"<? } ?> style="border-bottom:1px solid #747474;"><strong>A&ccedil;&atilde;o</strong></td>
					<td style="border-bottom:1px solid #747474;" width="715"><strong>T&iacute;tulo</strong></td>
				  </tr>
				  <?php
					if ($monta_select == "") {
						echo "<tr><td colspan=\"3\" align=\"center\" valign=\"middle\" height=\"30\"><strong>N&atilde;o existem conte&uacute;dos cadastrados</strong></td></tr>";
					} else {
					#In&iacute;cio do Loop que ir&aacute; montar a tabela dos registros
					do {
						$id = $monta_select["id"];
						$titulo = $monta_select["titulo"];

						#Imprime a linha da tabela dos conte&uacute;dos
						echo "<tr>\n";
						if($_COOKIE["nivel"] == "1") {
                            echo "<td width=\"30\" height=\"32\" style=\"border-bottom:1px solid #CCCCCC;\" align=\"center\"><a href=\"conteudos.php?acao=editar&id=$id\"><img src=\"images/icones/editar_mini.png\" border=\"0\" alt=\"Editar\" title=\"Editar\" /></a></td>\n";
                            echo "<td width=\"30\" style=\"border-bottom:1px solid #CCCCCC;\"><a href=\"#\" onclick=\"confirma('conteudos.php?acao=apagar&id=$id')\"><img src=\"images/icones/apagar_mini.png\" border=\"0\" alt=\"Apagar\" title=\"Apagar\" /></a></td>\n";
						} else {
                            echo "<td width=\"60\" height=\"32\" style=\"border-bottom:1px solid #CCCCCC;\" align=\"center\"><a href=\"conteudos.php?acao=editar&id=$id\"><img src=\"images/icones/editar_mini.png\" border=\"0\" alt=\"Editar\" title=\"Editar\" /></a></td>\n";
						}
						echo "<td style=\"border-bottom:1px solid #CCCCCC;\">$titulo</td>\n";
						echo "</tr>\n";
					}
					while ($monta_select = mysql_fetch_array($resultado));
					}
				  ?>
				</table>
			</td>
		</tr>
	</table>
<?php
}
include("rodape.php");
?>