/**
  * @param DiscussionWillBeSaved $event
  * @throws PermissionDeniedException
  * @throws ValidationException
  */
 public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
 {
     $discussion = $event->discussion;
     $actor = $event->actor;
     // TODO: clean up, prevent discussion from being created without tags
     if (isset($event->data['relationships']['tags']['data'])) {
         $linkage = (array) $event->data['relationships']['tags']['data'];
         $newTagIds = [];
         foreach ($linkage as $link) {
             $newTagIds[] = (int) $link['id'];
         }
         $newTags = Tag::whereIn('id', $newTagIds)->get();
         $primaryCount = 0;
         $secondaryCount = 0;
         foreach ($newTags as $tag) {
             if ($actor->cannot('startDiscussion', $tag)) {
                 throw new PermissionDeniedException();
             }
             if ($tag->position !== null && $tag->parent_id === null) {
                 $primaryCount++;
             } else {
                 $secondaryCount++;
             }
         }
         $this->validateTagCount('primary', $primaryCount);
         $this->validateTagCount('secondary', $secondaryCount);
         if ($discussion->exists) {
             $oldTags = $discussion->tags()->get();
             $oldTagIds = $oldTags->lists('id')->all();
             if ($oldTagIds == $newTagIds) {
                 return;
             }
             foreach ($newTags as $tag) {
                 if (!in_array($tag->id, $oldTagIds) && $actor->cannot('addToDiscussion', $tag)) {
                     throw new PermissionDeniedException();
                 }
             }
             $discussion->raise(new DiscussionWasTagged($discussion, $actor, $oldTags->all()));
         }
         $discussion->afterSave(function ($discussion) use($newTagIds) {
             $discussion->tags()->sync($newTagIds);
         });
     } elseif (!$discussion->exists && !$actor->hasPermission('startDiscussion')) {
         throw new PermissionDeniedException();
     }
 }
Esempio n. 2
0
 public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
 {
     if (isset($event->data['relationships']['tags']['data'])) {
         $discussion = $event->discussion;
         $actor = $event->actor;
         $linkage = (array) $event->data['relationships']['tags']['data'];
         $newTagIds = [];
         foreach ($linkage as $link) {
             $newTagIds[] = (int) $link['id'];
         }
         $newTags = Tag::whereIn('id', $newTagIds)->get();
         $primaryCount = 0;
         $secondaryCount = 0;
         foreach ($newTags as $tag) {
             if (!$tag->can($actor, 'startDiscussion')) {
                 throw new PermissionDeniedException();
             }
             if ($tag->position !== null && $tag->parent_id === null) {
                 $primaryCount++;
             } else {
                 $secondaryCount++;
             }
         }
         $this->validatePrimaryTagCount($primaryCount);
         $this->validateSecondaryTagCount($secondaryCount);
         $oldTags = [];
         if ($discussion->exists) {
             $oldTags = $discussion->tags()->get();
             $oldTagIds = $oldTags->lists('id');
             if ($oldTagIds == $newTagIds) {
                 return;
             }
             $discussion->raise(new DiscussionWasTagged($discussion, $actor, $oldTags->all()));
         }
         Discussion::saved(function ($discussion) use($newTagIds) {
             $discussion->tags()->sync($newTagIds);
         });
     }
 }
Esempio n. 3
0
 /**
  * @param DiscussionWasTagged $event
  */
 public function whenDiscussionWasTagged(DiscussionWasTagged $event)
 {
     $oldTags = Tag::whereIn('id', array_pluck($event->oldTags, 'id'));
     $this->updateTags($event->discussion, -1, $oldTags);
     $this->updateTags($event->discussion, 1);
 }