Exemplo n.º 1
0
 /**
  * Update the specified resource in storage.
  * PATCH /posts/{id}
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     $user = Auth::user();
     //		$validator = Post::validate(Input::all());
     //
     //		if ($validator->fails())
     //			return $this->respondInsufficientPrivileges($validator->messages()->all());
     $post = Post::find($id);
     if (!$post) {
         return $this->respondNotFound('Post not found');
     }
     if (!$user->can('Post.update', $post)) {
         return $this->respondInsufficientPrivileges('Unauthorized action');
     }
     if (strlen(Input::get('text')) > 10000) {
         return $this->respondInsufficientPrivileges('Слишком длинный текст');
     }
     $post->fill(Input::all());
     if ($post->save()) {
         if (Input::has('attachments') && !empty(Input::get('attachments'))) {
             $post->cars()->detach();
             $post->geos()->detach();
             $post->carsWithNumbers()->detach();
             $attachments = Input::get('attachments');
             foreach ($attachments as $attachment) {
                 $carHelper = new Helpers\carHelper();
                 if ($attachment['type'] == 'Geo') {
                     $geo = Geo::create(['long' => $attachment['long'], 'lat' => $attachment['lat'], 'location' => $attachment['location']]);
                     $post->geos()->save($geo);
                 }
                 if ($attachment['type'] == 'Car') {
                     $car = $carHelper::fetchCar($user, $attachment['id']);
                     if ($car) {
                         $post->cars()->attach($car->id);
                     }
                 }
                 if ($attachment['type'] == 'CarNumber') {
                     $car = $carHelper::fetchCar($user, $attachment['id']);
                     if ($car) {
                         $post->carsWithNumbers()->attach($car->id);
                     }
                 }
                 if ($attachment['type'] == 'Image') {
                     $image = Image::find($attachment['id']);
                     if ($image && !$post->images()->find($attachment['id'])) {
                         $post->images()->save($image);
                     }
                     if ($post->images()->find($attachment['id'])) {
                         $images[] = $image->id;
                     }
                 }
             }
             if (isset($images)) {
                 $post->images()->whereNotIn('id', $images)->delete();
             } else {
                 $post->images()->delete();
             }
         } else {
             $post->images()->delete();
             $post->cars()->detach();
             $post->geos()->detach();
             $post->carsWithNumbers()->detach();
         }
         $post->load('cars', 'geos', 'images');
         return $this->respond($this->collectionTransformer->transformPost($post, $user->favorites, $user->subscriptions));
     }
     return $this->respondServerError();
 }