You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PROJET-WEB_STRI1A/WEB/ressources/trajet.php

179 lines
6.3 KiB
PHTML

<?php
//Récupérer la liste complete des villes
6 years ago
function getListeVilles($db)
{
$result = pg_query($db, "SELECT * FROM Ville;");
return $result;
}
6 years ago
function reserver($db, $mail, $codetrajet){
return pg_query_params($db, "INSERT INTO Reserver VALUES ($1, $2, $3);", array($mail, $codetrajet, date('Y-m-d H:i:s')));
}
function getTrajet($db, $codetrajet)
{
return pg_query_params($db, "SELECT * FROM Trajet WHERE codetrajet = $1;", array($codetrajet));
}
//Créer un trajet en bd
6 years ago
function creerTrajet($db, $mail, $dateDepart, $datArrivee, $matricule)
{
$date = date('Y-m-d H:i:s');
6 years ago
$result = pg_query_params($db, "INSERT INTO Trajet (datedepart, datearrivee, dateproposition, estannule, mailproposition, matricule) VALUES ($1, $2, $3, false, $4, $5) RETURNING codeTrajet;", array($dateDepart, $datArrivee, $date, $mail, $matricule));
return $result;
}
//Creer les étapes d'un trajet
6 years ago
function creerEtapes($db, $codeTrajet, $etapes)
{
$result = true;
6 years ago
for ($i = 0; $i < count($etapes) && $result; $i++) {
$arr = explode("%%", $etapes[$i]);
6 years ago
$result = pg_query_params($db, "INSERT INTO traverser (codetrajet, nomville, codepostal, numeroetape) VALUES ($1, $2, $3, $4);", array($codeTrajet, $arr[0], $arr[1], $i + 1));
}
return $result;
}
//Vérifier la cohérence des dates
6 years ago
function checkDates($db, $mail, $dateDepart, $dateArrivee)
{
$valide = $dateDepart < $dateArrivee && $dateDepart > date('Y-m-d H:i:s');
$result = pg_query_params($db, "SELECT COUNT(*) From Trajet WHERE mailproposition = $1 AND $2 BETWEEN datedepart AND datearrivee;", array($mail, $dateDepart));
$row = pg_fetch_array($result);
return $row[0] == 0 && $valide;
}
6 years ago
//Vérifier le nombre de places restantes
function checkPlacesVoiture($db, $codetrajet)
{
$result = pg_query_params($db, "SELECT nbplaces FROM Voiture vo, Trajet ta WHERE vo.mail = ta.mailproposition AND ta.codeTrajet = $1 LIMIT 1;", array($codetrajet));
$result2 = pg_query_params($db, "SELECT COUNT(*) FROM Reserver re, Trajet ta WHERE re.codetrajet.mail = ta.codetrajet AND ta.codetrajet = $1;", array($codetrajet));
if(!$result || !$result2){
return false;
}
$nbplaces = pg_fetch_array($result);
$nbreservations = pg_fetch_array($result);
return $nbplaces-1>$nbreservations;
}
//Vérifier la cohérence des dates
function checkDatesReserve($db, $mail, $dateDepart)
{
$result = pg_query_params($db, "SELECT COUNT(*) From Trajet ta, reserve re WHERE re.mailutilisateur = $1 AND re.codetrajet = ta.codetrajet AND $2 BETWEEN datedepart AND datearrivee;", array($mail, $dateDepart));
$row = pg_fetch_array($result);
return $row[0] == 0;
}
//Vérifier que l'utilisateur est bien associé à cette voiture
6 years ago
function checkVoiture($db, $mail, $matricule)
{
$result = pg_query_params($db, "SELECT matricule From Voiture WHERE mail = $1 AND matricule = $2;", array($mail, $matricule));
$row = pg_fetch_array($result);
return !strcmp($matricule, $row[0]);
}
6 years ago
function getTrajetsProposes($db, $mail)
{
$result = pg_query_params($db, "SELECT COUNT(*) from Trajet WHERE mailProposition = $1 AND estAnnule = false AND datedepart >= current_date;", array($mail));
if ($result) {
$row = pg_fetch_array($result);
return $row[0];
}
return 0;
}
6 years ago
function getTrajetsReserves($db, $mail)
{
$result = pg_query_params($db, "SELECT COUNT(*) FROM trajet tr, reserver r WHERE r.codetrajet = tr.codetrajet AND mailutilisateur = $1 AND estAnnule = false AND datedepart >= current_date;", array($mail));
if ($result) {
$row = pg_fetch_array($result);
return $row[0];
}
return 0;
}
6 years ago
function getMessagesNonLus($db, $mail)
{
$result = pg_query_params($db, "SELECT COUNT(*) from Message WHERE mailRecepteur = $1 AND estLu = FALSE;", array($mail));
if ($result) {
$row = pg_fetch_array($result);
$login = $row[0];
return $login;
}
}
6 years ago
function getAvisNonLus($db, $mail)
{
global $db;
$result = pg_query_params($db, "SELECT COUNT(tr.mailProposition) FROM Trajet tr, Avis av WHERE tr.codeTrajet = av.codeTrajet AND av.estLu = false AND tr.mailProposition = $1;", array($mail));
if ($result) {
$row = pg_fetch_array($result);
$login = $row[0];
return $login;
}
}
//Récupérer la liste des voitures concernant un utilisateur
6 years ago
function getListeVoitures($db, $mail)
{
$result = pg_query_params($db, "SELECT * FROM Voiture WHERE mail = $1;", array($mail));
return $result;
}
//Afficher les voitures dans un select
6 years ago
function afficherVoitures($db, $mail)
{
$voitures = getListeVoitures($db, $mail);
while ($row = pg_fetch_array($voitures)) {
$modele = $row['modele'];
$matricule = $row['matricule'];
echo "<option value='$matricule'>$modele</option>";
}
}
//Afficher les voitures dans un select
6 years ago
function afficherVilles($db)
{
$villes = getListeVilles($db);
while ($row = pg_fetch_array($villes)) {
$nom = $row['nomville'];
$cp = $row['codepostal'];
echo "<option value='$nom%%$cp'>$nom ($cp)</option>";
}
}
6 years ago
function getDepart($db, $codetajet)
{
$result = pg_query_params($db, "SELECT v.nomVille FROM ville v, traverser tr WHERE tr.nomVille = v.nomVille AND tr.codepostal = v.codepostal AND tr.codetrajet = $1 AND tr.numeroetape = (SELECT MIN(numeroetape) FROM traverser WHERE codetrajet = $1);", array($codetajet));
$return = false;
6 years ago
if ($result) {
$return = pg_fetch_array($result)[0];
}
return $return;
}
6 years ago
function getFin($db, $codetajet)
{
$result = pg_query_params($db, "SELECT v.nomVille FROM ville v, traverser tr WHERE tr.nomVille = v.nomVille AND tr.codepostal = v.codepostal AND tr.codetrajet = $1 AND tr.numeroetape = (SELECT MAX(numeroetape) FROM traverser WHERE codetrajet = $1);", array($codetajet));
$return = false;
6 years ago
if ($result) {
$return = pg_fetch_array($result)[0];
}
return $return;
}
6 years ago
function getErrorDepotTrajet($code)
{
switch ($code) {
case 1:
return "Erreur lors de la vérification des étapes.";
case 2:
return "Erreur lors de la vérification des dates.";
case 3:
return "Ce compte n'est pas associé à la voiture sélectionnée.";
case 4:
return "Erreur lors de l'enregistrement du trajet.";
}
}
6 years ago
?>