Add the flight search and booking features.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 16 Jan 2018 15:44:21 +0000 (16:44 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 16 Jan 2018 15:44:21 +0000 (16:44 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
12 files changed:
includes/account.php
includes/config-example.php
includes/formbooking.php [new file with mode: 0644]
includes/formlogin.php
includes/formsearch.php [new file with mode: 0644]
includes/header.php
includes/login.php
includes/reservations.php [moved from includes/booking.php with 80% similarity]
includes/search.php
lib/db.php
lib/utils.php
styles/airpolytech.css

index 74fbddb498e068534a11ff241fc28809df1019d4..dc254491f554d190cadf40e0a2a12608f48e23c1 100644 (file)
@@ -5,3 +5,6 @@ if (!$is_logged_in) {
     redirect("index.php?page=login", 2);
 }
 ?>
+
+<h2>Mes informations</h2>
+<h2>Mes re&#769;versations</h2>
index d7ad85350e12466bac5d34862a87411f2310328e..a7edf02773269a5787f4c8ff898017f609e414ee 100644 (file)
@@ -11,6 +11,7 @@
          'login',
          'logout',
          'register',
+         'reservations',
          'booking',
          'search',
          'account'
diff --git a/includes/formbooking.php b/includes/formbooking.php
new file mode 100644 (file)
index 0000000..373e18b
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+$form_flight_id = filter_input(INPUT_POST, "flight_id", FILTER_SANITIZE_STRING);
+$form_class_name = filter_input(INPUT_POST, "class_name", FILTER_SANITIZE_STRING);
+$form_place_price = filter_input(INPUT_POST, "place_price", FILTER_VALIDATE_FLOAT);
+$form_nb_place = filter_input(INPUT_POST, "nb_place", FILTER_VALIDATE_INT);
+$form_return_flight = filter_input(INPUT_POST, "return_flight", FILTER_VALIDATE_BOOLEAN);
+
+global $is_logged_in;
+if (!$is_logged_in) {
+    echo "Please login first.";
+    redirect("index.php?page=login", 2);
+}
+
+$input_failure = false;
+
+if (!$form_place_price) {
+    echo "Prix invalide. <br>";
+    $input_failure = true;
+}
+
+if (!$form_nb_place) {
+    echo "Nombre de place(s) invalide. <br>";
+    $input_failure = true;
+}
+
+if (!$form_return_flight) {
+    echo "Vol retour invalide. <br>";
+    $input_failure = true;
+}
+
+if (!$input_failure && !empty($form_nb_place) && !empty($form_class_name)) {
+    global $connection;
+    $client_id = get_client_id($_SESSION['email']);
+    $nb_booked = nb_booked($client_id, $form_flight_id);
+    if (!empty($nb_booked)) {
+        echo "Vous avez de&#769;ja&#768; re&#769;serve&#769; ce vol, vous allez e&#770;tre redirige&#769; vers la liste de vos re&#769;servations dans 2 secondes. <br>";
+        redirect("index.php?page=reservations", 2);
+    }
+    $sql_pquery = "insert into RESERVATIONS (NumCl, NumVol, Classe, NbPlaces)
+                   values (?, ?, ?, ?)";
+    $connection->prepare_query($sql_pquery);
+    $connection->prepared_query_bind_param("issi", array($client_id, $form_flight_id, $form_class_name, $form_nb_place));
+    $connection->run_prepared_query();
+    $connection->close_prepared_query();
+    $sql_pquery = "select VilleD, DateD, VilleA, DateA from VOLS, DEFCLASSES
+                   where DEFCLASSES.NumVol = VOLS.NumVol and
+                   VOLS.NumVol = ? and Classe = ?";
+    $connection->prepare_query($sql_pquery);
+    $connection->prepared_query_bind_param("ss", array($form_flight_id, $form_class_name));
+    $connection->run_prepared_query();
+    $connection->get_pquery_result();
+    $rows = $connection->get_result_array();
+    $connection->close_prepared_query();
+    foreach ($rows as $row) {
+        echo "Vous avez re&#769;serve&#769; et paye&#769; " . $form_nb_place . " place(s) sur le vol " .$form_flight_id .
+             " au de&#769;part de " . $row['VilleD'].  " a&#768; " . $row['DateD'] . " arrivant a&#768; " . $row['VilleA'] . " a&#768; " . $row['DateA'] .
+             " pour un montant de " . $form_place_price * $form_nb_place . "&euro;. <br>";
+    }
+    if ($form_return_flight) {
+        redirect("index.php?page=search", 3);
+        $_SESSION['current_flight_id'] = $form_flight_id;
+        $_SESSION['return_flight'] = $form_return_flight;
+    } else {
+        redirect("index.php?page=reservations", 3);
+    }
+}
+
+?>
index 73cdff0e2f1ed3e899364925902ee1a08cdad50a..ed17f8069d80b075a3fb1b876ccba14071b1f87f 100644 (file)
@@ -15,7 +15,7 @@ if (!$form_email) {
             echo "You're successfully authenticated. <br>";
             redirect("index.php", 3);
         } else {
-            echo "Your password is incorrect for the account email" . $form_email . ". <br>";
+            echo "Your password is incorrect for the account email " . $form_email . ". <br>";
             echo "<a href=\"javascript:history.go(-1)\">Retour</a>";
         }
     } else {
diff --git a/includes/formsearch.php b/includes/formsearch.php
new file mode 100644 (file)
index 0000000..951bef1
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+
+$form_departure_city = filter_input(INPUT_POST, "departure_city", FILTER_SANITIZE_STRING);
+$form_departure_date = filter_input(INPUT_POST, "departure_date", FILTER_SANITIZE_STRING);
+$form_arrival_city = filter_input(INPUT_POST, "arrival_city", FILTER_SANITIZE_STRING);
+$form_arrival_date = filter_input(INPUT_POST, "arrival_date", FILTER_SANITIZE_STRING);
+$form_date_now = filter_input(INPUT_POST, "date_now", FILTER_SANITIZE_STRING);
+
+$oDepartureDate = new DateTime($form_departure_date);
+$oArrivalDate = new DateTime($form_arrival_date);
+$oDateNow = new Datetime($form_date_now);
+
+/**
+ * Sanity checks
+ */
+
+$input_failure = false;
+
+if ($form_departure_city === $form_arrival_city) {
+    echo "Departure and arrival city are the same. <br>";
+    $input_failure = true;
+}
+
+if ($oDepartureDate < $oDateNow) {
+    echo "The departure date is before the current date. <br>";
+    $input_failure = true;
+}
+
+if ($oArrivalDate <= $oDepartureDate) {
+    echo "Arrival date is before departure date. <br>";
+    $input_failure = true;
+}
+
+?>
+
+<h1> Rechercher un vol </h1>
+
+<form action="index.php" id="search" method="post">
+ <input type="hidden" name="form" value="search" />
+ <input type="hidden" name="date_now" value="<?php echo $form_date_now; ?>" />
+ <label> De&#769;part : Ville -> </label>
+ <input type="text" size="15" name="departure_city" value="<?php echo $form_departure_city; ?>" required/>
+ <label> Date -> </label>
+ <input type="datetime-local" name="departure_date" value="<?php echo $form_departure_date; ?>" required/>
+ <label> Arrive&#769;e : Ville -> </label>
+ <input type="text" size="15" name="arrival_city" value="<?php echo $form_arrival_city; ?>" required/>
+ <label> Date -> </label>
+ <input type="datetime-local" name="arrival_date" value="<?php echo $form_arrival_date; ?>" required/>
+ <input type="submit" value="Valider">
+</form>
+<br>
+
+<?php
+if (!$input_failure) {
+    global $connection;
+    $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, Classe, round(CoutVol*CoeffPrix, 2) as Prix from VOLS, DEFCLASSES
+                   where DEFCLASSES.NumVol = VOLS.NumVol and
+                   DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ?
+                   order by DateD, NumVol, Prix";
+    $connection->prepare_query($sql_pquery);
+    $connection->prepared_query_bind_param("ssss", array($form_departure_date, $form_departure_city, $form_arrival_date, $form_arrival_city));
+    $connection->run_prepared_query();
+    $connection->get_pquery_result();
+    $rows = $connection->get_result_array();
+    $connection->close_prepared_query();
+    //var_dump($rows);
+    if (empty($rows)) {
+        echo "Aucun vol ne correspond aux crite&#768;res de recherche. <br>";
+    } else {
+        echo "<table id=\"search\">\n";
+        echo "  <tr>\n";
+        echo "    <th>Nume&#769;ro de vol</th>\n";
+        echo "    <th>Ville de de&#769;part</th>\n";
+        echo "    <th>Date de de&#769;part</th>\n";
+        echo "    <th>Ville d'arrive&#769;e</th>\n";
+        echo "    <th>Date d'arrive&#769;e</th>\n";
+        echo "    <th>Classe</th>\n";
+        echo "    <th>Prix d'une place</th>\n";
+        echo "    <th>Re&#769;server</th>\n";
+        echo "  </tr>\n";
+        foreach ($rows as $row) {
+            echo "  <tr>\n";
+            echo "    <td>" . $row['NumVol'] . "</td>\n";
+            echo "    <td>" . $row['VilleD'] . "</td>\n";
+            echo "    <td>" . $row['DateD'] . "</td>\n";
+            echo "    <td>" . $row['VilleA'] . "</td>\n";
+            echo "    <td>" . $row['DateA'] . "</td>\n";
+            echo "    <td>" . $row['Classe'] . "</td>\n";
+            echo "    <td>" . $row['Prix'] . "&euro;</td>\n";
+            echo "    <td>
+                        <form action=\"index.php\" id=\"booking\" method=\"post\">
+                          <input type=\"hidden\" name=\"form\" value=\"booking\" />
+                          <input type=\"hidden\" name=\"flight_id\" value=\"" . $row['NumVol'] . "\" />
+                          <input type=\"hidden\" name=\"class_name\" value=\"" . $row['Classe'] . "\" />
+                          <input type=\"hidden\" name=\"place_price\" value=\"" . $row['Prix'] . "\" />
+                          <label> Place(s) : </label>
+                          <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"1\" required/>
+                          <label> Vol retour : </label>
+                          <input type=\"checkbox\" name=\"return_flight\" checked required/>
+                          <input type=\"submit\" value=\"Reserver\">
+                        </form>
+                      </td>\n";
+            echo "  </tr>\n";
+        }
+        echo "</table>";
+    }
+}
+
+?>
index 2e32552db9b69ea1d17e2cc45623f82ca953a456..4dae57600b2162ab7a7bce5d6fae2d35a5ba0e74 100644 (file)
@@ -33,10 +33,15 @@ $is_logged_in = chk_logged_in();
 </div>
 <div id="menu">
     | <a href="<?php echo $configs['root_url'] ?>/index.php?page=home">Accueil</a>
-    | <a href="<?php echo $configs['root_url'] ?>/index.php?page=search">Rechercher un vol</a> |
+    | <a href="<?php echo $configs['root_url'] ?>/index.php?page=search">Rechercher un vol</a>
+    |
+    <?php
+    if ($is_logged_in) {
+        echo "<a href=\"" . $configs['root_url'] . "/index.php?page=reservations\">Mes re&#769;servations</a> |";
+    }
+    ?>
 </div>
 
-
 <?php
 
 
index fe5c0fa4c36d6645bd513ebf044c4a8f3b5cb903..05f87148a6202ec150fd2dad75295717c253a1b4 100644 (file)
@@ -3,7 +3,7 @@
 <form action="index.php" id="login" method="post">
  <input type="hidden" name="form" value="login" />
  <label> Email : </label>
- <input type="email"size="25" name="email" required/>
+ <input type="email" size="25" name="email" required/>
  <label> Mot de passe : </label>
  <input type="password" size="15" name="password" required/>
  <input type="submit" value="Valider">
similarity index 80%
rename from includes/booking.php
rename to includes/reservations.php
index 74fbddb498e068534a11ff241fc28809df1019d4..b2d8f4fc9b6e6bf86155b7ce7215702db838579c 100644 (file)
@@ -5,3 +5,4 @@ if (!$is_logged_in) {
     redirect("index.php?page=login", 2);
 }
 ?>
+<h1>Mes re&#769;servations<h1>
index 366c9627d9215aff953d4e7c76979b9f1e2965ba..aab573303eccca127012171fdff80f304d892e1b 100644 (file)
@@ -1,7 +1,10 @@
 <h1> Rechercher un vol </h1>
 
+<?php if (isset($_SESSION['return_flight'])) ?>
+
 <form action="index.php" id="search" method="post">
  <input type="hidden" name="form" value="search" />
+ <input type="hidden" name="date_now" value="<?php echo date('Y-m-d\TH:i'); ?>" />
  <label> De&#769;part : Ville -> </label>
  <input type="text" size="15" name="departure_city" required/>
  <label> Date -> </label>
@@ -9,6 +12,6 @@
  <label> Arrive&#769;e : Ville -> </label>
  <input type="text" size="15" name="arrival_city" required/>
  <label> Date -> </label>
- <input type="datetime-local" name="arrival_date" value="<?php echo date('Y-m-d\TH:i'); ?>" required/>
+ <input type="datetime-local" name="arrival_date" value="<?php echo date('Y-m-d\TH:i', time() + 86400); ?>" required/>
  <input type="submit" value="Valider">
 </form>
index c7c2d48c71f9cb45e756b1151576b7e125e36c1a..cd9b0825331a60c40b9c9a6d23590c487ffa8cd6 100644 (file)
@@ -80,6 +80,7 @@ class CustomDB
 
     /**
      * [prepared_query_bind_param description]
+     * @param  [type] $types  [description]
      * @param  [type] $params [description]
      * @return [type]         [description]
      */
@@ -116,9 +117,18 @@ class CustomDB
 
     public function get_result_array()
     {
-        $rt_val = $this->current_result->fetch_array();
-        if (!$rt_val) {
+        $row = $this->current_result->fetch_array();
+        if (is_null($row)) {
+            $rt_val = [];
+        } elseif (!isset($row)) {
             echo "Fail to build SQL query result array : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
+            $rt_val = false;
+        } else {
+            $rows[] = $row;
+            while ($row = $this->current_result->fetch_array()) {
+                $rows[] = $row;
+            }
+            $rt_val = $rows;
         }
         return $rt_val;
     }
@@ -136,4 +146,5 @@ class CustomDB
         return $rt_val;
     }
 }
+
 ?>
index 8fdb0aa66d7834a235d0d8ce05d08a1d6e965df6..47c8bda2e0d0c1784fb129f09fb9709de4c99e1d 100644 (file)
@@ -28,7 +28,7 @@ function chk_account($email)
     $connection->get_pquery_result();
     $row = $connection->get_result_array();
     $connection->close_prepared_query();
-    if (!empty($row[0])) {
+    if (!empty($row[0][0])) {
         return true;
     } else {
         return false;
@@ -45,13 +45,39 @@ function chk_password($email, $password)
     $connection->get_pquery_result();
     $row = $connection->get_result_array();
     $connection->close_prepared_query();
-    if (password_verify($password, $row[0])) {
+    if (password_verify($password, $row[0][0])) {
         return true;
     } else {
         return false;
     }
 }
 
+function get_client_id($email)
+{
+    global $connection;
+    $sql_pquery = "select NumCl from CLIENTS where EmailCl = ?";
+    $connection->prepare_query($sql_pquery);
+    $connection->prepared_query_bind_param("s", array($email));
+    $connection->run_prepared_query();
+    $connection->get_pquery_result();
+    $row = $connection->get_result_array();
+    $connection->close_prepared_query();
+    return $row[0][0];
+}
+
+function nb_booked($client_id, $flight_id)
+{
+    global $connection;
+    $sql_pquery = "select SUM(NbPlaces) from RESERVATIONS where NumCl = ? and NumVol = ?";
+    $connection->prepare_query($sql_pquery);
+    $connection->prepared_query_bind_param("is", array($client_id, $flight_id));
+    $connection->run_prepared_query();
+    $connection->get_pquery_result();
+    $row = $connection->get_result_array();
+    $connection->close_prepared_query();
+    return $row[0][0];
+}
+
 function chk_logged_in()
 {
     if (isset($_SESSION['email']) && isset($_SESSION['IP_address'])) {
index e5a93bf572f1dc3d2f0ad8d6fc5f051483b8ac37..a05683e4951a4d0b65dc2233df1a6bb16bd71d1b 100644 (file)
@@ -41,6 +41,10 @@ form#register {
     width: 720px;
 }
 
+form#booking {
+    width: 250px;
+}
+
 label, input {
     /* in order to define widths */
     display: inline-block;
@@ -75,12 +79,16 @@ table#home {
     line-height: 1em;
 }
 
-table#result tr:nth-child(even) {
-    background-color: #eee;
+table#search {
+    border: none;
+}
+
+table#search tr:nth-child(even) {
+    background-color: #675b5b;
 }
 
-table#result tr:nth-child(odd) {
-    background-color: #fff;
+table#search tr:nth-child(odd) {
+    background-color: #474343;
 }
 
 #header {