/** * Query the users, restaurant, bookmarks, checkins, reviews * * @param $follower_id * @param int $restaurant_id * @return mixed */ public static function getFollowedActivities($follower_id, $restaurant_id) { $following_ids = Follow::getFollowedUserIds($follower_id); $res_activities = self::whereIn('user_id', $following_ids)->orderBy('date_created', CONSTANTS::ORDER_DESC); if ($restaurant_id) { $res_activities->where('restaurant_id', $restaurant_id); } return $res_activities->paginate(CONSTANTS::RESTAURANTS_GET_ACTIVITIES_PAGINATION_LIMIT); }
/** * Review Add * route: checkins/add * * @param Request $request * @return Response * @throws Exception */ public function addAction(Request $request) { $data = $request->only('json', 'fileField'); $data_photos = $data['fileField']; $data_json = json_decode(file_get_contents($data['json']), true); if (!isset($data_json['CheckIn']) || !isset($data_json['CheckIn']['user_id']) || !isset($data_json['CheckIn']['restaurant_id']) || !isset($data_json['CheckIn']['message'])) { $message = "Format should be: {'CheckIn': {'user_id': <int>, 'restaurant_id': <int>, 'message': <string>}, 'Photos': []}"; return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY); } // check for valid data //Check if restaurant ID is existing $restaurant = Restaurants::find($data_json['CheckIn']['restaurant_id']); if (!$restaurant) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } // Check Ng Words $ng_words = NgWord::ngword_filter($data_json['CheckIn']['message']); if ($ng_words) { $message = "Bad words found: " . implode(', ', $ng_words); return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND); } // check of Ng Words try { DB::beginTransaction(); // Save Checkin Data $checkin = new CheckIns(); $new_checkin = $checkin->addCheckin($data_json['CheckIn']); // Save and Upload Photo $photos_upload = new Photos(); $photos_upload->saveUploadedPhotos($data_photos, $data_json['CheckIn'], CONSTANTS::CHECKIN, $new_checkin->id); // Send Notification to Followers about the new Checkin $followers = Follow::getFollowerUsersAll($new_checkin->user_id); $notification = new Notification(); foreach ($followers as $follower) { $notification->addNotificationNewCheckin($new_checkin->user_id, $follower->follower_user_id, $new_checkin->id, $new_checkin->restaurant_id); } //Add Activity $activity = new Activities(); $new_activity = $activity->addActivity(CONSTANTS::CHECKIN, $new_checkin->id, $new_checkin->user_id, $new_checkin->restaurant_id); $photos = Photos::getByType(CONSTANTS::CHECKIN, $new_checkin->id); $photos_array = Photos::convertPhotosToArray($photos); $user = Users::find($new_checkin->user_id); $json_return[KeyParser::data] = array(KeyParser::activity => ModelFormatter::activityFormat($new_activity), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::checkin => ModelFormatter::checkinFormat($new_checkin), KeyParser::photos => $photos_array); //DB Commit DB::commit(); return response()->json($json_return); } catch (\Exception $e) { DB::rollback(); return showErrorResponse($e->getMessage()); } // end catch }
/** * Get the list of users that likes the activity * * @param request $request * @return Response */ public function likerListAction(Request $request) { $viewer_id = $request->viewer_id; $type = $request->type; $type_id = $request->type_id; $error_msg = checkTypeId($type, $type_id); if ($error_msg) { return $error_msg; } $liker_list = Like::getLikerList($type, $type_id); $json_return[KeyParser::data] = array(); if ($liker_list) { foreach ($liker_list as $index => $liker) { $user_id = $liker->user_id; $user_data = Users::find($user_id); if ($user_data) { $array = ModelFormatter::userLongFormat($user_data); $array += array(KeyParser::follower_count => Follow::getCountByUserId($user_id, CONSTANTS::FOLLOW_FOLLOWER), KeyParser::review_count => Reviews::getCountByUserId($user_id), KeyParser::is_followed_by_viewer => Follow::isFollowed($viewer_id, $user_id)); $json_return[KeyParser::data][] = $array; unset($array); } // end of check $user_date } $json_return[KeyParser::like_count] = Like::getCount($type, $type_id); } $json_return[KeyParser::page] = array(KeyParser::current => $liker_list->currentPage(), KeyParser::number => $liker_list->lastPage()); 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 }
/** * 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 a list of Twitter friends which you have not yet followed * * @param Request $request * @return Response * @throws Exception */ public function followTwitterUsersAction(Request $request) { $json_return[KeyParser::data] = array(KeyParser::users => array(), Keyparser::is_private => CONSTANTS::TWITTER_PUBLIC); $user_id = $request->json()->get('User')['user_id']; $twitter_id = $request->json()->get('User')['twitter_id']; if (!$twitter_id || !$user_id) { return showErrorResponse('Failed to access Twitter account'); } $settings = array('oauth_access_token' => Config::get('services.twitter.oauth_access_token'), 'oauth_access_token_secret' => Config::get('services.twitter.oauth_access_token_secret'), 'consumer_key' => Config::get('services.twitter.consumer_key'), 'consumer_secret' => Config::get('services.twitter.consumer_secret')); $url = 'https://api.twitter.com/1.1/friends/ids.json'; $getfield = "?user_id={$twitter_id}"; $requestMethod = 'GET'; $twitter = new \TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(); $friends = json_decode($response); $twitter_friends = array(); $followed_users = array(); $friend_count = 0; if (isset($friends->error)) { $json_return[KeyParser::data][Keyparser::is_private] = CONSTANTS::TWITTER_PRIVATE; } else { foreach ($friends->ids as $friend_id) { $friend_user = Users::getByTwitterId($friend_id); if (!$friend_user) { continue; } $is_followed = Follow::isFollowed($user_id, $friend_user->id); $follower_count = Follow::getCountByUserId($friend_user->id, CONSTANTS::FOLLOW_FOLLOWER); $review_count = Reviews::getCountByUserId($friend_user->id); if (!$is_followed && $friend_user->id != $user_id) { $twitter_friends[$friend_count] = ModelFormatter::userFormat($friend_user); $twitter_friends[$friend_count] += array(KeyParser::follower_count => $follower_count, KeyParser::review_count => $review_count, KeyParser::is_followed_by_viewer => $is_followed); } elseif ($is_followed && $friend_user->id != $user_id) { $followed_users[$friend_count] = ModelFormatter::userFormat($friend_user); $followed_users[$friend_count] += array(KeyParser::follower_count => $follower_count, KeyParser::review_count => $review_count, KeyParser::is_followed_by_viewer => $is_followed); } $friend_count++; } $twitter_friends = array_merge($twitter_friends, $followed_users); $json_return[KeyParser::data][KeyParser::users] = $twitter_friends; } return response()->json($json_return); }
/** * Get list of users with the most number of activities * Prioritize users that are not being followed yet * * @param $user_id * @return Response */ public function viewFeaturedUsersAction($user_id) { $followed_users = array(); $featured_users = array(); $json_return[KeyParser::data] = array(); $users = Users::getUsersWithMostActivities()->toArray(); $all_followed_users = Follow::getFollowedUserIds($user_id); foreach ($users as $user) { $is_followed = in_array($user['id'], $all_followed_users); if ($user['id'] != $user_id) { if ($is_followed) { $followed_users[] = $user['id']; } else { $featured_users[] = $user['id']; } } } $featured_users = array_merge($featured_users, $followed_users); $featured_users = array_slice($featured_users, 0, 20); foreach ($featured_users as $index => $featured_user) { $json_return[KeyParser::data][KeyParser::users][$index] = Users::getStatistics($featured_user, $user_id); } return response()->json($json_return); }
/** * 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 }