| 1 | <?php |
| 2 | $form_departure_city = filter_input(INPUT_POST, "departure_city", FILTER_SANITIZE_STRING); |
| 3 | $form_departure_date = filter_input(INPUT_POST, "departure_date", FILTER_SANITIZE_STRING); |
| 4 | $form_arrival_city = filter_input(INPUT_POST, "arrival_city", FILTER_SANITIZE_STRING); |
| 5 | $form_arrival_date = filter_input(INPUT_POST, "arrival_date", FILTER_SANITIZE_STRING); |
| 6 | $form_date_now = filter_input(INPUT_POST, "date_now", FILTER_SANITIZE_STRING); |
| 7 | $action = filter_input(INPUT_POST, "action", FILTER_SANITIZE_STRING); |
| 8 | |
| 9 | $oDepartureDate = new DateTime($form_departure_date); |
| 10 | $oArrivalDate = new DateTime($form_arrival_date); |
| 11 | $oDateNow = new Datetime($form_date_now); |
| 12 | |
| 13 | /** |
| 14 | * Sanity checks |
| 15 | */ |
| 16 | |
| 17 | $input_failure = false; |
| 18 | |
| 19 | if ($form_departure_city === $form_arrival_city) { |
| 20 | echo "Departure and arrival city are the same. <br>"; |
| 21 | $input_failure = true; |
| 22 | } |
| 23 | |
| 24 | if ($oDepartureDate < $oDateNow) { |
| 25 | echo "The departure date is before the current date. <br>"; |
| 26 | $input_failure = true; |
| 27 | } |
| 28 | |
| 29 | if (!($action === "return_flight") && $oArrivalDate <= $oDepartureDate) { |
| 30 | echo "Arrival date is before departure date. <br>"; |
| 31 | $input_failure = true; |
| 32 | } |
| 33 | |
| 34 | if (empty($form_arrival_date)) { |
| 35 | // Limit to a 6 months interval from the departure date. |
| 36 | $oArrivalDate = $oDepartureDate; |
| 37 | $oArrivalDate->add(new DateInterval("P6M")); |
| 38 | $form_arrival_date = $oArrivalDate->format('Y-m-d\TH:i'); |
| 39 | } |
| 40 | |
| 41 | ?> |
| 42 | |
| 43 | <h1> Rechercher un vol <?php if ($action === "return_flight") { echo "retour"; } ?> </h1> |
| 44 | |
| 45 | <form action="index.php" id="search" method="post"> |
| 46 | <input type="hidden" name="form" value="search" /> |
| 47 | <?php if ($action === "return_flight") { echo "<input type=\"hidden\" name=\"action\" value=\"return_flight\" />"; } ?> |
| 48 | <input type="hidden" name="date_now" value="<?php echo $form_date_now; ?>" /> |
| 49 | <label> Départ : Ville -> </label> |
| 50 | <select size="1" name="departure_city" required> |
| 51 | <optgroup label="Sélectionner une ville"> |
| 52 | <?php |
| 53 | $sql_pquery = "select distinct VilleD from VOLS order by VilleD"; |
| 54 | global $connection; |
| 55 | $connection->prepare_query($sql_pquery); |
| 56 | $connection->run_prepared_query(); |
| 57 | $connection->get_pquery_result(); |
| 58 | $cities = $connection->get_result_array(); |
| 59 | $connection->close_prepared_query(); |
| 60 | foreach ($cities as $city) { |
| 61 | if (strcmp($city[0], $form_departure_city) === 0) { |
| 62 | echo "<option value=\"$city[0]\" selected>$city[0]</option>\n"; |
| 63 | } else { |
| 64 | echo "<option value=\"$city[0]\">$city[0]</option>\n"; |
| 65 | } |
| 66 | } |
| 67 | ?> |
| 68 | </optgroup> |
| 69 | </select> |
| 70 | <label> Date -> </label> |
| 71 | <input type="datetime-local" name="departure_date" value="<?php echo $form_departure_date; ?>" required/> |
| 72 | <label> Arrivée : Ville -> </label> |
| 73 | <select size="1" name="arrival_city" required> |
| 74 | <optgroup label="Sélectionner une ville"> |
| 75 | <?php |
| 76 | $sql_pquery = "select distinct VilleA from VOLS order by VilleA"; |
| 77 | global $connection; |
| 78 | $connection->prepare_query($sql_pquery); |
| 79 | $connection->run_prepared_query(); |
| 80 | $connection->get_pquery_result(); |
| 81 | $cities = $connection->get_result_array(); |
| 82 | $connection->close_prepared_query(); |
| 83 | foreach ($cities as $city) { |
| 84 | if (strcmp($city[0], $form_arrival_city) === 0) { |
| 85 | echo "<option value=\"$city[0]\" selected>$city[0]</option>\n"; |
| 86 | } else { |
| 87 | echo "<option value=\"$city[0]\">$city[0]</option>\n"; |
| 88 | } |
| 89 | } |
| 90 | ?> |
| 91 | </optgroup> |
| 92 | </select> |
| 93 | <label> Date -> </label> |
| 94 | <input type="datetime-local" name="arrival_date" <?php if (isset($form_arrival_date)) { echo "value=\"$form_arrival_date\""; } ?> /> |
| 95 | <input type="submit" value="Rechercher"> |
| 96 | </form> |
| 97 | <br> |
| 98 | |
| 99 | <?php |
| 100 | if (!$input_failure) { |
| 101 | global $connection; |
| 102 | $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, DEFCLASSES.Classe, round(CoutVol*CoeffPrix, 2) as Prix, CapAv |
| 103 | from VOLS join DEFCLASSES on DEFCLASSES.NumVol = VOLS.NumVol |
| 104 | join AVIONS on AVIONS.NumAv = VOLS.NumAv |
| 105 | where DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ? |
| 106 | order by DateD, NumVol, Prix"; |
| 107 | $connection->prepare_query($sql_pquery); |
| 108 | $connection->prepared_query_bind_param("ssss", array($form_departure_date, $form_departure_city, $form_arrival_date, $form_arrival_city)); |
| 109 | $connection->run_prepared_query(); |
| 110 | $connection->get_pquery_result(); |
| 111 | $flights = $connection->get_result_array(); |
| 112 | $connection->close_prepared_query(); |
| 113 | //var_dump($flights); |
| 114 | if (empty($flights)) { |
| 115 | echo "Aucun vol ne correspond aux critères de recherche. <br>"; |
| 116 | } else { |
| 117 | if ($action === "return_flight" && isset($_SESSION['return_flight_nb_place'])) { |
| 118 | $nb_places = $_SESSION['return_flight_nb_place']; |
| 119 | unset($_SESSION['return_flight_nb_place']); |
| 120 | } else { |
| 121 | $nb_places = 1; |
| 122 | } |
| 123 | echo "<table id=\"search\">\n"; |
| 124 | echo " <tr>\n"; |
| 125 | echo " <th>Numéro de vol</th>\n"; |
| 126 | echo " <th>Ville de départ</th>\n"; |
| 127 | echo " <th>Date de départ</th>\n"; |
| 128 | echo " <th>Ville d'arrivée</th>\n"; |
| 129 | echo " <th>Date d'arrivée</th>\n"; |
| 130 | echo " <th>Classe</th>\n"; |
| 131 | echo " <th>Prix d'une place</th>\n"; |
| 132 | echo " <th>Réserver</th>\n"; |
| 133 | echo " </tr>\n"; |
| 134 | foreach ($flights as $flight) { |
| 135 | $sql_pquery = "select sum(NbPlaces) as BookedPlaces from RESERVATIONS where NumVol = ?"; |
| 136 | $connection->prepare_query($sql_pquery); |
| 137 | $connection->prepared_query_bind_param("s", array($flight['NumVol'])); |
| 138 | $connection->run_prepared_query(); |
| 139 | $connection->get_pquery_result(); |
| 140 | $rows = $connection->get_result_array(); |
| 141 | $connection->close_prepared_query(); |
| 142 | $booked_places = $rows[0][0]; |
| 143 | if (is_null($booked_places)) { |
| 144 | $booked_places = 0; |
| 145 | } |
| 146 | if (isset($flight['CapAv']) && isset($booked_places)) { |
| 147 | $free_places = $flight['CapAv'] - $booked_places; |
| 148 | } else { |
| 149 | $free_places = "Inconnu"; |
| 150 | } |
| 151 | echo " <tr>\n"; |
| 152 | echo " <td>" . $flight['NumVol'] . "</td>\n"; |
| 153 | echo " <td>" . $flight['VilleD'] . "</td>\n"; |
| 154 | echo " <td>" . $flight['DateD'] . "</td>\n"; |
| 155 | echo " <td>" . $flight['VilleA'] . "</td>\n"; |
| 156 | echo " <td>" . $flight['DateA'] . "</td>\n"; |
| 157 | echo " <td>" . $flight['Classe'] . "</td>\n"; |
| 158 | echo " <td>" . $flight['Prix'] . "€</td>\n"; |
| 159 | echo " <td> |
| 160 | Nombre de places libres : " . $free_places . " |
| 161 | <form action=\"index.php\" id=\"booking\" method=\"post\"> |
| 162 | <input type=\"hidden\" name=\"form\" value=\"booking\" /> |
| 163 | <input type=\"hidden\" name=\"flight_id\" value=\"" . $flight['NumVol'] . "\" /> |
| 164 | <input type=\"hidden\" name=\"class_name\" value=\"" . $flight['Classe'] . "\" /> |
| 165 | <input type=\"hidden\" name=\"place_price\" value=\"" . $flight['Prix'] . "\" /> |
| 166 | <label> Place(s) : </label> |
| 167 | <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"" . $nb_places . "\" required/>\n"; |
| 168 | if ($action !== "return_flight") { |
| 169 | echo " <label> Vol retour : </label>\n"; |
| 170 | echo " <input type=\"checkbox\" name=\"return_flight\" checked/>\n"; |
| 171 | } |
| 172 | if ($free_places !== 0 || $free_places === "Inconnu") { |
| 173 | echo " <input type=\"submit\" value=\"Réserver\">\n"; |
| 174 | } |
| 175 | echo " </form> |
| 176 | </td>\n"; |
| 177 | echo " </tr>\n"; |
| 178 | } |
| 179 | echo "</table>\n"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | ?> |