Add the flight search and booking features.
[Project_webapp.git] / includes / formsearch.php
1 <?php
2
3 $form_departure_city = filter_input(INPUT_POST, "departure_city", FILTER_SANITIZE_STRING);
4 $form_departure_date = filter_input(INPUT_POST, "departure_date", FILTER_SANITIZE_STRING);
5 $form_arrival_city = filter_input(INPUT_POST, "arrival_city", FILTER_SANITIZE_STRING);
6 $form_arrival_date = filter_input(INPUT_POST, "arrival_date", FILTER_SANITIZE_STRING);
7 $form_date_now = filter_input(INPUT_POST, "date_now", 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 ($oArrivalDate <= $oDepartureDate) {
30 echo "Arrival date is before departure date. <br>";
31 $input_failure = true;
32 }
33
34 ?>
35
36 <h1> Rechercher un vol </h1>
37
38 <form action="index.php" id="search" method="post">
39 <input type="hidden" name="form" value="search" />
40 <input type="hidden" name="date_now" value="<?php echo $form_date_now; ?>" />
41 <label> De&#769;part : Ville -> </label>
42 <input type="text" size="15" name="departure_city" value="<?php echo $form_departure_city; ?>" required/>
43 <label> Date -> </label>
44 <input type="datetime-local" name="departure_date" value="<?php echo $form_departure_date; ?>" required/>
45 <label> Arrive&#769;e : Ville -> </label>
46 <input type="text" size="15" name="arrival_city" value="<?php echo $form_arrival_city; ?>" required/>
47 <label> Date -> </label>
48 <input type="datetime-local" name="arrival_date" value="<?php echo $form_arrival_date; ?>" required/>
49 <input type="submit" value="Valider">
50 </form>
51 <br>
52
53 <?php
54 if (!$input_failure) {
55 global $connection;
56 $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, Classe, round(CoutVol*CoeffPrix, 2) as Prix from VOLS, DEFCLASSES
57 where DEFCLASSES.NumVol = VOLS.NumVol and
58 DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ?
59 order by DateD, NumVol, Prix";
60 $connection->prepare_query($sql_pquery);
61 $connection->prepared_query_bind_param("ssss", array($form_departure_date, $form_departure_city, $form_arrival_date, $form_arrival_city));
62 $connection->run_prepared_query();
63 $connection->get_pquery_result();
64 $rows = $connection->get_result_array();
65 $connection->close_prepared_query();
66 //var_dump($rows);
67 if (empty($rows)) {
68 echo "Aucun vol ne correspond aux crite&#768;res de recherche. <br>";
69 } else {
70 echo "<table id=\"search\">\n";
71 echo " <tr>\n";
72 echo " <th>Nume&#769;ro de vol</th>\n";
73 echo " <th>Ville de de&#769;part</th>\n";
74 echo " <th>Date de de&#769;part</th>\n";
75 echo " <th>Ville d'arrive&#769;e</th>\n";
76 echo " <th>Date d'arrive&#769;e</th>\n";
77 echo " <th>Classe</th>\n";
78 echo " <th>Prix d'une place</th>\n";
79 echo " <th>Re&#769;server</th>\n";
80 echo " </tr>\n";
81 foreach ($rows as $row) {
82 echo " <tr>\n";
83 echo " <td>" . $row['NumVol'] . "</td>\n";
84 echo " <td>" . $row['VilleD'] . "</td>\n";
85 echo " <td>" . $row['DateD'] . "</td>\n";
86 echo " <td>" . $row['VilleA'] . "</td>\n";
87 echo " <td>" . $row['DateA'] . "</td>\n";
88 echo " <td>" . $row['Classe'] . "</td>\n";
89 echo " <td>" . $row['Prix'] . "&euro;</td>\n";
90 echo " <td>
91 <form action=\"index.php\" id=\"booking\" method=\"post\">
92 <input type=\"hidden\" name=\"form\" value=\"booking\" />
93 <input type=\"hidden\" name=\"flight_id\" value=\"" . $row['NumVol'] . "\" />
94 <input type=\"hidden\" name=\"class_name\" value=\"" . $row['Classe'] . "\" />
95 <input type=\"hidden\" name=\"place_price\" value=\"" . $row['Prix'] . "\" />
96 <label> Place(s) : </label>
97 <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"1\" required/>
98 <label> Vol retour : </label>
99 <input type=\"checkbox\" name=\"return_flight\" checked required/>
100 <input type=\"submit\" value=\"Reserver\">
101 </form>
102 </td>\n";
103 echo " </tr>\n";
104 }
105 echo "</table>";
106 }
107 }
108
109 ?>