2019-05-22 00:10:25 +02:00
< ? php
//Récupérer la liste complete des villes
2019-05-23 00:25:28 +02:00
function getListeVilles ( $db )
{
2019-05-22 00:10:25 +02:00
$result = pg_query ( $db , " SELECT * FROM Ville; " );
return $result ;
}
2019-05-23 00:25:28 +02:00
function reserver ( $db , $mail , $codetrajet ){
2019-05-23 12:05:13 +02:00
return pg_query_params ( $db , " INSERT INTO Reserver VALUES ( $ 1, $ 2, false, $ 3); " , array ( $mail , $codetrajet , date ( 'Y-m-d H:i:s' )));
2019-05-23 00:25:28 +02:00
}
function getTrajet ( $db , $codetrajet )
{
return pg_query_params ( $db , " SELECT * FROM Trajet WHERE codetrajet = $ 1; " , array ( $codetrajet ));
}
2019-05-22 00:10:25 +02:00
//Créer un trajet en bd
2019-05-23 00:25:28 +02:00
function creerTrajet ( $db , $mail , $dateDepart , $datArrivee , $matricule )
{
2019-05-22 00:10:25 +02:00
$date = date ( 'Y-m-d H:i:s' );
2019-05-23 00:25:28 +02:00
$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 ));
2019-05-22 00:10:25 +02:00
return $result ;
}
//Creer les étapes d'un trajet
2019-05-23 00:25:28 +02:00
function creerEtapes ( $db , $codeTrajet , $etapes )
{
2019-05-22 00:10:25 +02:00
$result = true ;
2019-05-23 00:25:28 +02:00
for ( $i = 0 ; $i < count ( $etapes ) && $result ; $i ++ ) {
2019-05-22 00:10:25 +02:00
$arr = explode ( " %% " , $etapes [ $i ]);
2019-05-23 00:25:28 +02:00
$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 ));
2019-05-22 00:10:25 +02:00
}
return $result ;
}
//Vérifier la cohérence des dates
2019-05-23 00:25:28 +02:00
function checkDates ( $db , $mail , $dateDepart , $dateArrivee )
{
2019-05-23 12:05:13 +02:00
$valide = $dateDepart <= $dateArrivee && $dateDepart >= date ( 'Y-m-d H:i:s' );
$result = pg_query_params ( $db , " SELECT COUNT(*) From Trajet WHERE mailproposition = $ 1 AND estannule = false AND $ 2 BETWEEN datedepart AND datearrivee; " , array ( $mail , $dateDepart ));
2019-05-22 00:10:25 +02:00
$row = pg_fetch_array ( $result );
return $row [ 0 ] == 0 && $valide ;
}
2019-05-23 12:05:13 +02:00
//Récupérer les étapes d'un trajet
function getEtapesTrajet ( $db , $codetrajet ){
$result = pg_query_params ( $db , " SELECT * FROM Traverser WHERE codetrajet = $ 1 ; " , array ( $codetrajet ));
return $result ;
}
2019-05-23 00:25:28 +02:00
//Vérifier le nombre de places restantes
function checkPlacesVoiture ( $db , $codetrajet )
{
2019-05-23 02:11:30 +02:00
$result = pg_query_params ( $db , " SELECT nbplaces FROM Voiture vo, Trajet ta WHERE vo.matricule = ta.matricule AND ta.codeTrajet = $ 1; " , array ( $codetrajet ));
$result2 = pg_query_params ( $db , " SELECT COUNT(*) FROM Reserver re, Trajet ta WHERE re.codetrajet = ta.codetrajet AND ta.codetrajet = $ 1; " , array ( $codetrajet ));
2019-05-23 00:25:28 +02:00
if ( ! $result || ! $result2 ){
return false ;
}
2019-05-23 02:11:30 +02:00
$nbplaces = pg_fetch_array ( $result )[ 0 ];
$nbreservations = pg_fetch_array ( $result )[ 0 ];
return $nbplaces - 1 > $nbreservations ;
2019-05-23 00:25:28 +02:00
}
//Vérifier la cohérence des dates
function checkDatesReserve ( $db , $mail , $dateDepart )
{
2019-05-23 02:11:30 +02:00
$result = pg_query_params ( $db , " SELECT COUNT(*) From Trajet ta, reserver 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 ;
}
//Verifier reservations
function checkReservations ( $db , $mail , $codetrajet )
{
$result = pg_query_params ( $db , " SELECT COUNT(*) From reserver WHERE mailutilisateur = $ 1 AND codetrajet = $ 2; " , array ( $mail , $codetrajet ));
2019-05-23 00:25:28 +02:00
$row = pg_fetch_array ( $result );
return $row [ 0 ] == 0 ;
}
2019-05-22 00:10:25 +02:00
//Vérifier que l'utilisateur est bien associé à cette voiture
2019-05-23 00:25:28 +02:00
function checkVoiture ( $db , $mail , $matricule )
{
2019-05-22 00:10:25 +02:00
$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 ]);
2019-05-22 16:41:08 +02:00
}
2019-05-23 00:25:28 +02:00
function getTrajetsProposes ( $db , $mail )
{
2019-05-23 17:06:08 +02:00
$result = pg_query_params ( $db , " SELECT COUNT(*) from Trajet WHERE mailProposition = $ 1 AND estAnnule = false AND datedepart >= $ 2; " , array ( $mail , date ( 'Y-m-d H:i:s' )));
2019-05-22 16:41:08 +02:00
if ( $result ) {
$row = pg_fetch_array ( $result );
return $row [ 0 ];
}
return 0 ;
}
2019-05-23 00:25:28 +02:00
function getTrajetsReserves ( $db , $mail )
{
2019-05-23 17:06:08 +02:00
$result = pg_query_params ( $db , " SELECT COUNT(*) FROM trajet tr, reserver re WHERE re.codetrajet = tr.codetrajet AND mailutilisateur = $ 1 AND tr.estannule = false AND re.estannule = false AND tr.datedepart >= $ 2; " , array ( $mail , date ( 'Y-m-d H:i:s' )));
2019-05-22 16:41:08 +02:00
if ( $result ) {
$row = pg_fetch_array ( $result );
return $row [ 0 ];
}
return 0 ;
}
2019-05-23 00:25:28 +02:00
function getMessagesNonLus ( $db , $mail )
{
2019-05-22 16:41:08 +02:00
$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 ;
}
}
2019-05-23 00:25:28 +02:00
function getAvisNonLus ( $db , $mail )
{
2019-05-22 16:41:08 +02:00
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
2019-05-23 00:25:28 +02:00
function getListeVoitures ( $db , $mail )
{
2019-05-22 16:41:08 +02:00
$result = pg_query_params ( $db , " SELECT * FROM Voiture WHERE mail = $ 1; " , array ( $mail ));
return $result ;
}
//Afficher les voitures dans un select
2019-05-23 00:25:28 +02:00
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> " ;
}
2019-05-22 16:41:08 +02:00
}
//Afficher les voitures dans un select
2019-05-23 00:25:28 +02:00
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> " ;
}
2019-05-22 16:41:08 +02:00
}
2019-05-23 00:25:28 +02:00
function getDepart ( $db , $codetajet )
{
2019-05-23 17:06:08 +02:00
$result = pg_query_params ( $db , " SELECT * 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 ));
2019-05-22 22:52:03 +02:00
$return = false ;
2019-05-23 00:25:28 +02:00
if ( $result ) {
2019-05-23 17:06:08 +02:00
$return = pg_fetch_array ( $result );
2019-05-22 22:52:03 +02:00
}
return $return ;
}
2019-05-23 00:25:28 +02:00
function getFin ( $db , $codetajet )
{
2019-05-23 17:06:08 +02:00
$result = pg_query_params ( $db , " SELECT * 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 ));
2019-05-22 22:52:03 +02:00
$return = false ;
2019-05-23 00:25:28 +02:00
if ( $result ) {
2019-05-23 17:06:08 +02:00
$return = pg_fetch_array ( $result );
2019-05-22 22:52:03 +02:00
}
return $return ;
}
2019-05-23 02:11:30 +02:00
function annulerTrajet ( $db , $codetrajet ){
$result = pg_query_params ( $db , " UPDATE Trajet SET estannule = true WHERE codetrajet = $ 1; " , array ( $codetrajet ));
return $result ;
}
function annulerReservation ( $db , $mail , $codetrajet ){
$result = pg_query_params ( $db , " DELETE FROM reserver WHERE codetrajet = $ 1 AND mailutilisateur = $ 2; " , array ( $codetrajet , $mail ));
return $result ;
}
2019-05-23 00:25:28 +02:00
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. " ;
}
2019-05-22 16:41:08 +02:00
}
2019-05-23 00:25:28 +02:00
2019-05-23 17:06:08 +02:00
function avisExiste ( $db , $mail , $codetrajet ){
$result = pg_query_params ( $db , " SELECT COUNT(*) FROM Avis WHERE mailemetteur = $ 1 AND codetrajet = $ 2; " , array ( $mail , $codetrajet ));
if ( ! $result ){
return false ;
}
return pg_fetch_array ( $result )[ 0 ] != 0 ;
}
2019-05-23 00:25:28 +02:00
?>