예제 #1
0
 /**
  * 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
 }
예제 #2
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
 }
예제 #3
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
 }