/** * Report a comment, photo, restaurant, or review * * @param Request $request * @return Response */ public function addAction(Request $request) { $json_return = array(); $data = $request->json()->get('Report'); if ($data['type'] == CONSTANTS::PHOTO) { $photo = Photos::find($data['type_id']); if (!$photo) { $message = 'Photo not found or already deleted'; return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_PHOTO_MISSING); } $is_existing_photo_report = Reported::isExistingPhotoReport($data['type_id'], $data['user_id']); if ($is_existing_photo_report) { $message = 'You have already reported this photo'; return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_REPORTED_ALREADY); } } try { $report = new Reported(); $report_object = $report->addReport($data); $json_return[KeyParser::data] = ModelFormatter::reportFormat($report_object); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } return response()->json($json_return); }
/** * Add like on review, checkin or photo * * @param $request * @return mixed */ public function addAction(Request $request) { $data = $request->json()->get('Like'); $type = $data['type']; $type_id = $data['type_id']; $user_id = $data['user_id']; $user_data = Users::find($user_id); if (is_null($user_data)) { return showErrorResponse('Invalid user'); } $is_liked = Like::isLiked($user_id, $type, $type_id); if ($is_liked) { $like_count = Like::getCount($type, $type_id); $json_return[KeyParser::data] = array(KeyParser::type => $type, KeyParser::type_id => $type_id, KeyParser::user_id => $user_id, KeyParser::is_existing => CONSTANTS::LIKE_IS_EXISTING, KeyParser::like_count => $like_count); return response()->json($json_return); } switch ($type) { case CONSTANTS::REVIEW: $like_object = Reviews::find($type_id); $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_REVIEW; if (!$like_object) { $status_code = CONSTANTS::ERROR_CODE_REVIEW_MISSING; } break; case CONSTANTS::CHECKIN: $like_object = CheckIns::find($type_id); $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_CHECKIN; if (!$like_object) { $status_code = CONSTANTS::ERROR_CODE_CHECKIN_MISSING; } break; case CONSTANTS::PHOTO: $like_object = Photos::find($type_id); $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_PHOTO; if (!$like_object) { $status_code = CONSTANTS::ERROR_CODE_PHOTO_MISSING; } break; default: return showErrorResponse('Invalid type', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_INVALID_TYPE); } if (!isset($like_object)) { return showErrorResponse('Invalid type id', HTTP_ACCEPTED, $status_code); } if (!Restaurants::isExists($like_object->restaurant_id)) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } try { $like_data = new Like(); $query_response = $like_data->addLike($type_id, $type, $user_id, $like_object, $like_type); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } $json_return[KeyParser::data] = array(KeyParser::type => $query_response->type, KeyParser::type_id => $query_response->type_id, KeyParser::user_id => $query_response->user_id, KeyParser::is_existing => CONSTANTS::LIKE_IS_NOT_EXISTING, KeyParser::like_count => Like::getCount($type, $type_id)); return response()->json($json_return); }
/** * Output JSON output of comments in a photo * * @param $id * @return Response */ public function viewByPhotoIdAction($id) { $photo = Photos::find($id); if (!$photo) { return showErrorResponse('Photo not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_PHOTO_MISSING); } if (!Restaurants::isExists($photo->restaurant_id)) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $json_return = array(KeyParser::data => array(), KeyParser::comment_count => 0, KeyParser::page => array()); $comments = Comments::getByTypePaginate(CONSTANTS::PHOTO, $id); if ($comments->count()) { foreach ($comments as $comment) { $json_return[KeyParser::data][] = array(KeyParser::comment => ModelFormatter::commentFormat($comment), KeyParser::user => Users::find($comment->user_id)); } $json_return[KeyParser::comment_count] = Comments::getCountByType(CONSTANTS::PHOTO, $id); $json_return[KeyParser::page] = array(KeyParser::current => $comments->currentPage(), KeyParser::number => $comments->lastPage()); } return response()->json($json_return); }
/** * Displays individual restaurant information * route: /restaurants/{restaurant_id}/{viewer_id?} * * @param id - restaurant ID * @param viewer_id - user ID of the viewer (Optional) * @return Response */ public function viewAction($id) { $viewer_id = Input::get('viewer_id', null); $restaurant = Restaurants::find($id); $data = array(); if ($restaurant) { $restaurant->view_count++; $restaurant->save(); $restaurant_categories = RestaurantsCategory::getByRestaurantId($id); $photos = Photos::getByRestaurantId($id); $is_bookmarked = Bookmarks::isBookmarked($viewer_id, $restaurant->id); $data[KeyParser::restaurant] = ModelFormatter::restaurantViewFormat($restaurant, $is_bookmarked); foreach ($photos as $photo) { $data[KeyParser::photos][] = ModelFormatter::photosFormat($photo); } $data[KeyParser::categories] = Categories::getFormattedRestaurantCategories($restaurant->id); Categories::getFormattedRestaurantCategories($restaurant->id); $latest_activity = Activities::getLatestRestaurantActivity($id); if ($latest_activity) { $data[KeyParser::activity] = ModelFormatter::activityRestaurantViewFormat($latest_activity); $data[KeyParser::user] = Users::getStatistics($id); $data += Activities::getActivityType($latest_activity->type, $latest_activity->type_id); } } $recently_viewed = array(); if ($viewer_id) { $where = array('user_id' => $viewer_id, 'restaurant_id' => $id); $rv = LogRecentlyViewed::where($where)->get()->first(); if ($rv) { $rv->date_modified = date('Y-m-d H:i:s'); $rv->save(); } $rv = new LogRecentlyViewed(); $rv->addNewLog($viewer_id, $id); } $json_return[KeyParser::data] = $data; return response()->json($json_return); }
function checkTypeId($type, $type_id) { switch ($type) { case CONSTANTS::REVIEW: $object = Reviews::find($type_id); $message = "Review does not exist"; $error_code = CONSTANTS::ERROR_CODE_REVIEW_MISSING; break; case CONSTANTS::CHECKIN: $object = CheckIns::find($type_id); $message = "Checkin does not exist"; $error_code = CONSTANTS::ERROR_CODE_CHECKIN_MISSING; break; case CONSTANTS::BOOKMARK: $object = Bookmarks::find($type_id); $message = "Bookmark does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case CONSTANTS::COMMENT: $object = Comments::find($type_id); $message = "Comment does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case CONSTANTS::PHOTO: $object = Photos::find($type_id); $message = "Photo does not exist"; $error_code = CONSTANTS::ERROR_CODE_PHOTO_MISSING; break; case CONSTANTS::RESTAURANT: $object = Restaurants::find($type_id); $message = "Restaurant does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case 'user': $object = Users::find($type_id); $message = "User does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; default: return showErrorResponse('Invalid type', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_INVALID_TYPE); } if (!$object) { return showErrorResponse($message, HTTP_ACCEPTED, $error_code); } return false; }
/** * 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; }
/** * Save Photos in DB * * @param $data_photos * @param $data_json * @param $type * @param $type_id */ public function saveUploadedPhotos($data_photos, $data_json, $type, $type_id) { if (!isset($data_photos)) { return; } $return_array = array(); foreach ($data_photos as $photo) { $filename_new = $this->moveUploadPhotos($photo, $data_json['restaurant_id'], $type, $type_id); $photos = new Photos(); $photos->user_id = $data_json['user_id']; $photos->restaurant_id = $data_json['restaurant_id']; $photos->type = $type; $photos->type_id = $type_id; $photos->url = $filename_new; $photos->date_uploaded = date('Y-m-d H:i:s'); if (isset($photo->text)) { $photos->text = $photo->text; } $photos->save(); $return_array[] = $photos; } return $return_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 }
/** * Delete a Photo * route: /photos/delete{id} * * @param Request $request * @return Response */ public function photoDeleteAction(Request $request) { $path = API_UPLOAD_DIR . '/'; $data = $request->json()->get('Photo'); $failed_ids = array(); $succeeded_ids = array(); foreach ($data['id'] as $photo_id) { $photo = Photos::find($photo_id); if (!$photo || $photo->user_id != $data['user_id']) { $failed_ids[] = $photo_id; continue; } $filename = $photo->url; $fullpath = $path . $filename; try { DB::beginTransaction(); if (FILE::exists($fullpath)) { // Delete an array of files //$files = array($file1, $file2); FILE::delete($fullpath); } // end if Exists $comment = new Comments(); $comment->deleteCommentByType(CONSTANTS::PHOTO, $photo->id); $like = new Like(); $like->deleteLikes(CONSTANTS::PHOTO, $photo->id); $activity = new Activities(); $activity->deleteActivity(CONSTANTS::PHOTO_UPLOAD_RESTAURANT, $photo->id); $photo->delete(); DB::commit(); $succeeded_ids[] = $photo_id; } catch (\Exception $e) { DB::rollback(); return showErrorResponse('Error deleting photo'); } } $json_return[KeyParser::data] = array(KeyParser::success => $succeeded_ids, KeyParser::failed => $failed_ids); return response()->json($json_return); }
/** * Add new comment to table * * @param $text * @param $type * @param $type_id * @param $user_id * @throws \Exception */ public function addComment($text, $type, $type_id, $user_id) { $connection = $this->getConnection(); try { $connection->beginTransaction(); $type_object = null; switch ($type) { case CONSTANTS::REVIEW: $type_object = Reviews::find($type_id); if (!$type_object) { //Review not Found throw new \Exception(CONSTANTS::ERROR_CODE_REVIEW_MISSING); } $this->type = CONSTANTS::REVIEW; $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_REVIEW; break; case CONSTANTS::CHECKIN: $type_object = CheckIns::find($type_id); if (!$type_object) { throw new \Exception(CONSTANTS::ERROR_CODE_CHECKIN_MISSING); } $this->type = CONSTANTS::CHECKIN; $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_CHECKIN; break; case CONSTANTS::PHOTO: $type_object = Photos::find($type_id); if (!$type_object) { throw new \Exception(CONSTANTS::ERROR_CODE_PHOTO_MISSING); } $this->type = CONSTANTS::PHOTO; $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_PHOTO; break; default: throw new \Exception(CONSTANTS::ERROR_CODE_INVALID_TYPE); } $this->type_id = $type_id; $this->comment = $text; $this->status = CONSTANTS::STATUS_ENABLED; $this->user_id = $user_id; $this->date_created = date('Y-m-d H:i:s'); $this->save(); $restaurant_id = $type_object['restaurant_id']; $owner_id = $type_object['user_id']; if ($user_id != $owner_id) { $notification_data = new Notification(); $notification_data->addCommentNotification($user_id, $owner_id, $comment_type, $type_id, $restaurant_id); } $connection->commit(); } catch (\Exception $e) { $connection->rollBack(); throw $e; } }
/** * Get the review, checkin, bookmark, follow, photos, comments, and notification data of a user * * @param $id * @param $viewer_id * @return array */ public static function getStatistics($id, $viewer_id = false) { $user = self::find($id); if (!$user) { return array(); } $user_array = ModelFormatter::userLongFormat($user); $user_array[KeyParser::review_count] = Reviews::getCountByUserId($id); $user_array[KeyParser::checkin_count] = CheckIns::getCountByUserId($id); $user_array[KeyParser::bookmark_count] = Bookmarks::getCountByUserId($id); $user_array[KeyParser::following_count] = Follow::getCountByUserId($id, CONSTANTS::FOLLOW_FOLLOWED); $user_array[KeyParser::follower_count] = Follow::getCountByUserId($id, CONSTANTS::FOLLOW_FOLLOWER); $user_array[KeyParser::photo_count] = Photos::getCountByUserId($id); $user_array[KeyParser::comment_count] = Comments::getCountByUserId($id); $user_array[KeyParser::unread_notification_count] = Notification::getNotificationByUserToCustomPaginate(CONSTANTS::NOTIFICATION_STATUS_UNREAD, CONSTANTS::ORDER_DESC, $id, null, true); $user_array[KeyParser::new_notification_count] = Notification::getNotificationByUserToCustomPaginate(CONSTANTS::NOTIFICATION_STATUS_NEW, CONSTANTS::ORDER_DESC, $id, null, true); if ($viewer_id) { $user_array[KeyParser::is_followed_by_viewer] = Follow::isFollowed($viewer_id, $id); } return $user_array; }
/** * Returns either review, checkin, or bookmark activity based on $type parameter. * * @param $type - activity type. Either 'checkin', 'review', or 'bookmark' * @param $type_id - ID for checkin/review/bookmark activity * @return mixed */ public static function getActivityType($type, $type_id) { $arr = array(); switch ($type) { case CONSTANTS::CHECKIN: $check_in = CheckIns::find($type_id); if ($check_in) { $arr[KeyParser::checkin] = ModelFormatter::checkinFormat($check_in); } else { $arr[KeyParser::checkin] = array(); } $photos = Photos::getByType(CONSTANTS::CHECKIN, $type_id); $arr[KeyParser::photos] = Photos::convertPhotosToArray($photos); break; case CONSTANTS::REVIEW: $review = Reviews::find($type_id); if ($review) { $arr[KeyParser::review] = ModelFormatter::reviewFormat($review); } else { $arr[KeyParser::review] = array(); } $photos = Photos::getByType(CONSTANTS::REVIEW, $type_id); $arr[KeyParser::photos] = Photos::convertPhotosToArray($photos); break; case CONSTANTS::PHOTO_UPLOAD_RESTAURANT: $photos = Photos::where('id', $type_id)->get(); $arr[KeyParser::photos] = Photos::convertPhotosToArray($photos); break; } unset($photos_arr); return $arr; }
/** * 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); }