date; $tenantId = $request->tenantId(); // if yes then will return also the booking attached to each review $withBookings = $request->with_bookings; $reviews = []; // One day if (preg_match('#^\d{4}-\d{2}-\d{2}$#', $date)) { $reviews = BookingReview::where('restaurant_id', $tenantId) ->where('date_created', 'like', $date.'%'); } // One month else if (preg_match('#^\d{4}-\d{2}$#', $date)) { $reviews = BookingReview::where('restaurant_id', $tenantId) ->where('date_created', 'LIKE', $date.'%'); } // Range dates else if (preg_match('#^(\d{4}-\d{2}-\d{2})-(\d{4}-\d{2}-\d{2})$#', $date, $matches)) { $date1 = $matches[1]; $date2 = $matches[2]; $reviews = BookingReview::where('restaurant_id', $tenantId) ->where('date_created', '>=', $date1.' 00:00:00') ->where('date_created', '<=', $date2.' 23:59:59'); } //Query::setDebug(true); Query::setCache(true); $reviews = $reviews->orderBy('date_created', 'ASC')->get()->toArray(); // Add booking to every review if ($withBookings == 'yes') { $list = []; foreach($reviews as $review) { $item = $review->toArray(); $item['booking'] = $review->booking; $list[] = $item; } $reviews = $list; } return $this->response([ 'success' => true, 'reviews' => $reviews ]); } /** * Save response to a review * * @param Request $request * * @return \WP_REST_Response */ public function respond(Request $request) { // Get request parameters $tenantId = $request->tenantId(); $review_id = $request->review_id; $booking_id = $request->booking_id; $response = $request->response; // Validate request parameters if (empty($review_id) || empty($response)) { return $this->response([ 'success' => false, 'message' => 'Missing required parameters', 'error' => 'Review ID and response text are required.' ], 400); } try { // Find the review by ID and tenant ID for security $review = BookingReview::where('id', $review_id) ->where('restaurant_id', $tenantId) ->first(); // Check if review exists if (!$review) { return $this->response([ 'success' => false, 'message' => 'Review not found', 'error' => 'The specified review does not exist or you do not have permission to modify it.' ], 404); } // Update the review with the response //$review->response = base64_encode($response); $review->response = $response; $review->response_date = date('Y-m-d H:i:s'); $review->save(); // Optionally, notify the customer about the response $this->notifyCustomerAboutResponse($review); return $this->response([ 'success' => true, 'message' => 'Response saved successfully', 'data' => [ 'review_id' => $review_id, 'response_date' => $review->response_date ] ]); } catch (\Exception $e) { return $this->response([ 'success' => false, 'message' => 'An error occurred', 'error' => $e->getMessage() ], 500); } } /** * Method to notify customer about the response (optional implementation) * * @param BookingReview $review * @return void */ private function notifyCustomerAboutResponse(BookingReview $review) { // Get the booking associated with the review $booking = $review->booking; // Make sure the booking exists and has an email if (!$booking || !$booking->email) { return false; } // Send the email notification about the response return $booking->sendReviewResponseEmail($review); } }