Esempio n. 1
0
 /**
  * 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);
 }
 public function readAction(Request $request)
 {
     $data = $request->json()->get('Notification');
     if (!$data) {
         return showErrorResponse('Incorrect request parameters', HTTP_UNPROCESSABLE_ENTITY);
     }
     $notification = Notification::find($data[CONSTANTS::KEY_ID]);
     if ($notification) {
         $notification = ModelFormatter::notificationFormat($notification->updateStatus(CONSTANTS::NOTIFICATION_STATUS_READ));
     }
     $json_return[KeyParser::data][KeyParser::notification] = $notification;
     return response()->json($json_return);
 }
 /**
  * Returns restaurants recently viewed by a user
  *
  * @param $user_id
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function getAction($user_id)
 {
     $json_return = array();
     if (!$user_id) {
         return showErrorResponse('Invalid user_id');
     }
     $recently_viewed_data = LogRecentlyViewed::getByUserId($user_id);
     $recently_viewed_data = $recently_viewed_data->toArray();
     $page_data = $recently_viewed_data;
     unset($page_data['data']);
     $recently_viewed_data = $recently_viewed_data['data'];
     $recently_viewed = array();
     $restaurant = null;
     foreach ($recently_viewed_data as $rvd) {
         $restaurant = Restaurants::find($rvd['restaurant_id']);
         if ($restaurant) {
             $recently_viewed[] = ModelFormatter::restaurantFormat($restaurant);
         }
         $restaurant = null;
     }
     $json_return = array(KeyParser::data => $recently_viewed, KeyParser::page => array(KeyParser::current => $page_data['current_page'], KeyParser::number => $page_data['last_page']));
     return response()->json($json_return);
 }
Esempio n. 4
0
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;
}
Esempio n. 5
0
 /**
  * Review Delete
  * route: checkin/delete/{id}
  *
  * @param $id
  * @return request
  */
 public function deleteAction($id)
 {
     try {
         $checkin = new CheckIns();
         $checkin->deleteCheckin($id);
         $json_return[KeyParser::data] = array(KeyParser::id => $id, KeyParser::is_success => CONSTANTS::DELETE_SUCCESS);
     } catch (\Exception $e) {
         $message = "Failed to delete checkin with ID {$id}";
         return showErrorResponse($message);
     }
     return response()->json($json_return);
 }
Esempio n. 6
0
 /**
  * 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);
 }
Esempio n. 7
0
 /**
  * Add new restaurant suggest and return JSON data
  * route: /restaurants/suggest
  *
  * @param Request $request
  * @return Response
  */
 public function suggestAction(Request $request)
 {
     $data = $request->json()->get('restaurant');
     if (!isset($data['name']) || !isset($data['address']) || !isset($data['latitude']) || !isset($data['longitude']) || !isset($data['user_id'])) {
         $message = "Format should be: {'restaurant': {'name': <string>, 'address': <string>, 'latitude': <double>, 'longitude': <double>, 'user_id': <int>}}";
         return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY);
     }
     // Check Ng Words
     $ng_words = NgWord::ngword_filter($data['name'] . ' ' . $data['telephone'] . ' ' . $data['address'] . ' ' . $data['cuisines'] . ' ' . $data['other_details']);
     if ($ng_words) {
         $message = "Bad words found: " . implode(', ', $ng_words);
         return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND);
     }
     try {
         $restaurant_suggest = new RestaurantsSuggest();
         $restaurant_suggest->addRestaurantSuggest($data);
         $json_return[KeyParser::data] = ModelFormatter::restaurantSuggestFormat($restaurant_suggest);
     } catch (\Exception $e) {
         return showErrorResponse($e->getMessage());
     }
     return response()->json($json_return);
 }
Esempio n. 8
0
 /**
  * Delete like on review, checkin or photo
  *
  * @param $request
  * @return mixed
  */
 public function deleteAction(Request $request)
 {
     $type = $request->type;
     $type_id = $request->type_id;
     $user_id = $request->user_id;
     $error_msg = checkTypeId($type, $type_id);
     if ($error_msg) {
         return $error_msg;
     }
     $is_liked = Like::isLiked($user_id, $type, $type_id);
     if ($is_liked) {
         try {
             $like = new Like();
             $like->deleteLike($type, $type_id, $user_id);
         } catch (\Exception $e) {
             return showErrorResponse($e->getMessage());
         }
     }
     $json_return[KeyParser::data] = array(KeyParser::type => $type, KeyParser::type_id => $type_id, KeyParser::user_id => $user_id, KeyParser::like_count => Like::getCount($type, $type_id));
     return response()->json($json_return);
 }
Esempio n. 9
0
 /**
  * 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);
 }
Esempio n. 10
0
 /**
  * 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);
 }
Esempio n. 11
0
 /**
  * Displays individual user information
  * route: /users/viewstats/<user_id>?viewer_id=<viewer_id>
  *
  * @param $user_id
  * @return Response
  */
 public function viewStatisticsAction($user_id)
 {
     $viewer_id = Input::get('viewer_id', false);
     $user = Users::getStatistics($user_id, $viewer_id);
     if (!$user) {
         return showErrorResponse('User not found');
     }
     $json_return[KeyParser::data][KeyParser::user] = $user;
     return response()->json($json_return);
 }
Esempio n. 12
0
 /**
  * get all of the bookmarks of a user
  * /bookmarks/user/{user_id}
  *
  * @param $user_id
  * @return Response
  */
 public function userBookmarkListAction($user_id)
 {
     if (!is_numeric($user_id)) {
         return showErrorResponse('Incorrect User ID format');
     }
     $bookmark_list = Bookmarks::getBookmarkByUserId($user_id);
     $user_data = Users::find($user_id);
     $json_return[KeyParser::data] = array();
     if ($bookmark_list) {
         foreach ($bookmark_list as $bookmark) {
             $restaurant_data = Restaurants::find($bookmark->restaurant_id);
             if ($restaurant_data) {
                 $json_return[KeyParser::data][] = array(KeyParser::bookmark => ModelFormatter::bookmarkFormat($bookmark), KeyParser::user => ModelFormatter::userFormat($user_data), KeyParser::restaurant => ModelFormatter::restaurantBookmarkListViewFormat($restaurant_data), KeyParser::categories => Categories::getFormattedRestaurantCategories($bookmark->restaurant_id));
             }
         }
     }
     $json_return[KeyParser::page] = array(KeyParser::current => $bookmark_list->currentPage(), KeyParser::number => $bookmark_list->lastPage());
     return response()->json($json_return);
 }
Esempio n. 13
0
 /**
  * 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);
 }