SW/assets/php/includes/session_dao.php

176 lines
7.4 KiB
PHP
Raw Normal View History

2021-05-26 09:03:02 +02:00
<?php
include_once('session.php');
class SessionDAO extends DAO {
//Constructor:
function __construct($bd_name){
parent::__construct($bd_name);
}
2021-06-04 11:36:15 +02:00
//Create a new Session taking the new id,film, hall, cinema, date, start time, seat price and format saving in the database
2021-05-26 09:03:02 +02:00
public function createSession($id, $idfilm, $idhall, $idcinema, $date, $startTime, $seatPrice, $format){
$format = $this->mysqli->real_escape_string($format);
$date = date('Y-m-d', strtotime( $date ) );
$startTime = date('H:i:s', strtotime( $startTime ) );
$sql = sprintf( "INSERT INTO `session` (`id`, `idfilm`, `idhall`, `idcinema`, `date`, `start_time`, `seat_price`, `format`, `seats_full`)
VALUES ('%d', '%d', '%d', '%d', '%s', '%s', '%d', '%s', '%d')",
$id, $idfilm, $idhall, $idcinema, $date, $startTime, $seatPrice, $format, "0");
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error createSession');
2021-05-26 09:03:02 +02:00
return $sql;
}
//Returns a query to get the session's data.
public function sessionData($id){
$sql = sprintf( "SELECT * FROM `session` WHERE id = '%d'", $id );
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error accessing to the session with id '. $id);
2021-05-26 09:03:02 +02:00
while($fila=$resul->fetch_assoc()){
$session = $this->loadSession($fila["id"], $fila["idfilm"], $fila["idhall"], $fila["idcinema"], $fila["date"], $fila["start_time"], $fila["seat_price"], $fila["format"], $fila["seats_full"]);
}
$resul->free();
return $session;
}
2021-06-06 13:33:40 +02:00
//Look for a film with the id film
2021-05-26 09:03:02 +02:00
public function filmTittle($idfilm){
$sql = sprintf("SELECT * FROM film JOIN session ON film.id = session.idfilm WHERE session.idfilm = '%d' ", $idfilm );
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error accessing to the film with id '. $idfilm);
2021-05-26 09:03:02 +02:00
$resul = mysqli_fetch_array($resul);
return $resul;
}
2021-06-04 11:36:15 +02:00
//Look for a session with the primary key
2021-05-26 09:03:02 +02:00
public function searchSession($cinema, $hall, $startTime, $date){
$date = date('Y-m-d', strtotime( $date ) );
$startTime = date('H:i:s', strtotime( $startTime ) );
2021-06-06 13:33:40 +02:00
2021-05-26 09:03:02 +02:00
$sql = sprintf( "SELECT * FROM session WHERE
idcinema = '%s' AND idhall = '%s' AND date = '%s' AND start_time = '%s'",
$cinema, $hall, $date, $startTime);
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error searching for a session');
2021-05-26 09:03:02 +02:00
$session = mysqli_fetch_array($resul);
mysqli_free_result($resul);
return $session;
}
2021-06-06 13:33:40 +02:00
public function searchSessionActivesAtStartTimeAndFilmDuration($cinema, $hall, $startTime, $date, $idfilm){
$date = date('Y-m-d', strtotime( $date ) );
$startTime = date('H:i:s', strtotime( $startTime ) );
$sessions = [];
$sql = sprintf("SELECT duration FROM film WHERE id='%s'", $idfilm );
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error looking for the film duration of id '. $idfilm);
2021-06-06 13:33:40 +02:00
$duration = ($resul->fetch_assoc())["duration"]+10;
2021-06-07 23:16:22 +02:00
$endHour = date('H:i:s', strtotime( $startTime . ' +'.$duration.' minute'));
2021-06-06 13:33:40 +02:00
$sql = sprintf( "SELECT * FROM session WHERE
idcinema = '%s' AND idhall = '%s' AND date = '%s' AND start_time BETWEEN '%s' AND '%s' ORDER BY start_time ASC;",
$cinema, $hall, $date, $startTime, $endHour);
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error looking for sessions between start time and start time + film duration');
2021-06-06 13:33:40 +02:00
while($fila=$resul->fetch_assoc()){
$sessions[] = $this->loadSession($fila["id"], $fila["idfilm"], $fila["idhall"], $fila["idcinema"], $fila["date"], $fila["start_time"], $fila["seat_price"], $fila["format"], $fila["seats_full"]);
}
mysqli_free_result($resul);
return $sessions;
}
2021-05-26 09:03:02 +02:00
//Returns a query to get all the session's data.
2021-06-03 11:32:27 +02:00
public function getAllSessions($hall, $cinema, $date, $end){
if($end){
2021-06-07 23:16:22 +02:00
2021-06-03 11:32:27 +02:00
$sql = sprintf( "SELECT * FROM session WHERE
idcinema = '%s' AND idhall = '%s' AND date BETWEEN '%s' AND '%s' ORDER BY start_time ASC;",
2021-06-07 23:16:22 +02:00
$cinema, $hall, $date, $end);
}else if($date && !$end){
2021-06-03 11:32:27 +02:00
$date = date('Y-m-d', strtotime( $date ) );
$sql = sprintf( "SELECT * FROM session WHERE
idcinema = '%s' AND idhall = '%s' AND date = '%s' ORDER BY start_time ASC;",
$cinema, $hall, $date);
}else{
$sql = sprintf( "SELECT * FROM session WHERE
idcinema = '%s' AND idhall = '%s' ORDER BY start_time ASC;",
$cinema, $hall);
}
2021-05-26 09:03:02 +02:00
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error geting all sessions');
2021-05-26 09:03:02 +02:00
$sessions = null;
2021-06-03 11:32:27 +02:00
while($fila=$resul->fetch_assoc()){
2021-05-26 09:03:02 +02:00
$sessions[] = $this->loadSession($fila["id"], $fila["idfilm"], $fila["idhall"], $fila["idcinema"], $fila["date"], $fila["start_time"], $fila["seat_price"], $fila["format"], $fila["seats_full"]);
}
mysqli_free_result($resul);
return $sessions;
}
2021-06-03 11:32:27 +02:00
2021-06-04 09:33:18 +02:00
//Look for a title and cinema
2021-05-26 09:03:02 +02:00
public function getSessions_Film_Cinema($idFiml, $idCinema){
$sql = sprintf( "SELECT * FROM session WHERE session.idfilm = '%d' AND session.idcinema = '%d' ", $idFiml, $idCinema);
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error geting sessions with a idfilm and cinema');
2021-05-26 09:03:02 +02:00
$sessions = null;
while($fila = $resul->fetch_assoc()){
$sessions[] = $this->loadSession($fila["id"], $fila["idfilm"], $fila["idhall"], $fila["idcinema"], $fila["date"], $fila["start_time"], $fila["seat_price"], $fila["format"], $fila["seats_full"]);
}
$resul->free();
return $sessions;
}
2021-06-04 11:36:15 +02:00
//Edit a session taking the new film, hall, date, start time, seat price and format with respect to its origin parameter
2021-05-26 09:03:02 +02:00
public function editSession($idfilm, $idhall, $idcinema, $date, $startTime, $seatPrice, $format, $origin){
$format = $this->mysqli->real_escape_string($format);
$date = date('Y-m-d', strtotime( $date ) );
2021-06-07 23:16:22 +02:00
$origin["date"] = date('Y-m-d', strtotime( $origin["date"] ) );
2021-05-26 09:03:02 +02:00
$startTime = date('H:i:s', strtotime( $startTime ) );
2021-06-07 23:16:22 +02:00
$origin["start"] = date('H:i:s', strtotime( $origin["start"] ) );
2021-05-26 09:03:02 +02:00
$sql = sprintf( "UPDATE `session`
SET `idfilm` = '%d' , `idhall` = '%d', `idcinema` = '%d', `date` = '%s',
`start_time` = '%s', `seat_price` = '%d', `format` = '%s'
WHERE
2021-06-07 23:16:22 +02:00
idcinema = '%s' AND idhall = '%s' AND session.date = '%s' AND start_time = '%s'",
2021-05-26 09:03:02 +02:00
$idfilm, $idhall, $idcinema, $date, $startTime, $seatPrice, $format, $origin["cinema"],$origin["hall"],$origin["date"],$origin["start"]);
2021-06-06 13:33:40 +02:00
2021-06-07 23:16:22 +02:00
mysqli_query($this->mysqli, $sql) or die ('Error editing a session');
2021-06-06 13:33:40 +02:00
2021-05-26 09:03:02 +02:00
}
2021-06-04 11:36:15 +02:00
//Delete a session whit the primary key
2021-05-26 09:03:02 +02:00
public function deleteSession($hall, $cinema, $date, $startTime){
$sql = sprintf( "DELETE FROM `session` WHERE
idcinema = '%s' AND idhall = '%s' AND date = '%s' AND start_time = '%s'",
$cinema, $hall, $date, $startTime);
2021-06-07 23:16:22 +02:00
$resul = mysqli_query($this->mysqli, $sql) or die ('Error deleting a session');
2021-05-26 09:03:02 +02:00
return $resul;
}
//Create a new Session Data Transfer Object.
public function loadSession( $id, $idfilm, $idhall, $idcinema, $date, $startTime, $seatPrice, $format, $seats_full){
return new Session( $id, $idfilm, $idhall, $idcinema, $date, $startTime, $seatPrice, $format, $seats_full);
}
}
?>