示例#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);
 }
示例#2
0
 /**
  * Add like on review, checkin or photo
  *
  * @param $request
  * @return mixed
  */
 public function addAction(Request $request)
 {
     $data = $request->json()->get('Like');
     $type = $data['type'];
     $type_id = $data['type_id'];
     $user_id = $data['user_id'];
     $user_data = Users::find($user_id);
     if (is_null($user_data)) {
         return showErrorResponse('Invalid user');
     }
     $is_liked = Like::isLiked($user_id, $type, $type_id);
     if ($is_liked) {
         $like_count = Like::getCount($type, $type_id);
         $json_return[KeyParser::data] = array(KeyParser::type => $type, KeyParser::type_id => $type_id, KeyParser::user_id => $user_id, KeyParser::is_existing => CONSTANTS::LIKE_IS_EXISTING, KeyParser::like_count => $like_count);
         return response()->json($json_return);
     }
     switch ($type) {
         case CONSTANTS::REVIEW:
             $like_object = Reviews::find($type_id);
             $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_REVIEW;
             if (!$like_object) {
                 $status_code = CONSTANTS::ERROR_CODE_REVIEW_MISSING;
             }
             break;
         case CONSTANTS::CHECKIN:
             $like_object = CheckIns::find($type_id);
             $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_CHECKIN;
             if (!$like_object) {
                 $status_code = CONSTANTS::ERROR_CODE_CHECKIN_MISSING;
             }
             break;
         case CONSTANTS::PHOTO:
             $like_object = Photos::find($type_id);
             $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_PHOTO;
             if (!$like_object) {
                 $status_code = CONSTANTS::ERROR_CODE_PHOTO_MISSING;
             }
             break;
         default:
             return showErrorResponse('Invalid type', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_INVALID_TYPE);
     }
     if (!isset($like_object)) {
         return showErrorResponse('Invalid type id', HTTP_ACCEPTED, $status_code);
     }
     if (!Restaurants::isExists($like_object->restaurant_id)) {
         return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL);
     }
     try {
         $like_data = new Like();
         $query_response = $like_data->addLike($type_id, $type, $user_id, $like_object, $like_type);
     } catch (\Exception $e) {
         return showErrorResponse($e->getMessage());
     }
     $json_return[KeyParser::data] = array(KeyParser::type => $query_response->type, KeyParser::type_id => $query_response->type_id, KeyParser::user_id => $query_response->user_id, KeyParser::is_existing => CONSTANTS::LIKE_IS_NOT_EXISTING, KeyParser::like_count => Like::getCount($type, $type_id));
     return response()->json($json_return);
 }
示例#3
0
 /**
  * Output JSON output of comments in a photo
  *
  * @param $id
  * @return Response
  */
 public function viewByPhotoIdAction($id)
 {
     $photo = Photos::find($id);
     if (!$photo) {
         return showErrorResponse('Photo not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_PHOTO_MISSING);
     }
     if (!Restaurants::isExists($photo->restaurant_id)) {
         return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL);
     }
     $json_return = array(KeyParser::data => array(), KeyParser::comment_count => 0, KeyParser::page => array());
     $comments = Comments::getByTypePaginate(CONSTANTS::PHOTO, $id);
     if ($comments->count()) {
         foreach ($comments as $comment) {
             $json_return[KeyParser::data][] = array(KeyParser::comment => ModelFormatter::commentFormat($comment), KeyParser::user => Users::find($comment->user_id));
         }
         $json_return[KeyParser::comment_count] = Comments::getCountByType(CONSTANTS::PHOTO, $id);
         $json_return[KeyParser::page] = array(KeyParser::current => $comments->currentPage(), KeyParser::number => $comments->lastPage());
     }
     return response()->json($json_return);
 }
示例#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;
}
示例#5
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);
 }
示例#6
0
 /**
  * Add new comment to table
  *
  * @param $text
  * @param $type
  * @param $type_id
  * @param $user_id
  * @throws \Exception
  */
 public function addComment($text, $type, $type_id, $user_id)
 {
     $connection = $this->getConnection();
     try {
         $connection->beginTransaction();
         $type_object = null;
         switch ($type) {
             case CONSTANTS::REVIEW:
                 $type_object = Reviews::find($type_id);
                 if (!$type_object) {
                     //Review not Found
                     throw new \Exception(CONSTANTS::ERROR_CODE_REVIEW_MISSING);
                 }
                 $this->type = CONSTANTS::REVIEW;
                 $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_REVIEW;
                 break;
             case CONSTANTS::CHECKIN:
                 $type_object = CheckIns::find($type_id);
                 if (!$type_object) {
                     throw new \Exception(CONSTANTS::ERROR_CODE_CHECKIN_MISSING);
                 }
                 $this->type = CONSTANTS::CHECKIN;
                 $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_CHECKIN;
                 break;
             case CONSTANTS::PHOTO:
                 $type_object = Photos::find($type_id);
                 if (!$type_object) {
                     throw new \Exception(CONSTANTS::ERROR_CODE_PHOTO_MISSING);
                 }
                 $this->type = CONSTANTS::PHOTO;
                 $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_PHOTO;
                 break;
             default:
                 throw new \Exception(CONSTANTS::ERROR_CODE_INVALID_TYPE);
         }
         $this->type_id = $type_id;
         $this->comment = $text;
         $this->status = CONSTANTS::STATUS_ENABLED;
         $this->user_id = $user_id;
         $this->date_created = date('Y-m-d H:i:s');
         $this->save();
         $restaurant_id = $type_object['restaurant_id'];
         $owner_id = $type_object['user_id'];
         if ($user_id != $owner_id) {
             $notification_data = new Notification();
             $notification_data->addCommentNotification($user_id, $owner_id, $comment_type, $type_id, $restaurant_id);
         }
         $connection->commit();
     } catch (\Exception $e) {
         $connection->rollBack();
         throw $e;
     }
 }