/** * @param User $actor * @param Tag $tag * @return bool|null */ public function addToDiscussion(User $actor, Tag $tag) { $disallowedTags = Tag::getIdsWhereCannot($actor, 'discussion.startWithoutApproval'); if (in_array($tag->id, $disallowedTags)) { return false; } }
/** * @param CreateTag $command * @return Tag */ public function handle(CreateTag $command) { $actor = $command->actor; $data = $command->data; $this->forum->assertCan($actor, 'createTag'); $tag = Tag::build(array_get($data, 'attributes.name'), array_get($data, 'attributes.slug'), array_get($data, 'attributes.description'), array_get($data, 'attributes.color'), array_get($data, 'attributes.isHidden')); $tag->save(); return $tag; }
public function scopeDiscussionVisibility(ScopeModelVisibility $event) { // Hide discussions which have tags that the user is not allowed to see. if ($event->model instanceof Discussion) { $event->query->whereNotExists(function ($query) use($event) { return $query->select(app('flarum.db')->raw(1))->from('discussions_tags')->whereIn('tag_id', Tag::getNotVisibleTo($event->actor))->whereRaw('discussion_id = ' . app('flarum.db')->getQueryGrammar()->wrap('discussions.id')); }); } }
/** * @param PrepareApiData $event */ public function loadTagsRelationship(PrepareApiData $event) { // Expose the complete tag list to clients by adding it as a // relationship to the /api/forum endpoint. Since the Forum model // doesn't actually have a tags relationship, we will manually load and // assign the tags data to it using an event listener. if ($event->isController(ShowForumController::class)) { $event->data['tags'] = Tag::whereVisibleTo($event->actor)->with('lastDiscussion')->get(); } }
public function scopeHiddenDiscussionVisibility(ScopeHiddenDiscussionVisibility $event) { // By default, discussions are not visible to the public if they are // hidden or contain zero comments - unless the actor has a certain // permission. Since we grant permissions per-tag, we will make // discussions visible in the tags for which the user has that // permission. $event->query->orWhereExists(function ($query) use($event) { return $query->select(new Expression(1))->from('discussions_tags')->whereIn('tag_id', Tag::getIdsWhereCan($event->actor, $event->permission))->where('discussions.id', new Expression('discussion_id')); }); }
public function hideTags(DiscussionSearchWillBePerformed $event) { $query = $event->search->getQuery(); foreach ($event->search->getActiveGambits() as $gambit) { if ($gambit instanceof TagGambit) { return; } } $query->whereNotExists(function ($query) { return $query->select(app('flarum.db')->raw(1))->from('discussions_tags')->whereIn('tag_id', Tag::where('is_hidden', 1)->lists('id'))->where('discussions.id', new Expression('discussion_id')); }); }
/** * @param ConfigureDiscussionSearch $event */ public function hideTagsFromDiscussionList(ConfigureDiscussionSearch $event) { $query = $event->search->getQuery(); foreach ($event->search->getActiveGambits() as $gambit) { if ($gambit instanceof TagGambit) { return; } } $query->whereNotExists(function ($query) { return $query->select(new Expression(1))->from('discussions_tags')->whereIn('tag_id', Tag::where('is_hidden', 1)->lists('id'))->where('discussions.id', new Expression('discussion_id')); }); }
public function loadTagsRelationship(WillSerializeData $event) { // Expose the complete tag list to clients by adding it as a // relationship to the /api/forum endpoint. Since the Forum model // doesn't actually have a tags relationship, we will manually load and // assign the tags data to it using an event listener. if ($event->action instanceof Forum\ShowAction) { $forum = $event->data; $query = Tag::whereVisibleTo($event->request->actor); $forum->tags = $query->with('lastDiscussion')->get(); $forum->tags_ids = $forum->tags->lists('id'); } }
protected function importTags($from, $to) { $colors = ['#F16655', '#F59B66', '#4E89DA', '#5AC169', '#96A2AF']; $this->info('Importing tags...'); $to->table('tags')->truncate(); $channels = $from->table('channel')->orderBy('lft')->get(); $progress = new ProgressBar($this->output, count($channels)); $i = 0; foreach ($channels as $c) { $tag = new Tag(); $tag->id = $c->channelId; $tag->name = $c->title; $tag->slug = $c->slug; $tag->description = $c->description; $tag->color = $colors[$i++ % count($colors)]; $tag->discussions_count = $c->countConversations; $tag->position = $c->lft; $tag->save(); $progress->advance(); } $progress->finish(); $this->info("\n"); }
/** * {@inheritdoc} */ public function handle(ServerRequestInterface $request) { $this->assertAdmin($request->getAttribute('actor')); $order = array_get($request->getParsedBody(), 'order'); Tag::query()->update(['position' => null, 'parent_id' => null]); foreach ($order as $i => $parent) { $parentId = array_get($parent, 'id'); Tag::where('id', $parentId)->update(['position' => $i]); if (isset($parent['children']) && is_array($parent['children'])) { foreach ($parent['children'] as $j => $childId) { Tag::where('id', $childId)->update(['position' => $j, 'parent_id' => $parentId]); } } } return new EmptyResponse(204); }
/** * @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(); } }
public function handle(Request $request) { if (!$request->actor->isAdmin()) { throw new PermissionDeniedException(); } $order = $request->get('order'); Tag::query()->update(['position' => null, 'parent_id' => null]); foreach ($order as $i => $parent) { $parentId = array_get($parent, 'id'); Tag::where('id', $parentId)->update(['position' => $i]); if (isset($parent['children']) && is_array($parent['children'])) { foreach ($parent['children'] as $j => $childId) { Tag::where('id', $childId)->update(['position' => $j, 'parent_id' => $parentId]); } } } return new EmptyResponse(204); }
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); }); } }
/** * Run the migrations. * * @return void */ public function up() { $this->schema->create('tags', function (Blueprint $table) { $table->increments('id'); $table->string('name', 100); $table->string('slug', 100); $table->text('description')->nullable(); $table->string('color', 50)->nullable(); $table->string('background_path', 100)->nullable(); $table->string('background_mode', 100)->nullable(); $table->integer('position')->nullable(); $table->integer('parent_id')->unsigned()->nullable(); $table->string('default_sort', 50)->nullable(); $table->boolean('is_restricted')->default(0); $table->boolean('is_hidden')->default(0); $table->integer('discussions_count')->unsigned()->default(0); $table->integer('last_time')->unsigned()->nullable(); $table->integer('last_discussion_id')->unsigned()->nullable(); }); Tag::unguard(); Tag::insert(['name' => 'General', 'slug' => 'general', 'color' => '#888', 'position' => '0']); }
/** * @param GetPermission $event * @return bool */ public function grantGlobalDiscussionPermissions(GetPermission $event) { if (in_array($event->ability, ['viewDiscussions', 'startDiscussion']) && is_null($event->model)) { return !empty(Tag::getIdsWhereCan($event->actor, $event->ability)); } }
public function scopeTagVisibility(ScopeModelVisibility $event) { if ($event->model instanceof Tag) { $event->query->whereNotIn('id', Tag::getIdsWhereCannot($event->actor, 'view')); } }
/** * @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); }
/** * @param User $actor * @param Builder $query */ public function find(User $actor, Builder $query) { $query->select('flags.*')->leftJoin('posts', 'posts.id', '=', 'flags.post_id')->leftJoin('discussions', 'discussions.id', '=', 'posts.discussion_id')->whereNotExists(function ($query) use($actor) { return $query->select(new Expression(1))->from('discussions_tags')->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'discussion.viewFlags'))->where('discussions.id', new Expression('discussion_id')); }); }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); return $this->tags->whereVisibleTo($actor)->withStateFor($actor)->get(); }
/** * @param User $actor * @param Builder $query */ public function find(User $actor, Builder $query) { $query->whereNotIn('id', Tag::getIdsWhereCannot($actor, 'viewDiscussions')); }
public function boot() { Tag::setValidator($this->app->make('validator')); }
public function scopeEmptyDiscussionVisibility(ScopeEmptyDiscussionVisibility $event) { $event->query->orWhereExists(function ($query) use($event) { return $query->select(new Expression(1))->from('discussions_tags')->whereIn('tag_id', Tag::getIdsWhereCan($event->actor, 'discussion.editPosts'))->where('discussions.id', new Expression('discussion_id')); }); }
public function scopeTagVisibility(ScopeModelVisibility $event) { if ($event->model instanceof Tag) { $event->query->whereNotIn('id', Tag::getNotVisibleTo($event->actor)); } }
/** * Get the ID of a tag with the given slug. * * @param string $slug * @param User|null $user * @return integer */ public function getIdForSlug($slug, User $user = null) { $query = Tag::where('slug', 'like', $slug); return $this->scopeVisibleTo($query, $user)->pluck('id'); }