Fix the flight reservation with a return cinematic.
[Project_webapp.git] / includes / formsearch.php
CommitLineData
33eb6f2a 1<?php
33eb6f2a
JB
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);
22f1dc64 7$action = filter_input(INPUT_POST, "action", FILTER_SANITIZE_STRING);
33eb6f2a
JB
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
19if ($form_departure_city === $form_arrival_city) {
20 echo "Departure and arrival city are the same. <br>";
21 $input_failure = true;
22}
23
24if ($oDepartureDate < $oDateNow) {
25 echo "The departure date is before the current date. <br>";
26 $input_failure = true;
27}
28
22f1dc64 29if (!($action === "return_flight") && $oArrivalDate <= $oDepartureDate) {
33eb6f2a
JB
30 echo "Arrival date is before departure date. <br>";
31 $input_failure = true;
32}
33
a2f7a729
JB
34if (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
33eb6f2a
JB
41?>
42
77c2d82c 43<h1> Rechercher un vol <?php if ($action === "return_flight") { echo "retour"; } ?> </h1>
33eb6f2a
JB
44
45<form action="index.php" id="search" method="post">
46 <input type="hidden" name="form" value="search" />
77c2d82c 47 <?php if ($action === "return_flight") { echo "<input type=\"hidden\" name=\"action\" value=\"return_flight\" />"; } ?>
33eb6f2a
JB
48 <input type="hidden" name="date_now" value="<?php echo $form_date_now; ?>" />
49 <label> De&#769;part : Ville -> </label>
a2f7a729
JB
50 <select size="1" name="departure_city" required>
51 <optgroup label="Se&#769;lectionner une ville">
52 <?php
f38123a5 53 $sql_pquery = "select distinct VilleD from VOLS order by VilleD";
a2f7a729
JB
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>
33eb6f2a
JB
70 <label> Date -> </label>
71 <input type="datetime-local" name="departure_date" value="<?php echo $form_departure_date; ?>" required/>
72 <label> Arrive&#769;e : Ville -> </label>
a2f7a729
JB
73 <select size="1" name="arrival_city" required>
74 <optgroup label="Se&#769;lectionner une ville">
75 <?php
f38123a5 76 $sql_pquery = "select distinct VilleA from VOLS order by VilleA";
a2f7a729
JB
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>
33eb6f2a 93 <label> Date -> </label>
77c2d82c 94 <input type="datetime-local" name="arrival_date" <?php if (isset($form_arrival_date)) { echo "value=\"$form_arrival_date\""; } ?> />
0a87f453 95 <input type="submit" value="Rechercher">
33eb6f2a
JB
96</form>
97<br>
98
99<?php
100if (!$input_failure) {
77c2d82c 101 global $connection;
a0e7c679
JB
102 $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, Classe, round(CoutVol*CoeffPrix, 2) as Prix, NumAv
103 from VOLS, DEFCLASSES
77c2d82c
JB
104 where DEFCLASSES.NumVol = VOLS.NumVol and
105 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 $rows = $connection->get_result_array();
112 $connection->close_prepared_query();
113 //FIXME: Use NumAv to see if a flight is fully booked.
114 //var_dump($rows);
115 if (empty($rows)) {
116 echo "Aucun vol ne correspond aux crite&#768;res de recherche. <br>";
117 } else {
a0e7c679
JB
118 if ($action === "return_flight" && isset($_SESSION['return_flight_nb_place'])) {
119 $nb_places = $_SESSION['return_flight_nb_place'];
120 unset($_SESSION['return_flight_nb_place']);
121 } else {
122 $nb_places = 1;
123 }
77c2d82c
JB
124 echo "<table id=\"search\">\n";
125 echo " <tr>\n";
126 echo " <th>Nume&#769;ro de vol</th>\n";
127 echo " <th>Ville de de&#769;part</th>\n";
128 echo " <th>Date de de&#769;part</th>\n";
129 echo " <th>Ville d'arrive&#769;e</th>\n";
130 echo " <th>Date d'arrive&#769;e</th>\n";
131 echo " <th>Classe</th>\n";
132 echo " <th>Prix d'une place</th>\n";
133 echo " <th>Re&#769;server</th>\n";
134 echo " </tr>\n";
135 foreach ($rows as $row) {
136 echo " <tr>\n";
137 echo " <td>" . $row['NumVol'] . "</td>\n";
138 echo " <td>" . $row['VilleD'] . "</td>\n";
139 echo " <td>" . $row['DateD'] . "</td>\n";
140 echo " <td>" . $row['VilleA'] . "</td>\n";
141 echo " <td>" . $row['DateA'] . "</td>\n";
142 echo " <td>" . $row['Classe'] . "</td>\n";
143 echo " <td>" . $row['Prix'] . "&euro;</td>\n";
144 echo " <td>
33eb6f2a
JB
145 <form action=\"index.php\" id=\"booking\" method=\"post\">
146 <input type=\"hidden\" name=\"form\" value=\"booking\" />
147 <input type=\"hidden\" name=\"flight_id\" value=\"" . $row['NumVol'] . "\" />
148 <input type=\"hidden\" name=\"class_name\" value=\"" . $row['Classe'] . "\" />
149 <input type=\"hidden\" name=\"place_price\" value=\"" . $row['Prix'] . "\" />
150 <label> Place(s) : </label>
a0e7c679 151 <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"" . $nb_places . "\" required/>\n";
77c2d82c 152 if (!($action === "return_flight")) {
a0e7c679 153 echo " <label> Vol retour : </label>\n";
77c2d82c
JB
154 echo " <input type=\"checkbox\" name=\"return_flight\" checked/>\n";
155 }
156 echo " <input type=\"submit\" value=\"Re&#769;server\">\n";
157 echo " </form>
158 </td>\n";
159 echo " </tr>\n";
160 }
161 echo "</table>\n";
162 }
163 }
33eb6f2a
JB
164
165?>