Merge pull request #3704 from CachetHQ/hotfix/tags-syncing

Improved Tags Management
This commit is contained in:
James Brooks
2019-07-13 09:12:23 +01:00
committed by GitHub
27 changed files with 255 additions and 749 deletions

View File

@@ -74,6 +74,13 @@ final class CreateComponentCommand
*/
public $meta;
/**
* Tags string.
*
* @var string
*/
public $tags;
/**
* The validation rules.
*
@@ -88,23 +95,25 @@ final class CreateComponentCommand
'group_id' => 'nullable|int',
'enabled' => 'nullable|bool',
'meta' => 'nullable|array',
'tags' => 'nullable|string',
];
/**
* Create a new add component command instance.
*
* @param string $name
* @param string $description
* @param int $status
* @param string $link
* @param int $order
* @param int $group_id
* @param bool $enabled
* @param array|null $meta
* @param string $name
* @param string $description
* @param int $status
* @param string $link
* @param int $order
* @param int $group_id
* @param bool $enabled
* @param array|null $meta
* @param string|null $tags
*
* @return void
*/
public function __construct($name, $description, $status, $link, $order, $group_id, $enabled, $meta)
public function __construct($name, $description, $status, $link, $order, $group_id, $enabled, $meta, $tags = null)
{
$this->name = $name;
$this->description = $description;
@@ -114,5 +123,6 @@ final class CreateComponentCommand
$this->group_id = $group_id;
$this->enabled = $enabled;
$this->meta = $meta;
$this->tags = $tags;
}
}

View File

@@ -78,6 +78,13 @@ final class UpdateComponentCommand
*/
public $meta;
/**
* The tags.
*
* @var string|null
*/
public $tags;
/**
* If this is true, we won't notify subscribers of the change.
*
@@ -114,11 +121,12 @@ final class UpdateComponentCommand
* @param int|null $group_id
* @param bool|null $enabled
* @param array|null $meta
* @param string|null $tags
* @param bool $silent
*
* @return void
*/
public function __construct(Component $component, $name = null, $description = null, $status = null, $link = null, $order = null, $group_id = null, $enabled = null, $meta = null, $silent = null)
public function __construct(Component $component, $name = null, $description = null, $status = null, $link = null, $order = null, $group_id = null, $enabled = null, $meta = null, $tags = null, $silent = null)
{
$this->component = $component;
$this->name = $name;
@@ -129,6 +137,8 @@ final class UpdateComponentCommand
$this->group_id = $group_id;
$this->enabled = $enabled;
$this->meta = $meta;
$this->tags = $tags;
$this->silent = $silent;
$this->tags = $tags;
}
}

View File

@@ -1,51 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Model;
/**
* This is the apply tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class ApplyTagCommand
{
/**
* The model to apply the tag to.
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $model;
/**
* The tag to apply.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* Create a new apply tag command instance.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \CachetHQ\Cachet\Models\Tag $tag
*
* @return void
*/
public function __construct(Model $model, Tag $tag)
{
$this->model = $model;
$this->tag = $tag;
}
}

View File

@@ -1,48 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
/**
* This is the create tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class CreateTagCommand
{
/**
* The tag name.
*
* @var string
*/
public $name;
/**
* The tag slug.
*
* @var string|null
*/
public $slug;
/**
* Create a new create tag command instance.
*
* @param string $name
* @param string|null $slug
*
* @return void
*/
public function __construct($name, $slug = null)
{
$this->name = $name;
$this->slug = $slug;
}
}

View File

@@ -1,41 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
/**
* This is the delete tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class DeleteTagCommand
{
/**
* The tag.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* Create a new delete tag command instance.
*
* @param \CachetHQ\Cachet\Models\Tag $tag
*
* @return void
*/
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
}

View File

@@ -1,59 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
/**
* This is the update tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class UpdateTagCommand
{
/**
* The tag.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* The new tag name.
*
* @var string|null
*/
public $name;
/**
* The new tag slug.
*
* @var string|null
*/
public $slug;
/**
* Create a new update tag command instance.
*
* @param \CachetHQ\Cachet\Models\Tag $tag
* @param string|null $name
* @param string|null $slug
*
* @return void
*/
public function __construct(Tag $tag, $name, $slug)
{
$this->tag = $tag;
$this->name = $name;
$this->slug = $slug;
}
}

View File

@@ -53,6 +53,15 @@ class CreateComponentCommandHandler
{
$component = Component::create($this->filter($command));
// Sync the tags into the component.
if ($command->tags) {
collect(preg_split('/ ?, ?/', $command->tags))->filter()->map(function ($tag) {
return trim($tag);
})->pipe(function ($tags) use ($component) {
$component->attachTags($tags);
});
}
event(new ComponentWasCreatedEvent($this->auth->user(), $component));
return $component;

View File

@@ -56,6 +56,15 @@ class UpdateComponentCommandHandler
$component->update($this->filter($command));
// Sync the tags into the component.
if ($command->tags) {
collect(preg_split('/ ?, ?/', $command->tags))->filter()->map(function ($tag) {
return trim($tag);
})->pipe(function ($tags) use ($component) {
$component->syncTags($tags);
});
}
event(new ComponentWasUpdatedEvent($this->auth->user(), $component));
return $component;

View File

@@ -1,39 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Models\Taggable;
/**
* This is the apply tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class ApplyTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand $command
*
* @return void
*/
public function handle(ApplyTagCommand $command)
{
Taggable::firstOrCreate([
'tag_id' => $command->tag->id,
'taggable_id' => $command->model->id,
'taggable_type' => $command->model->getTable(),
]);
}
}

View File

@@ -1,39 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Support\Str;
/**
* This is the create tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class CreateTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand $command
*
* @return \CachetHQ\Cachet\Models\Tag
*/
public function handle(CreateTagCommand $command)
{
return Tag::firstOrCreate([
'name' => $command->name,
'slug' => $command->slug ? $command->slug : Str::slug($command->name),
]);
}
}

View File

@@ -1,34 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand;
/**
* This is the delete tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class DeleteTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand $command
*
* @return \CachetHQ\Cachet\Models\Tag
*/
public function handle(DeleteTagCommand $command)
{
$command->tag->delete();
}
}

View File

@@ -1,38 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand;
use Illuminate\Support\Str;
/**
* This is the create tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class UpdateTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand $command
*
* @return void
*/
public function handle(UpdateTagCommand $command)
{
return $command->tag->update([
'name' => $command->name,
'slug' => $command->slug ? $command->slug : Str::slug($command->name),
]);
}
}

View File

@@ -14,13 +14,10 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Component;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -84,25 +81,13 @@ class ComponentController extends AbstractApiController
Binput::get('order'),
Binput::get('group_id'),
(bool) Binput::get('enabled', true),
Binput::get('meta', null)
Binput::get('meta'),
Binput::get('tags')
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
if (Binput::has('tags')) {
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
}
return $this->item($component);
}
@@ -125,26 +110,14 @@ class ComponentController extends AbstractApiController
Binput::get('order'),
Binput::get('group_id'),
Binput::get('enabled', $component->enabled),
Binput::get('meta', null),
Binput::get('meta'),
Binput::get('tags'),
(bool) Binput::get('silent', false)
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
if (Binput::has('tags')) {
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
}
return $this->item($component);
}

View File

@@ -15,14 +15,10 @@ use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
/**
@@ -113,7 +109,6 @@ class ComponentController extends Controller
public function updateComponentAction(Component $component)
{
$componentData = Binput::get('component');
$tags = Arr::pull($componentData, 'tags');
try {
$component = execute(new UpdateComponentCommand(
@@ -126,6 +121,7 @@ class ComponentController extends Controller
$componentData['group_id'],
$componentData['enabled'],
null, // Meta data cannot be supplied through the dashboard yet.
$componentData['tags'], // Meta data cannot be supplied through the dashboard yet.
true // Silent since we're not really making changes to the component (this should be optional)
));
} catch (ValidationException $e) {
@@ -135,17 +131,6 @@ class ComponentController extends Controller
->withErrors($e->getMessageBag());
}
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
return cachet_redirect('dashboard.components.edit', [$component->id])
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success')));
}
@@ -170,7 +155,6 @@ class ComponentController extends Controller
public function createComponentAction()
{
$componentData = Binput::get('component');
$tags = Arr::pull($componentData, 'tags');
try {
$component = execute(new CreateComponentCommand(
@@ -181,7 +165,8 @@ class ComponentController extends Controller
$componentData['order'],
$componentData['group_id'],
$componentData['enabled'],
null // Meta data cannot be supplied through the dashboard yet.
null, // Meta data cannot be supplied through the dashboard yet.
$componentData['tags']
));
} catch (ValidationException $e) {
return cachet_redirect('dashboard.components.create')
@@ -190,15 +175,6 @@ class ComponentController extends Controller
->withErrors($e->getMessageBag());
}
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
return cachet_redirect('dashboard.components')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
}

View File

@@ -55,4 +55,31 @@ class Tag extends Model
{
return $this->belongsToMany(Component::class);
}
/**
* @param array|\ArrayAccess $values
*
* @return \CachetHQ\Cachet\Models\Tag|static
*/
public static function findOrCreate($values)
{
$tags = collect($values)->map(function ($value) {
if ($value instanceof self) {
return $value;
}
$tag = static::where('name', '=', $value)->first();
if (!$tag instanceof self) {
$tag = static::create([
'name' => $value,
'slug' => Str::slug($value),
]);
}
return $tag;
});
return is_string($values) ? $tags->first() : $tags;
}
}

View File

@@ -1,79 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;
/**
* This is the taggable model class.
*
* @author James Brooks <james@alt-three.com>
*/
class Taggable extends Model
{
use ValidatingTrait;
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'tag_id' => 'int',
'taggable_id' => 'int',
'taggable_type' => 'string',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'tag_id',
'taggable_id',
'taggable_type',
];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'tag_id' => 'required|int',
'taggable_id' => 'required|int',
'taggable_type' => 'required|string',
];
/**
* Get the tag relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function tag()
{
return $this->belongsTo(Tag::class);
}
/**
* Get the taggable relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function taggable()
{
return $this->morphTo();
}
}

View File

@@ -13,14 +13,43 @@ namespace CachetHQ\Cachet\Models\Traits;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* This is the has tags trait.
* Code based on https://github.com/spatie/laravel-tags.
*
* @author James Brooks <james@alt-three.com>
*/
trait HasTags
{
/**
* @var array
*/
protected $queuedTags = [];
/**
* Boot the trait.
*
* @return void
*/
public static function bootHasTags()
{
static::created(function (Model $taggableModel) {
if (count($taggableModel->queuedTags) > 0) {
$taggableModel->attachTags($taggableModel->queuedTags);
$taggableModel->queuedTags = [];
}
});
static::deleted(function (Model $deletedModel) {
$tags = $deletedModel->tags()->get();
$deletedModel->detachTags($tags);
});
}
/**
* Get the tags relation.
*
@@ -31,6 +60,20 @@ trait HasTags
return $this->morphToMany(Tag::class, 'taggable');
}
/**
* @param string|array|\ArrayAccess|\CachetHQ\Cachet\Models\Tag $tags
*/
public function setTagsAttribute($tags)
{
if (!$this->exists) {
$this->queuedTags = $tags;
return;
}
$this->attachTags($tags);
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array|\ArrayAccess $tags
@@ -43,7 +86,7 @@ trait HasTags
$tags->each(function ($tag) use ($query) {
$query->whereHas('tags', function (Builder $query) use ($tag) {
return $query->where('id', $tag ? $tag->id : 0);
return $query->where('tags.id', $tag ? $tag->id : 0);
});
});
@@ -61,12 +104,78 @@ trait HasTags
$tags = static::convertToTags($tags);
return $query->whereHas('tags', function (Builder $query) use ($tags) {
$tagIds = $tags->pluck('id')->toArray();
$tagIds = collect($tags)->pluck('id');
$query->whereIn('taggables.tag_id', $tagIds);
$query->whereIn('tags.id', $tagIds);
});
}
/**
* @param array|\ArrayAccess|\CachetHQ\Cachet\Models\Tag $tags
*
* @return $this
*/
public function attachTags($tags)
{
$tags = collect(Tag::findOrCreate($tags));
$this->tags()->syncWithoutDetaching($tags->pluck('id')->toArray());
return $this;
}
/**
* @param string|\CachetHQ\Cachet\Models\Tag $tag
*
* @return $this
*/
public function attachTag($tag)
{
return $this->attachTags([$tag]);
}
/**
* @param array|\ArrayAccess $tags
*
* @return $this
*/
public function detachTags($tags)
{
$tags = static::convertToTags($tags);
collect($tags)
->filter()
->each(function (Tag $tag) {
$this->tags()->detach($tag);
});
return $this;
}
/**
* @param string|\CachetHQ\Cachet\Models\Tag $tag
*
* @return $this
*/
public function detachTag($tag)
{
return $this->detachTags([$tag]);
}
/**
* @param array|\ArrayAccess $tags
*
* @return $this
*/
public function syncTags($tags)
{
$tags = collect(Tag::findOrCreate($tags));
$this->tags()->sync($tags->pluck('id')->toArray());
return $this;
}
/**
* Convert a list of tags into a collection of \CachetHQ\Cachet\Models\Tag.
*

View File

@@ -53,7 +53,7 @@ class ComponentPresenter extends BasePresenter implements Arrayable
*/
public function tags()
{
return $this->wrappedObject->tags->pluck('tag.name', 'tag.slug');
return $this->wrappedObject->tags->pluck('name', 'slug');
}
/**