/** * 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; }
/** * @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 }
/** * 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; }
/** * 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); }
/** * Update comment * * @param Request $request * @return Response */ public function editCommentAction(Request $request) { $data = $request->json()->get('Comment'); if (!isset($data['id']) || !isset($data['text']) || !isset($data['user_id'])) { $message = "Format should be: {'Comment': {'id': <int>, 'text': <string>, 'user_id': <int>}}"; return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY); } // Check Ng Words $ng_words = NgWord::ngword_filter($data['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 { $comment = new Comments(); $comment = $comment->editComment($data['id'], $data['user_id'], $data['text']); $comment = ModelFormatter::commentFormat($comment); $user = Users::find($data['user_id']); $user = ModelFormatter::userLongFormat($user); $json_return[KeyParser::data] = array(KeyParser::comment => $comment, KeyParser::user => $user); } catch (\Exception $e) { return showErrorResponse('Error Editing Comment'); } 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; }
/** * Get the target user's following list * Displays if the viewer is also following the users in the target's following list * * @param Request $request * @return Response */ public function followingAction(Request $request) { $json_return[KeyParser::data] = array(KeyParser::users => array()); $user_id = $request->user_id; $viewer_id = $request->viewer_id; if (!$user_id) { return showErrorResponse('Incorrect request parameters', HTTP_UNPROCESSABLE_ENTITY); } $followed_users = Follow::getFollowedUsers($user_id); if ($followed_users) { foreach ($followed_users as $key => $followed_user) { $followed_user_id = $followed_user->id; $json_return[KeyParser::data][KeyParser::users][$key] = ModelFormatter::userLongFormat($followed_user); $json_return[KeyParser::data][KeyParser::users][$key] += array(KeyParser::is_followed_by_user => CONSTANTS::FOLLOW_IS_FOLLOWED, KeyParser::follower_count => Follow::getCountByUserId($followed_user_id, CONSTANTS::FOLLOW_FOLLOWER), KeyParser::review_count => Reviews::getCountByUserId($followed_user_id), KeyParser::is_followed_by_viewer => Follow::isFollowed($viewer_id, $followed_user_id)); } } $json_return[KeyParser::page] = array(KeyParser::current => $followed_users->currentPage(), KeyParser::number => $followed_users->lastPage()); return response()->json($json_return); }
/** * Create a new user * * @param Request $request * @return Response * @throws FacebookRequestException */ public function addAction(Request $request) { $data = $request->json()->get('User'); if (!$data) { return showErrorResponse('Incorrect request parameters', HTTP_UNPROCESSABLE_ENTITY); } try { $new_user = new Users(); $new_user->addUser($data); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } //Send push notifications to all Facebook friends who are using Masarap $fb_access_token = $data[CONSTANTS::KEY_FB_ACCESS_TOKEN]; if (!$fb_access_token) { return showErrorResponse('Failed to access Facebook account'); } FacebookSession::setDefaultApplication(Config::get('services.facebook.client_id'), Config::get('services.facebook.client_secret')); FacebookSession::enableAppSecretProof(false); $facebook_session = new FacebookSession($fb_access_token); $facebook_response = (new FacebookRequest($facebook_session, 'GET', '/me/friends/'))->execute(); $friend_list = $facebook_response->getResponse(); $failed_notifications = array(); foreach ($friend_list->data as $friend) { $friend_user = Users::getByFbId($friend->id); if (!$friend_user) { continue; } $params = array(CONSTANTS::KEY_USER_ID_FROM => $new_user->id, CONSTANTS::KEY_USER_ID_TO => $friend_user->id, CONSTANTS::KEY_TYPE => CONSTANTS::NOTIFICATION_TYPE_FRIEND_JOIN, CONSTANTS::KEY_TYPE_ID => $new_user->id); try { $notification = new Notification(); $notification->addGeneralNotification($params); } catch (\Exception $e) { $failed_notifications[] = $friend_user->id; } } $json_return[KeyParser::data] = array(KeyParser::user => ModelFormatter::userLongFormat($new_user), KeyParser::message => 'User successfully registered and push notifications are sent to Facebook friends', KeyParser::unsent_notifications => $failed_notifications); return response()->json($json_return); }
/** * 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); }