<?php require_once('../Connections/poblets.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "../index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "Administrador,SuperAdministrador";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "n_noticia.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
$bnoticia = 0;
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if ((isset($_POST['id2'])) && ($_POST['id2'] != "")) {
  $deleteSQL = sprintf("DELETE FROM noticias WHERE Id=%s",
                       GetSQLValueString($_POST['id2'], "int"));

  mysql_select_db($database_poblets, $poblets);
  $Result1 = mysql_query($deleteSQL, $poblets) or die(mysql_error());

  $deleteGoTo = "noticias.php?bnoticia=1";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "radiopublicar")) {
  $updateSQL = sprintf("UPDATE noticias SET Publicada=%s WHERE Id=%s",
                       GetSQLValueString($_POST['Publicar'], "text"),
                       GetSQLValueString($_POST['id_publi'], "int"));

  mysql_select_db($database_poblets, $poblets);
  $Result1 = mysql_query($updateSQL, $poblets) or die(mysql_error());

  $updateGoTo = "admin.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "radiodestacar")) {
  $updateSQL = sprintf("UPDATE noticias SET destacada=%s WHERE Id=%s",
                       GetSQLValueString($_POST['radiodestacar'], "text"),
                       GetSQLValueString($_POST['id_radiodestacar'], "int"));

  mysql_select_db($database_poblets, $poblets);
  $Result1 = mysql_query($updateSQL, $poblets) or die(mysql_error());

  $updateGoTo = "admin.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$maxRows_noticias = 10;
$pageNum_noticias = 0;
if (isset($_GET['pageNum_noticias'])) {
  $pageNum_noticias = $_GET['pageNum_noticias'];
}
$startRow_noticias = $pageNum_noticias * $maxRows_noticias;

mysql_select_db($database_poblets, $poblets);
$query_noticias = "SELECT * FROM noticias ORDER BY Fecha DESC";
$query_limit_noticias = sprintf("%s LIMIT %d, %d", $query_noticias, $startRow_noticias, $maxRows_noticias);
$noticias = mysql_query($query_limit_noticias, $poblets) or die(mysql_error());
$row_noticias = mysql_fetch_assoc($noticias);

if (isset($_GET['totalRows_noticias'])) {
  $totalRows_noticias = $_GET['totalRows_noticias'];
} else {
  $all_noticias = mysql_query($query_noticias);
  $totalRows_noticias = mysql_num_rows($all_noticias);
}
$totalPages_noticias = ceil($totalRows_noticias/$maxRows_noticias)-1;
?>
<!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>CMSPoblets | Editor de noticias</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="logout"><a href="<?php echo $logoutAction ?>">Log out</a></div>
<?php include("cabecera.php"); ?>
<?php include("menu.php"); ?>
<div id="container">
<?php if(isset($_GET['bnoticias']) && ($GET['bnoticias'] == 1)){
echo("Noticia eliminada");
}
?>
<div class="cabeceranoticias">EDITOR DE NOTICIAS</div>
    <?php do { ?>
<table width="100%" border="0" cellspacing="0" cellpadding="1" id="editnot">
  <tr class="titnot">
    <td>Título</td>
    <td><?php echo $row_noticias['Titulo']; ?></td>
  </tr>
  <tr class="autnot">
    <td>Autor</td>
    <td><?php echo $row_noticias['Autor']; ?></td>
  </tr>
  <tr>
    <td class="datenot">Fecha</td>
    <td><?php echo $row_noticias['Fecha']; ?></td>
  </tr>
  <tr>
    <td>Resumen</td>
    <td><?php echo $row_noticias['Resumen']; ?></td>
  </tr>
  <tr>
    <td>Publicada</td>
    <td>
	<?php 
	if (($row_noticias['Publicada'])==('true')) {
	echo ('Esta noticia está publicada') ;
	} else {
	echo ('Esta noticia NO está publicada') ;
	}
	 ?>;
	<form id="radiopublicar"  name="radiopublicar" method="POST" action="<?php echo $editFormAction; ?>">
	  <table class="radio" width="100%">
      <tr>
	      <td>¿Publicar?</td>
	      </tr>
	    <tr>
	      <td><label>
	        <input type="radio" name="Publicar" value="true" id="Publicar_0" 
			<?php if($row_noticias['Publicada'] == 'true') echo "checked"?> />
	        Sí</label></td>
	      </tr>
	    <tr>
	      <td><label>
	        <input type="radio" name="Publicar" value="false" id="Publicar_1" 
			<?php if($row_noticias['Publicada'] == 'false') echo "checked"?> />
	        No</label></td>
	      </tr>
            <td><input name="publi_enviar" type="submit" class="tablafill" value="Enviar" />
              <input name="id_publi" type="hidden" id="id_publi" value="<?php echo $row_noticias['Id']; ?>" />            </td>
	      </tr>
	    </table>
	  <input type="hidden" name="MM_update" value="radiopublicar" />
	</form>
	</td>
    
  </tr>
  <tr>
    <td>Destacada</td>
    <td><?php 
	if (($row_noticias['destacada'])==('true')) {
	echo ('Esta es una noticia destacada') ;
	} else {
	echo ('Esta NO es una noticia destacada') ;
	}
	 ?>;
      <form id="radiodestacar" name="radiodestacar" method="POST" action="<?php echo $editFormAction; ?>">
      
        <table class="radio" width="100%">
        <tr>
            <td>¿Destacar?</td>
          </tr>
          <tr>
            <td><label>
              <input type="radio" name="radiodestacar" value="true" id="radiodestacar_0" 
              <?php if($row_noticias['destacada'] == 'true') echo "checked"?>/>
              Destacado</label></td>
          </tr>
          <tr>
            <td><label>
              <input type="radio" name="radiodestacar" value="false" id="radiodestacar_1" 
              <?php if($row_noticias['destacada'] == 'false') echo "checked"?>/>
              No destacado</label></td>
          </tr>
          <tr>
            <td>
            <input name="Enviar" type="submit" class="tablafill" value="Enviar" />
            <input name="id_radiodestacar" type="hidden" id="id_radiodestacar" value="<?php echo $row_noticias['Id']; ?>" />            </td>
          </tr>
        </table>
        <input type="hidden" name="MM_update" value="radiodestacar" />
      </form>
      </td>
  </tr>
  <tr>
    <td colspan="2" align="right" class="submittable"><span><form style="width:20%; float:right;" id="formborrar" name="formborrar" method="post" action="">
          <input name="id2" type="hidden" id="id2" value="<?php echo $row_noticias['Id']; ?>" />
          <input name="Borrarnot" type="submit" class="tablafill" value="Borrar noticia" />
        </form></span><span><form style="width:10%; float:right;" id="editno" name="editno" method="post" action="edit_noticia.php">
      <input name="id" type="hidden" id="id" value="<?php echo $row_noticias['Id']; ?>" />
      <input name="Editar noticia" type="submit" class="tablafill" value="Editar noticia" />
      </form>      </span><span>
       
      </span>
    
   
      <span>      </span>      </td>
    </tr>
</table>
      <?php } while ($row_noticias = mysql_fetch_assoc($noticias)); ?>

</div>
</body>
</html>
<?php
mysql_free_result($noticias);
?>
