source: business.php

Last change on this file was 0791611, checked in by sstalevska <sara.stalevska@…>, 20 months ago

Push the entire project.

  • Property mode set to 100644
File size: 7.4 KB
Line 
1<?php
2require_once('./inc/common.php');
3
4/**
5 * Check whether the required parameter ID is sent. Go back with an error message if not.
6 */
7if ((! isset($_GET['id'])) || (empty($_GET['id'])) || (! is_numeric($_GET['id']))) {
8 header('Location: /businesses.php?err=no_id');
9 exit();
10}
11$business_id = $_GET['id'];
12
13/**
14 * Get the name of this business (needed for the page title).
15 */
16$sql = 'SELECT business_name FROM business WHERE business_id = :id';
17$stm = $conn->prepare($sql);
18$stm->execute([':id' => $business_id]);
19$business_name = $stm->fetch()[0];
20/**
21 * Go back with an error message if the business with this id does not exist.
22 */
23if (empty($business_name)) {
24 header('Location: /businesses.php?err=bad_id');
25 exit();
26}
27
28$pageTitle = 'Business: ' . $business_name;
29$pageSlug = 'businesses';
30
31require_once('./inc/head.php');
32require_once('./inc/header.php');
33?>
34<div class="container">
35 <h1 class="mt-5"><?= $business_name ?></h1>
36
37 <?php require_once('./inc/alerts-msg.php'); ?>
38
39 <?php
40 /**
41 * Fetch the details of the business with the given business id.
42 */
43 $sql = '
44 select
45 b.business_name,
46 (
47 select avg(r.review_stars)
48 from review r
49 where r.business_id = b.business_id
50 ) as business_avg_stars,
51 c.category_name,
52 b.business_hours,
53 b.business_descr,
54 b.business_phone
55 from
56 business b
57 join category c on
58 b.category_id = c.category_id
59 where
60 b.business_id = :id';
61 $stm = $conn->prepare($sql);
62 $stm->execute([':id' => $business_id]);
63 $business = $stm->fetch();
64
65 /**
66 * Fetch all services that this business offers and concatenate them in a single string.
67 */
68 $sql = '
69 select
70 string_agg(s.service_name, \'; \') as services
71 from
72 business_service bs
73 join service s on
74 bs.service_id = s.service_id
75 where business_id = :id';
76 $stm = $conn->prepare($sql);
77 $stm->execute([':id' => $business_id]);
78 $services = $stm->fetch()[0];
79 ?>
80 <div class="row">
81 <div class="col-md-6">
82 <?= outputStars($business['business_avg_stars'], 48) ?>
83
84 <div class="lead my-3"><strong>Category:</strong> <?= $business['category_name'] ?></div>
85
86 <div class="fw-light my-3"><strong>Service(s):</strong> <?= $services ?></div>
87
88 <div class="fw-light my-3"><strong>Description:</strong> <?= $business['business_descr'] ?></div>
89 </div>
90 <div class="col-md-6">
91 <div class="fw-light my-3"><strong>Phone:</strong> <?= $business['business_phone'] ?></div>
92
93 <div class="fw-light my-3"><strong>Work hours:</strong> <?= $business['business_hours'] ?></div>
94
95 <div class="fw-light my-3">
96 <strong>Address(es):</strong>
97 <ul>
98 <?php
99 /**
100 * Get all addresses of this business and display them in a list.
101 */
102 $sql = '
103 select
104 address_id,
105 address_street,
106 address_postal_code,
107 address_city
108 from address
109 where business_id = :id';
110 $stm = $conn->prepare($sql);
111 $stm->execute([':id' => $business_id]);
112 $addresses = $stm->fetchAll();
113 foreach ($addresses as $row) {
114 ?>
115 <li>
116 <?= $row['address_street'] ?><br>
117 <?= $row['address_postal_code'] . ' ' . $row['address_city'] ?>
118 </li>
119 <?php } ?>
120 </ul>
121 </div>
122 </div>
123 </div>
124
125 <hr class="my-5">
126
127 <h3>Reviews</h3>
128
129 <?php
130 /**
131 * Get all reviews for this business and display them in a list.
132 */
133 $sql = '
134 select
135 r.reviewer_name,
136 r.reviewer_verified,
137 rv.review_title,
138 rv.review_text,
139 rv.review_stars,
140 rv.address_id,
141 rv.review_timestamp
142 from review rv
143 join reviewer r on
144 rv.reviewer_id = r.reviewer_id
145 where
146 rv.business_id = :id
147 order by
148 rv.review_timestamp';
149 $stm = $conn->prepare($sql);
150 $stm->execute([':id' => $business_id]);
151 $reviews = $stm->fetchAll();
152 foreach ($reviews as $row) {
153 ?>
154 <div class="card shadow-sm mb-3">
155 <div class="card-body">
156 <div class="row">
157 <div class="col-lg-8">
158 <h5 class="card-title">
159 <?= $row['reviewer_name'] ?>
160 <?php if ($row['reviewer_verified']) {
161 echo '<small class="ms-5 text-success"><small>✅ Verified</small></small>';
162 } else {
163 echo '<small class="ms-5 text-muted"><small><span class="opacity-25">☑️</span> Not verified</small></small>';
164 } ?>
165 </h5>
166 </div>
167 <div class="col-lg-4 text-lg-end"><?= outputStars($row['review_stars'], 24) ?></div>
168 </div>
169
170 <div class="card-text">
171 <div class="lead fw-bold"><?= $row['review_title'] ?></div>
172 <div class="mb-3"><?= $row['review_text'] ?></div>
173 <?php
174 $aid = $row['address_id'];
175 $location = array_values(array_filter($addresses, function($item) use ($aid) {
176 return $item[0] == $aid;
177 }))[0];
178 ?>
179 <div class="text-muted"><small><strong>Location:</strong> <?= $location[1] . ', ' . $location[2] . ' ' . $location[3] ?></small></div>
180 <div class="text-muted"><small><strong>Reviewed at:</strong> <?= date('d.m.Y H:i', strtotime($row['review_timestamp'])) ?></small></div>
181 </div>
182 </div>
183 </div>
184 <?php } ?>
185
186 <?php
187 /**
188 * If the person viewing this page is logged in as a reviewer, display a form from where
189 * the reviewer can add a review.
190 */
191 ?>
192 <?php if (isset($_SESSION['is_reviewer'])) { ?>
193 <hr id="add-review" class="my-5">
194
195 <h3 class="text-center pt-3">Add review</h3>
196
197 <div class="row justify-content-center mt-3">
198 <div class="col-md-8 col-lg-6 col-xl-5">
199 <div class="card shadow-sm p-3">
200 <?php require_once('./inc/alerts-err.php'); ?>
201
202 <form action="review-add.php" method="POST">
203 <input type="hidden" name="business" value="<?= $business_id ?>">
204 <div class="mb-3">
205 <label class="form-label">Location</label>
206 <div>
207 <?php foreach ($addresses as $i => $row) { ?>
208 <div class="form-check">
209 <input class="form-check-input" type="radio" name="address" id="address<?= $i ?>" value="<?= $row['address_id'] ?>" <?= ($i == 0) ? 'checked' : '' ?>>
210 <label class="form-check-label" for="address<?= $i ?>"><?= $row['address_street'] . ', ' . $row['address_postal_code'] . ' ' . $row['address_city'] ?></label>
211 </div>
212 <?php } ?>
213 </div>
214 </div>
215 <div class="mb-3">
216 <label class="form-label">Star rating</label>
217 <div>
218 <?php for ($i = 1; $i < 6; $i++) { ?>
219 <div class="form-check form-check-inline">
220 <input class="form-check-input" type="radio" name="rating" id="rating<?= $i ?>" value="<?= $i ?>" <?= ($i == 3) ? 'checked' : '' ?>>
221 <label class="form-check-label" for="rating<?= $i ?>"><?= $i ?></label>
222 </div>
223 <?php } ?>
224 </div>
225 </div>
226 <div class="mb-3">
227 <label for="title" class="form-label">Review title</label>
228 <?php
229 /**
230 * Value by default holds the "old" value, i.e. when the user submits
231 * the form, but it has errors, we don't want to lose reviewer's review.
232 */
233 ?>
234 <input type="text" id="title" name="title" class="form-control" minlength="2" maxlength="150" value="<?= (isset($_REQUEST["title"])) ? strip_tags($_REQUEST["title"]) : '' ?>" required>
235 </div>
236 <div class="mb-3">
237 <label for="text" class="form-label">Your review</label>
238 <textarea id="text" name="text" class="form-control" rows="7" minlength="2" maxlength="1000" required><?= (isset($_REQUEST["title"])) ? strip_tags($_REQUEST["title"]) : '' ?></textarea>
239 </div>
240 <button type="submit" class="btn btn-success">Submit</button>
241 </form>
242 </div>
243 </div>
244 </div>
245 <?php } ?>
246</div>
247<?php
248require_once('./inc/footer.php');
Note: See TracBrowser for help on using the repository browser.