Add return flight booking cinematic and personal informations editing.
[Project_webapp.git] / includes / formsearch.php
index 951bef1e546da3e6f311a6a8c32638fcdb818ea0..66c57c94b8412c8c0bbe6d654acaff78b6a5ad8b 100644 (file)
@@ -1,10 +1,10 @@
 <?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);
+$action = filter_input(INPUT_POST, "action", FILTER_SANITIZE_STRING);
 
 $oDepartureDate = new DateTime($form_departure_date);
 $oArrivalDate = new DateTime($form_arrival_date);
@@ -26,34 +26,80 @@ if ($oDepartureDate < $oDateNow) {
     $input_failure = true;
 }
 
-if ($oArrivalDate <= $oDepartureDate) {
+if (!($action === "return_flight") && $oArrivalDate <= $oDepartureDate) {
     echo "Arrival date is before departure date. <br>";
     $input_failure = true;
 }
 
+if (empty($form_arrival_date)) {
+    // Limit to a 6 months interval from the departure date.
+    $oArrivalDate = $oDepartureDate;
+    $oArrivalDate->add(new DateInterval("P6M"));
+    $form_arrival_date = $oArrivalDate->format('Y-m-d\TH:i');
+}
+
 ?>
 
-<h1> Rechercher un vol </h1>
+<h1> Rechercher un vol <?php if ($action === "return_flight") { echo "retour"; } ?></h1>
 
 <form action="index.php" id="search" method="post">
  <input type="hidden" name="form" value="search" />
+ <?php if ($action === "return_flight") echo "<input type=\"hidden\" name=\"action\" value=\"return_flight\" />" ?>
  <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/>
+ <select size="1" name="departure_city" required>
+  <optgroup label="Se&#769;lectionner une ville">
+ <?php
+ $sql_pquery = "select distinct VilleD from VOLS";
+ global $connection;
+ $connection->prepare_query($sql_pquery);
+ $connection->run_prepared_query();
+ $connection->get_pquery_result();
+ $cities = $connection->get_result_array();
+ $connection->close_prepared_query();
+ foreach ($cities as $city) {
+     if (strcmp($city[0], $form_departure_city) === 0) {
+         echo "<option value=\"$city[0]\" selected>$city[0]</option>\n";
+     } else {
+         echo "<option value=\"$city[0]\">$city[0]</option>\n";
+     }
+ }
+ ?>
+  </optgroup>
+ </select>
  <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/>
+ <select size="1" name="arrival_city" required>
+  <optgroup label="Se&#769;lectionner une ville">
+ <?php
+ $sql_pquery = "select distinct VilleA from VOLS";
+ global $connection;
+ $connection->prepare_query($sql_pquery);
+ $connection->run_prepared_query();
+ $connection->get_pquery_result();
+ $cities = $connection->get_result_array();
+ $connection->close_prepared_query();
+ foreach ($cities as $city) {
+     if (strcmp($city[0], $form_arrival_city) === 0) {
+         echo "<option value=\"$city[0]\" selected>$city[0]</option>\n";
+     } else {
+         echo "<option value=\"$city[0]\">$city[0]</option>\n";
+     }
+ }
+ ?>
+  </optgroup>
+ </select>
  <label> Date -> </label>
- <input type="datetime-local" name="arrival_date" value="<?php echo $form_arrival_date; ?>" required/>
- <input type="submit" value="Valider">
+ <input type="datetime-local" name="arrival_date" <?php if (isset($form_arrival_date)) echo "value=\"$form_arrival_date\"";?> />
+ <input type="submit" value="Rechercher">
 </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
+    $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, Classe, round(CoutVol*CoeffPrix, 2) as Prix, NumAv from VOLS, DEFCLASSES
                    where DEFCLASSES.NumVol = VOLS.NumVol and
                    DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ?
                    order by DateD, NumVol, Prix";
@@ -63,6 +109,7 @@ if (!$input_failure) {
     $connection->get_pquery_result();
     $rows = $connection->get_result_array();
     $connection->close_prepared_query();
+    //FIXME: Use NumAv to see if a flight is fully booked.
     //var_dump($rows);
     if (empty($rows)) {
         echo "Aucun vol ne correspond aux crite&#768;res de recherche. <br>";
@@ -95,14 +142,16 @@ if (!$input_failure) {
                           <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";
+                          <label> Vol retour : </label>\n";
+                          if (!($action === "return_flight")) {
+                              echo "        <input type=\"checkbox\" name=\"return_flight\" checked/>\n";
+                          }
+            echo "        <input type=\"submit\" value=\"Re&#769;server\">\n";
+            echo "      </form>
+                     </td>\n";
             echo "  </tr>\n";
         }
-        echo "</table>";
+        echo "</table>\n";
     }
 }