<?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
$borrar = 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["MM_insert"])) && ($_POST["MM_insert"] == "form_nevento")) {
  $insertSQL = sprintf("INSERT INTO eventos (Evento, Desde, Hasta, Lugar) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['evento'], "text"),
                       GetSQLValueString($_POST['fechadesde'], "date"),
                       GetSQLValueString($_POST['fechahasta'], "date"),
                       GetSQLValueString($_POST['lugar'], "text"));

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

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

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

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

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

if ((isset($_GET['idborrar'])) && ($_GET['idborrar'] != "")) {
  $deleteSQL = sprintf("DELETE FROM eventos WHERE id=%s",
                       GetSQLValueString($_GET['idborrar'], "int"));

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

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

$maxRows_eventos = 10;
$pageNum_eventos = 0;
if (isset($_GET['pageNum_eventos'])) {
  $pageNum_eventos = $_GET['pageNum_eventos'];
}
$startRow_eventos = $pageNum_eventos * $maxRows_eventos;

mysql_select_db($database_poblets, $poblets);
$query_eventos = "SELECT * FROM eventos ORDER BY Hasta ASC";
$query_limit_eventos = sprintf("%s LIMIT %d, %d", $query_eventos, $startRow_eventos, $maxRows_eventos);
$eventos = mysql_query($query_limit_eventos, $poblets) or die(mysql_error());
$row_eventos = mysql_fetch_assoc($eventos);

if (isset($_GET['totalRows_eventos'])) {
  $totalRows_eventos = $_GET['totalRows_eventos'];
} else {
  $all_eventos = mysql_query($query_eventos);
  $totalRows_eventos = mysql_num_rows($all_eventos);
}
$totalPages_eventos = ceil($totalRows_eventos/$maxRows_eventos)-1;
$delete = 0;
?><!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 | Eventos</title>

<!--<link href="dhtmlgoodies_calendar/dhtmlgoodies_calendar.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="dhtmlgoodies_calendar/dhtmlgoodies_calendar.js"></script>-->

<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['borrar']) && ($GET['borrar'] == 1)){
echo("Evento eliminado");
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="1" id="editnot">
  <tr >
    <td class="cabeceranoticias" colspan="6"><?php if(isset($_GET['delete'])&& $_GET['delete'] == 1){ echo "REGISTRO BORRADO - ";} ?>EDITOR DE EVENTOS</td>
    </tr>
  <tr class="cabeceranoticias">
    <td>Evento</td>
    <td>Desde</td>
    <td>Hasta</td>
    <td>Lugar</td>
    <td>Editar </td>
    <td>Borrar </td>
  </tr>
  <?php do { ?>
    <tr class="autnot">
      <td><?php echo $row_eventos['Evento']; ?></td>
      <td><?php echo $row_eventos['Desde']; ?></td>
      <td><?php echo $row_eventos['Hasta']; ?></td>
      <td><?php echo $row_eventos['Lugar']; ?></td>
      <td><form id="form1" name="form1" method="post" action="eventedit.php">
    
            <input name="edit_user" type="submit" class="tablafill" id="edit_user" value="Editar" />
            <input name="editevent" type="hidden" id="editevent" value="<?php echo $row_eventos['id']; ?>" />
      </form></td>
      <td><form id="form2" name="form2" method="post" action="">
        <input name="borrar" type="submit" class="tablafill" id="borrar" value="Borrar" />
            <input name="idborrar" type="hidden" id="idborrar" value="<?php echo $row_eventos['id']; ?>" />
      </form>
      </td>
    </tr>
    <?php } while ($row_eventos = mysql_fetch_assoc($eventos)); ?>
</table>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form_nevento" id="form_nevento">
<table width="100%" border="0" cellspacing="1" cellpadding="0" id="editnot">
    <tr >
    <td colspan="2" class="cabeceranoticias">INSERTAR EVENTO NUEVO</td>
    </tr>
  <tr>
    <td>Evento</td>
    <td>
      <input name="evento" type="text" class="tablafill" onfocus="value=''" value="Escriba aquí el nombre del evento " size="100%" maxlength="36" />   </td>
  </tr>
  <tr>
    <td>Lugar</td>
    <td><input name="lugar" type="text" class="tablafill" onfocus="value=''" value="Escriba aquí el lugar del evento " size="100%" maxlength="36" /></td>
  </tr>
  <tr>
    <td>Desde</td>
    <td><input name="fechadesde" type="text" id="Desde" onfocus="this.select();lcs(this)" onclick="event.cancelBubble=true;this.select();lcs(this)" value="Haga click para seleccionar fecha" size="40" readonly>
    <script type="text/javascript" src="calendar.js"></script>
</td>
  </tr>
  <tr>
    <td>Hasta</td>
    <td><input name="fechahasta" type="text" id="Hasta" onfocus="this.select();lcs(this)" onclick="event.cancelBubble=true;this.select();lcs(this)" value="Haga click para seleccionar fecha" size="40" readonly>
    <script type="text/javascript" src="calendar.js"></script>
</td>
  </tr>
    <tr>
    <td align="right" class="submittable" colspan="2"><input name="Env_Evento" type="submit" class="tablafill" id="Env_Evento" value="Enviar" /></td>
    </tr>
</table>

<input type="hidden" name="MM_insert" value="form_nevento" />
</form>
</div>
</body>
</html>
<?php
mysql_free_result($eventos);
?>
