Add password change feature.
[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
53 $sql_pquery = "select distinct VilleD from VOLS";
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
76 $sql_pquery = "select distinct VilleA from VOLS";
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
JB
101 global $connection;
102 $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, Classe, round(CoutVol*CoeffPrix, 2) as Prix, NumAv from VOLS, DEFCLASSES
103 where DEFCLASSES.NumVol = VOLS.NumVol and
104 DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ?
105 order by DateD, NumVol, Prix";
106 $connection->prepare_query($sql_pquery);
107 $connection->prepared_query_bind_param("ssss", array($form_departure_date, $form_departure_city, $form_arrival_date, $form_arrival_city));
108 $connection->run_prepared_query();
109 $connection->get_pquery_result();
110 $rows = $connection->get_result_array();
111 $connection->close_prepared_query();
112 //FIXME: Use NumAv to see if a flight is fully booked.
113 //var_dump($rows);
114 if (empty($rows)) {
115 echo "Aucun vol ne correspond aux crite&#768;res de recherche. <br>";
116 } else {
117 echo "<table id=\"search\">\n";
118 echo " <tr>\n";
119 echo " <th>Nume&#769;ro de vol</th>\n";
120 echo " <th>Ville de de&#769;part</th>\n";
121 echo " <th>Date de de&#769;part</th>\n";
122 echo " <th>Ville d'arrive&#769;e</th>\n";
123 echo " <th>Date d'arrive&#769;e</th>\n";
124 echo " <th>Classe</th>\n";
125 echo " <th>Prix d'une place</th>\n";
126 echo " <th>Re&#769;server</th>\n";
127 echo " </tr>\n";
128 foreach ($rows as $row) {
129 echo " <tr>\n";
130 echo " <td>" . $row['NumVol'] . "</td>\n";
131 echo " <td>" . $row['VilleD'] . "</td>\n";
132 echo " <td>" . $row['DateD'] . "</td>\n";
133 echo " <td>" . $row['VilleA'] . "</td>\n";
134 echo " <td>" . $row['DateA'] . "</td>\n";
135 echo " <td>" . $row['Classe'] . "</td>\n";
136 echo " <td>" . $row['Prix'] . "&euro;</td>\n";
137 echo " <td>
33eb6f2a
JB
138 <form action=\"index.php\" id=\"booking\" method=\"post\">
139 <input type=\"hidden\" name=\"form\" value=\"booking\" />
140 <input type=\"hidden\" name=\"flight_id\" value=\"" . $row['NumVol'] . "\" />
141 <input type=\"hidden\" name=\"class_name\" value=\"" . $row['Classe'] . "\" />
142 <input type=\"hidden\" name=\"place_price\" value=\"" . $row['Prix'] . "\" />
143 <label> Place(s) : </label>
144 <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"1\" required/>
22f1dc64 145 <label> Vol retour : </label>\n";
77c2d82c
JB
146 if (!($action === "return_flight")) {
147 echo " <input type=\"checkbox\" name=\"return_flight\" checked/>\n";
148 }
149 echo " <input type=\"submit\" value=\"Re&#769;server\">\n";
150 echo " </form>
151 </td>\n";
152 echo " </tr>\n";
153 }
154 echo "</table>\n";
155 }
156 }
33eb6f2a
JB
157
158?>