<?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
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;
}
}

mysql_select_db($database_poblets, $poblets);
$query_users = "SELECT * FROM users WHERE registro = 'no'";
$users = mysql_query($query_users, $poblets) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
$totalRows_users = mysql_num_rows($users);

$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 WHERE Publicada = 'false' ORDER BY Id 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;


/*PRUEBAS*/
$maxRows_ultimas = 2;
$pageNum_ultimas = 0;
if (isset($_GET['pageNum_noticias'])) {
  $pageNum_ultimas = $_GET['pageNum_noticias'];
}
$startRow_ultimas = $pageNum_ultimas * $maxRows_ultimas;

mysql_select_db($database_poblets, $poblets);
$query_ultimas = "SELECT * FROM noticias ORDER BY Id DESC ";
$query_limit_ultimas = sprintf("%s LIMIT %d, %d", $query_ultimas, $startRow_ultimas, $maxRows_ultimas);
$ultimas = mysql_query($query_limit_ultimas, $poblets) or die(mysql_error());
$row_ultimas = mysql_fetch_assoc($ultimas);

if (isset($_GET['totalRows_noticias'])) {
  $totalRows_ultimas = $_GET['totalRows_noticias'];
} else {
  $all_ultimas = mysql_query($query_ultimas);
  $totalRows_ultimas = mysql_num_rows($all_ultimas);
}
$totalPages_ultimas = ceil($totalRows_ultimas/$maxRows_ultimas)-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</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">
<table id="adminbox">
  <tr>
    <td>
    <span class="postboton">
				<span class="posttext"><a href="noticias.php"><img src="../img/not_dest.png" alt="noticias" />
				<?php
					if((isset($totalRows_noticias)) && ($totalRows_noticias == "0" )) {
						echo "No hay noticias nuevas o por aceptar";
						} else {
						echo "Hay un total de $totalRows_noticias noticias por aceptar";
					}
					?></a></span>	</span>
		<span class="postboton">
			<span class="posttext"><a href="usuarios.php"><img src="../img/users.png" alt="usuarios" /><br /><?php
				if((isset($totalRows_users)) && ($totalRows_users == "0" )) {
					echo "No hay nuevos usuarios registrados o por aceptar";
					} else {
					echo "Hay un total de $totalRows_users usuarios nuevos o esperando a ser aceptados";
				}
				?> </a>            </span>		</span>
			
    <span class="postboton">
			<span class="posttext"><a href="gestor.php"><img src="../img/docs.png" alt="documentos" />¿DESEA AÑADIR NUEVOS DOCUMENTOS?
            </a></span>		</span>

		<span class="postboton">
			<span class="posttext"><a href="images.php"><img src="../img/foto.png" alt="foto" />¿DESEA AÑADIR NUEVAS IMAGENES A LA GALERIA?
            </a></span>		</span>

	
		<span class="postboton">
			<span class="posttext"><a href="../index.php" target="_blank"><img src="../img/home.png" alt="home" /><br />Ver la pagina principal</a> </span>		</span>       </td>
  </tr>
</table>
<table cellpadding="0" cellspacing="0" id="editnot">
       <tr>
    <td colspan="2" class="cabeceranoticias">ÚLTIMAS NOTICIAS</td>
    </tr>
     <?php do { ?>
       <tr class='titnot'>
         <td>Titulo</td>
         <td><?php echo $row_ultimas['Titulo']; ?></td>
         </tr>
         <tr>
         <td>Autor</td>
         <td><?php echo $row_ultimas['Autor']; ?></td>
         </tr>
         <tr>
         <td>Fecha</td>
         <td><?php echo $row_ultimas['Fecha']; ?></td>
         </tr>
         <tr>
         <td>Resumen</td>
         <td><?php echo $row_ultimas['Resumen']; ?></td>
         </tr>
         <?php } while ($row_ultimas = mysql_fetch_assoc($ultimas)); ?>
     </table>
  <!-- Fin del container --> 
</div>
    
</body>
</html>
<?php
mysql_free_result($noticias);

mysql_free_result($users);
?>


