Ejemplo n.º 1
0
 /**
  * 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);
 }
Ejemplo n.º 2
0
 /**
  * Delete Review
  * @param $id
  * @throws $e
  */
 public function deleteReview($id)
 {
     $connection = $this->getConnection();
     try {
         $connection->beginTransaction();
         $review = Reviews::find($id);
         if ($review) {
             //delete review
             $review->delete();
             //delete activities
             $activities = new Activities();
             $activities->deleteActivity(CONSTANTS::REVIEW, $id);
             //delete comments
             $comments = new Comments();
             $comments->deleteCommentByType(CONSTANTS::REVIEW, $id);
             //delete likes
             $like = new Like();
             $like->deleteLikes(CONSTANTS::REVIEW, $id);
         } else {
             throw new \Exception('No review found');
         }
         $connection->commit();
     } catch (\Exception $e) {
         $connection->rollBack();
         throw $e;
     }
 }
Ejemplo n.º 3
0
 /**
  * Returns the nearby restaurant activities of the user base on location
  * route: /activities/restaurant/near/{longitude}/{latitude}/{distance}
  * get user_id parameter to exclude current user data to show in teh activities
  *
  * @param $longitude
  * @param $latitude
  * @param $distance
  * @return response
  */
 public function getNearRestaurantActivitiesAction($longitude, $latitude, $distance)
 {
     $viewer_id = Input::get('viewer_id', 0);
     $restaurant_ids = array();
     $near_restaurant = Restaurants::getNearbyRestaurants($longitude, $latitude, $distance, CONSTANTS::RESTAURANTS_GET_NEARBY_PAGINATION_LIMIT);
     // get restaurant ids
     foreach ($near_restaurant as $restaurant) {
         $restaurant_ids[] = $restaurant->id;
     }
     $restaurant_activities = Activities::getRestaurantsActivities($restaurant_ids, $viewer_id);
     $activitiesArray = Activities::activitiesQueries($restaurant_activities);
     $page[KeyParser::current] = $restaurant_activities->currentPage();
     $page[KeyParser::number] = $restaurant_activities->lastPage();
     $json_return = array(KeyParser::data => $activitiesArray, KeyParser::page => $page);
     return response()->json($json_return);
 }
Ejemplo n.º 4
0
 /**
  * Delete Checkin
  * @param $id
  * @throws $e
  */
 public function deleteCheckin($id)
 {
     $connection = $this->getConnection();
     try {
         $connection->beginTransaction();
         $checkin = CheckIns::find($id);
         if ($checkin) {
             //delete checkin
             $checkin->delete();
             //delete activities
             $activities = new Activities();
             $activities->deleteActivity(CONSTANTS::CHECKIN, $id);
             //delete comments
             $comments = new Comments();
             $comments->deleteCommentByType(CONSTANTS::CHECKIN, $id);
             //delete likes
             $like = new Like();
             $like->deleteLikes(CONSTANTS::CHECKIN, $id);
         } else {
             throw new \Exception('No checkin found');
         }
         $connection->commit();
     } catch (\Exception $e) {
         $connection->rollBack();
         throw $e;
     }
 }
Ejemplo n.º 5
0
 /**
  * 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
 }
Ejemplo n.º 6
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);
 }
Ejemplo n.º 7
0
 /**
  * Delete bookmark/s and activity when it was bookmarked
  *
  * @param $id
  * @return mixed
  * @throws \Exception
  */
 public function deleteBookmark($id)
 {
     $connection = $this->getConnection();
     try {
         $connection->beginTransaction();
         $bookmark = self::find($id);
         if ($bookmark) {
             $activity = new Activities();
             $activity->deleteActivity(CONSTANTS::BOOKMARK, $id);
             $bookmark->delete();
         } else {
             throw new \Exception('No bookmark found');
         }
         $connection->commit();
     } catch (\Exception $e) {
         $connection->rollBack();
         throw $e;
     }
 }
Ejemplo n.º 8
0
 /**
  * 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
 }