/** * Get comment count by user id * * @param $user_id * @return mixed */ public static function getCountByUserId($user_id) { $user_comments = self::where('user_id', $user_id)->get(); $user_valid_comments = 0; foreach ($user_comments as $user_comment) { switch ($user_comment->type) { case CONSTANTS::REVIEW: $review = Reviews::find($user_comment->type_id); if ($review && Restaurants::isExists($review->restaurant_id)) { $user_valid_comments++; } break; case CONSTANTS::CHECKIN: $checkin = CheckIns::find($user_comment->type_id); if ($checkin && Restaurants::isExists($checkin->restaurant_id)) { $user_valid_comments++; } break; case CONSTANTS::PHOTO: $photo = Photos::find($user_comment->type_id); if ($photo && Restaurants::isExists($photo->restaurant_id)) { $user_valid_comments++; } break; } } return $user_valid_comments; }
/** * Returns all notification data per user_id and status using lengthAwarePagination * * @param int $status * @param string $order * @param bool $user_id_to * @param int current_page * @param boolean $count_only * @return LengthAwarePaginator */ public static function getNotificationByUserToCustomPaginate($status = CONSTANTS::NOTIFICATION_STATUS_DELETED, $order = CONSTANTS::ORDER_DESC, $user_id_to = false, $current_page = CONSTANTS::FIRST_PAGE, $count_only = false) { $notifications = self::where('status', '<=', $status); if ($user_id_to) { $notifications->where('user_id_to', $user_id_to); } $notifications = $notifications->orderBy('date_created', $order)->get(); $data = array(); $duplicate_activities = array(); $notif_count = 0; foreach ($notifications as $notification) { $type_id = $notification->type_id; $type = $notification->type; $restaurant_id = $notification->restaurant_id; if ($restaurant_id != 0 && !Restaurants::isExists($restaurant_id)) { continue; } $from_user = Users::find($notification->user_id_from); if (!$count_only) { if ($notification->status == CONSTANTS::NOTIFICATION_STATUS_NEW) { $notification->updateStatus(CONSTANTS::NOTIFICATION_STATUS_UNREAD); } } if ($from_user && isset($duplicate_activities[$type][$type_id])) { $date_diff = strtotime($duplicate_activities[$type][$type_id][KeyParser::date_created]) - strtotime($notification->date_created); if ($date_diff < CONSTANTS::DAY_SECOND_VALUE && $date_diff >= 0) { $notif_index = $duplicate_activities[$type][$type_id][KeyParser::index]; $user_fullname = $from_user->getUserFullName(); if (!array_key_exists(KeyParser::usernames_from, $data[$notif_index][KeyParser::notification]) || !in_array($user_fullname, $data[$notif_index][KeyParser::notification][KeyParser::usernames_from])) { $data[$notif_index][KeyParser::notification][KeyParser::usernames_from][] = $user_fullname; $data[$notif_index][KeyParser::notification][KeyParser::users_from][] = $from_user->toArray(); } continue; } } $data[$notif_count][KeyParser::notification] = ModelFormatter::notificationFormat($notification); $to_user = Users::find($notification->user_id_to); if ($to_user) { $data[$notif_count][KeyParser::notification] += array(KeyParser::facebook_id_to => $to_user->facebook_id); } if ($from_user) { $data[$notif_count][KeyParser::notification] += array(KeyParser::facebook_id_from => $from_user->facebook_id, KeyParser::usernames_from => array($from_user->getUserFullName()), KeyParser::users_from => array($from_user->toArray())); } $duplicate_activities[$type][$type_id] = array(KeyParser::date_created => $notification->date_created, KeyParser::index => $notif_count); $restaurant = Restaurants::find($notification->restaurant_id); if ($restaurant) { $data[$notif_count][KeyParser::notification] += array(KeyParser::restaurant_name => $restaurant->name); } $notif_count++; } if ($count_only) { return count($data); } $max_results = CONSTANTS::NOTIFICATIONS_VIEW_PAGINATION_LIMIT; $offset = $current_page * $max_results - $max_results; $notification_data = array_slice($data, $offset, $max_results); $paginated_data = new LengthAwarePaginator($notification_data, count($data), $max_results); return $paginated_data; }
/** * 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); }
/** * Review Edit * route: reviews/edit * * @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['Review'] = $request->json()->get('Review'); 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>, 'title': <string>, 'text': <string>, 'price': <int>, 'date_visited': <string>, 'rating': <double>, 'restaurant_id': <int>}}"; 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']['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 { // Edit Review Data $review = new Reviews(); $edit_review = $review->editReview($id, $data_json['Review']); $photos = Photos::getByType(CONSTANTS::REVIEW, $edit_review->id); $photos_array = Photos::convertPhotosToArray($photos); $restaurant = Restaurants::find($edit_review->restaurant_id); $user = Users::find($edit_review->user_id); $json_return[KeyParser::data] = array(KeyParser::review => ModelFormatter::reviewFormat($edit_review), 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 }