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