<?php
// Controles sobre añadir y edicion de "Disponible" || Controls on adding and editing "Available"
$return = true; // Valor default exit

$values['lastDateUpdated'] = now();
$values['lastUserUpdated'] = $_SESSION['login'];

//  Control de rango de fechas | Date Range Control
if ($values['startDate'] >= $values['endDate']){
		$error = GetCustomLabel("MEDIA_ERROR1"); //  Rango mal definido |  Poorly defined range
		$message = $error;
		$return = false;
    return ;
}
$V_media_idmedia = $values['media_idmedia'];
$V_startDate = $values['startDate'];
$V_endDate = $values['endDate'];
$V_idavailability = $keys['idavailability'];
// if EDIT
if ( $V_idavailability <> '' ) {
		$V_idavailability = " and idavailability <> $V_idavailability ";
		} else {
		$V_idavailability = '';
		}

$strSQL = "SELECT idavailability, media_idmedia, startDate, endDate
FROM availability
where
media_idmedia = $V_media_idmedia and
startDate < '$V_endDate' and
endDate > '$V_startDate' $V_idavailability ";
$rsSQL = db_query($strSQL,$conn);

$SQL_cnt = $rsSQL->num_rows;

if ($SQL_cnt <> 0){
		$error = GetCustomLabel("MEDIA_ERROR2"); //  Rango se solapa con otro ya existente | Range overlaps with an existing one
		$message = $error;
		$return = false;
    return ;
}

// Si existen reservas no se puede modificar ||  If there are reservations it cannot be modified
if ( $V_idavailability <> '' ) {
$V_idavailability = $keys['idavailability'];
$strSQL = "SELECT * FROM reservations WHERE availability_idavailability = $V_idavailability ";
$rsSQL = db_query($strSQL,$conn);
$SQL_cnt = $rsSQL->num_rows;
if ($SQL_cnt <> 0){
		$error = GetCustomLabel("MEDIA_ERROR3"); //  Si existen reservas no se puede modificar ||  If there are reservations it cannot be modified
		$message = $error;
		$return = false;
    return ;
}
}

?>