/** * Review Add * route: reviews/add * * @param Request $request * @return Response * @throws Exception */ public function addAction(Request $request) { $json = Input::get('json', ''); $data = $request->only('json', 'fileField'); $data_photos = $data['fileField']; if ($json) { $data_json['Review'] = $request->json()->get('Review'); } else { $data_json = json_decode(file_get_contents($data['json']), true); } if (!isset($data_json['Review']) || !isset($data_json['Review']['user_id']) || !isset($data_json['Review']['restaurant_id']) || !isset($data_json['Review']['title']) || !isset($data_json['Review']['text']) || !isset($data_json['Review']['rating'])) { $message = "Format {'Review': {'user_id': <int>, 'restaurant_id': <int>, 'title': <string>, 'text': <string>, 'rating': <double>}, 'Photos': []}"; return showErrorResponse($message); } // check fo valid data if (!Restaurants::isExists($data_json['Review']['restaurant_id'])) { return showErrorResponse("Restaurant data not found", HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } if (!in_array($data_json['Review']['rating'], array('0.5', '1.0', '1', '1.5', '2.0', '2', '2.5', '3.0', '3', '3.5', '4.0', '4', '4.5', '5.0', '5'))) { $message = "Rating must not be 0. Any of 0.5, 1.0, 1.5, ..., 5.0"; return showErrorResponse($message); } // check for rating value // Check Ng Words $ng_words = NgWord::ngword_filter($data_json['Review']['title'] . ' ' . $data_json['Review']['text']); if ($ng_words) { $message = "Bad word(s) found: " . implode(' ', $ng_words); return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND); } // check of Ng Words try { DB::beginTransaction(); // Save Review Data $review = new Reviews(); $new_review = $review->addReview($data_json['Review']); // Save and Upload Photo $photos_upload = new Photos(); $photos_upload->saveUploadedPhotos($data_photos, $data_json['Review'], CONSTANTS::REVIEW, $new_review->id); //@TODO Restaurant Rating Implementation create a cron job for Rating Implementation // Send Notification to Followers about the new Review $followers = Follow::getFollowerUsersAll($new_review->user_id); foreach ($followers as $follower) { $notification = new Notification(); $notification->addNotificationNewReview($new_review->user_id, $follower->follower_user_id, $new_review->id, $new_review->restaurant_id); } //Add Activity $activity = new Activities(); $new_activity = $activity->addActivity(CONSTANTS::REVIEW, $new_review->id, $new_review->user_id, $new_review->restaurant_id); $photos = Photos::getByType(CONSTANTS::REVIEW, $new_review->id); $photos_array = Photos::convertPhotosToArray($photos); $restaurant = Restaurants::find($new_review->restaurant_id); $user = Users::find($new_review->user_id); $json_return[KeyParser::data] = array(KeyParser::activity => ModelFormatter::activityFormat($new_activity), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::review => ModelFormatter::reviewFormat($new_review), KeyParser::photos => $photos_array); DB::commit(); return response()->json($json_return); } catch (\Exception $e) { DB::rollback(); return showErrorResponse($e->getMessage()); } // end catch }