/** * Construct the needed Array for Reviews User and Restaurant * action: userAction, restaurantAction * * @param $reviews * @return mixed */ public static function reviewsQueries($reviews) { $reviews_array = array(); if (!$reviews) { return $reviews_array; } foreach ($reviews as $review) { $restaurant = Restaurants::find($review->restaurant_id); if (!$restaurant) { continue; } $user = Users::find($review->user_id); $photos = Photos::getByType(CONSTANTS::REVIEW, $review->id); $photos_array = Photos::convertPhotosToArray($photos); if ($user) { $reviews_array[] = array(KeyParser::review => ModelFormatter::reviewFormat($review), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); } //end of check user unset($restaurant); unset($user); unset($photos); unset($photos_array); } //end foreach return $reviews_array; }
/** * Construct the needed Array for Checkins User and Restaurant * action: * * @param $checkins * @return mixed */ public static function checkinsQueries($checkins) { $checkins_array = array(); if (!$checkins) { return $checkins_array; } foreach ($checkins as $checkin) { $restaurant = Restaurants::find($checkin->restaurant_id); if (!$restaurant) { continue; } $user = Users::find($checkin->user_id); $photos = Photos::getByType(CONSTANTS::CHECKIN, $checkin->id); $photos_array = Photos::convertPhotosToArray($photos); $checkins_array[] = array(KeyParser::checkin => ModelFormatter::checkinFormat($checkin), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); unset($restaurant); unset($user); unset($photos); unset($photos_array); } //end foreach return $checkins_array; }
/** * @param Request $request * @param $id * @return Response * @throws Exception */ public function editAction(Request $request, $id) { /* Multipart Procedure $data = $request->only('json'); $data_json = json_decode(file_get_contents($data['json']), true); */ $data_json['CheckIn'] = $request->json()->get('CheckIn'); if (!isset($data_json['CheckIn']) || !isset($data_json['CheckIn']['message'])) { $message = "Format should be: {'CheckIn': 'message': <string>}}"; return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY); } // check fo valid data // Check Ng Words $ng_words = NgWord::ngword_filter($data_json['CheckIn']['message']); 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 { // Edit Review Data $checkin = new Checkins(); $edit_checkin = $checkin->editCheckin($id, $data_json['CheckIn']); $photos = Photos::getByType(CONSTANTS::CHECKIN, $edit_checkin->id); $photos_array = Photos::convertPhotosToArray($photos); $restaurant = Restaurants::find($edit_checkin->restaurant_id); if (!$restaurant) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $user = Users::find($edit_checkin->user_id); $json_return[KeyParser::data] = array(KeyParser::checkin => ModelFormatter::checkinFormat($edit_checkin), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); return response()->json($json_return); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } // end catch }
/** Displays list of restaurants within user's recent activity list * route: /restaurants/recent-activity/{user_id}/{search_key?} * * @param $user_id * @param $search_key (optional) for restaurant name searches * @return Response */ public function recentActivitySearchAction($user_id, $search_key = null) { $restaurants = Restaurants::getRecentActivityRestaurants($user_id, $search_key); $data = array(); foreach ($restaurants as $restaurant) { $categories = Categories::getFormattedRestaurantCategories($restaurant->id); $data[] = array(KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::categories => $categories); } $page = array(KeyParser::current => $restaurants->currentPage(), KeyParser::number => $restaurants->lastPage()); $json_return = array(KeyParser::data => $data, KeyParser::page => $page); return response()->json($json_return); }
/** * Upload Photo Routine for Restaurant * route: photos/upload/restaurant * * @param Request $request * @return Mixed */ public function photoUploadRestaurantAction(Request $request) { $data = $request->only('json', 'fileField'); $data_photos = $data['fileField']; $data_json = json_decode(file_get_contents($data['json']), true); try { DB::beginTransaction(); foreach ($data_json['Photos'] as $data_json_photo) { foreach ($data_photos as $key => $data_photo) { $photo_text = ""; if (isset($data_json_photo['Photo']['text'])) { $photo_text = $data_json_photo['Photo']['text']; // Check Ng Words $ng_words = NgWord::ngword_filter($photo_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 } $data_photos[$key]->text = $photo_text; } } $restaurant_id = $data_json['Restaurant']['restaurant_id']; $user_id = $data_json['Restaurant']['user_id']; // Save and Upload Photo $photos_upload = new Photos(); $success_photo = $photos_upload->saveUploadedPhotos($data_photos, $data_json['Restaurant'], CONSTANTS::RESTAURANT, $restaurant_id); $followers = Follow::getFollowerUsersAll($user_id); $notification = new Notification(); //Add Activity foreach ($success_photo as $photo) { $activity = new Activities(); $activity->addActivity(CONSTANTS::PHOTO_UPLOAD_RESTAURANT, $photo->id, $user_id, $restaurant_id); foreach ($followers as $follower) { $notification->addNotificationNewPhoto($user_id, $follower->follower_user_id, $photo->id, $restaurant_id); } } $photos_array = Photos::convertPhotosToArray($success_photo); $restaurant = Restaurants::find($restaurant_id); $user = Users::find($user_id); $json_return[KeyParser::data] = array(KeyParser::activity => ModelFormatter::activityFormat($activity), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); DB::commit(); return response()->json($json_return); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } // end catch }
/** * Review View * route: reviews/view{id} * * @param $id * @optional ?viewer_id * @return Response */ public function viewAction($id) { $json_return = array(); $review = Reviews::find($id); if (!$review) { return showErrorResponse('Review not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_REVIEW_MISSING); } $restaurant = Restaurants::find($review->restaurant_id); if (!$restaurant) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $user = Users::find($review->user_id); $photos = Photos::getByType(CONSTANTS::REVIEW, $review->id); $photos_array = Photos::convertPhotosToArray($photos); $comments = Comments::getByType(CONSTANTS::REVIEW, $review->id); $comments_array = array(); if ($comments) { foreach ($comments as $comment) { $comments_array[] = ModelFormatter::commentFormat($comment); } } $json_return[KeyParser::data] = array(KeyParser::review => ModelFormatter::reviewFormat($review), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array, KeyParser::comments => $comments_array); return response()->json($json_return); }