Tag syncing is now done within the Component commands
This commit is contained in:
@@ -74,6 +74,13 @@ final class CreateComponentCommand
|
|||||||
*/
|
*/
|
||||||
public $meta;
|
public $meta;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tags string.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $tags;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The validation rules.
|
* The validation rules.
|
||||||
*
|
*
|
||||||
@@ -88,23 +95,25 @@ final class CreateComponentCommand
|
|||||||
'group_id' => 'nullable|int',
|
'group_id' => 'nullable|int',
|
||||||
'enabled' => 'nullable|bool',
|
'enabled' => 'nullable|bool',
|
||||||
'meta' => 'nullable|array',
|
'meta' => 'nullable|array',
|
||||||
|
'tags' => 'nullable|string',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new add component command instance.
|
* Create a new add component command instance.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $description
|
* @param string $description
|
||||||
* @param int $status
|
* @param int $status
|
||||||
* @param string $link
|
* @param string $link
|
||||||
* @param int $order
|
* @param int $order
|
||||||
* @param int $group_id
|
* @param int $group_id
|
||||||
* @param bool $enabled
|
* @param bool $enabled
|
||||||
* @param array|null $meta
|
* @param array|null $meta
|
||||||
|
* @param string|null $tags
|
||||||
*
|
*
|
||||||
* @return void
|
* @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->name = $name;
|
||||||
$this->description = $description;
|
$this->description = $description;
|
||||||
@@ -114,5 +123,6 @@ final class CreateComponentCommand
|
|||||||
$this->group_id = $group_id;
|
$this->group_id = $group_id;
|
||||||
$this->enabled = $enabled;
|
$this->enabled = $enabled;
|
||||||
$this->meta = $meta;
|
$this->meta = $meta;
|
||||||
|
$this->tags = $tags;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,6 +78,13 @@ final class UpdateComponentCommand
|
|||||||
*/
|
*/
|
||||||
public $meta;
|
public $meta;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The tags.
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
public $tags;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If this is true, we won't notify subscribers of the change.
|
* 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 int|null $group_id
|
||||||
* @param bool|null $enabled
|
* @param bool|null $enabled
|
||||||
* @param array|null $meta
|
* @param array|null $meta
|
||||||
|
* @param string|null $tags
|
||||||
* @param bool $silent
|
* @param bool $silent
|
||||||
*
|
*
|
||||||
* @return void
|
* @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->component = $component;
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
@@ -129,6 +137,8 @@ final class UpdateComponentCommand
|
|||||||
$this->group_id = $group_id;
|
$this->group_id = $group_id;
|
||||||
$this->enabled = $enabled;
|
$this->enabled = $enabled;
|
||||||
$this->meta = $meta;
|
$this->meta = $meta;
|
||||||
|
$this->tags = $tags;
|
||||||
$this->silent = $silent;
|
$this->silent = $silent;
|
||||||
|
$this->tags = $tags;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,15 @@ class CreateComponentCommandHandler
|
|||||||
{
|
{
|
||||||
$component = Component::create($this->filter($command));
|
$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));
|
event(new ComponentWasCreatedEvent($this->auth->user(), $component));
|
||||||
|
|
||||||
return $component;
|
return $component;
|
||||||
|
|||||||
@@ -56,6 +56,15 @@ class UpdateComponentCommandHandler
|
|||||||
|
|
||||||
$component->update($this->filter($command));
|
$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));
|
event(new ComponentWasUpdatedEvent($this->auth->user(), $component));
|
||||||
|
|
||||||
return $component;
|
return $component;
|
||||||
|
|||||||
@@ -84,25 +84,13 @@ class ComponentController extends AbstractApiController
|
|||||||
Binput::get('order'),
|
Binput::get('order'),
|
||||||
Binput::get('group_id'),
|
Binput::get('group_id'),
|
||||||
(bool) Binput::get('enabled', true),
|
(bool) Binput::get('enabled', true),
|
||||||
Binput::get('meta', null)
|
Binput::get('meta'),
|
||||||
|
Binput::get('tags'),
|
||||||
));
|
));
|
||||||
} catch (QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
throw new BadRequestHttpException();
|
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);
|
return $this->item($component);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,26 +113,14 @@ class ComponentController extends AbstractApiController
|
|||||||
Binput::get('order'),
|
Binput::get('order'),
|
||||||
Binput::get('group_id'),
|
Binput::get('group_id'),
|
||||||
Binput::get('enabled', $component->enabled),
|
Binput::get('enabled', $component->enabled),
|
||||||
Binput::get('meta', null),
|
Binput::get('meta'),
|
||||||
|
Binput::get('tags'),
|
||||||
(bool) Binput::get('silent', false)
|
(bool) Binput::get('silent', false)
|
||||||
));
|
));
|
||||||
} catch (QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
throw new BadRequestHttpException();
|
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);
|
return $this->item($component);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ class ComponentController extends Controller
|
|||||||
public function updateComponentAction(Component $component)
|
public function updateComponentAction(Component $component)
|
||||||
{
|
{
|
||||||
$componentData = Binput::get('component');
|
$componentData = Binput::get('component');
|
||||||
$tags = Arr::pull($componentData, 'tags');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$component = execute(new UpdateComponentCommand(
|
$component = execute(new UpdateComponentCommand(
|
||||||
@@ -126,6 +125,7 @@ class ComponentController extends Controller
|
|||||||
$componentData['group_id'],
|
$componentData['group_id'],
|
||||||
$componentData['enabled'],
|
$componentData['enabled'],
|
||||||
null, // Meta data cannot be supplied through the dashboard yet.
|
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)
|
true // Silent since we're not really making changes to the component (this should be optional)
|
||||||
));
|
));
|
||||||
} catch (ValidationException $e) {
|
} catch (ValidationException $e) {
|
||||||
@@ -135,17 +135,6 @@ class ComponentController extends Controller
|
|||||||
->withErrors($e->getMessageBag());
|
->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])
|
return cachet_redirect('dashboard.components.edit', [$component->id])
|
||||||
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success')));
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success')));
|
||||||
}
|
}
|
||||||
@@ -170,7 +159,6 @@ class ComponentController extends Controller
|
|||||||
public function createComponentAction()
|
public function createComponentAction()
|
||||||
{
|
{
|
||||||
$componentData = Binput::get('component');
|
$componentData = Binput::get('component');
|
||||||
$tags = Arr::pull($componentData, 'tags');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$component = execute(new CreateComponentCommand(
|
$component = execute(new CreateComponentCommand(
|
||||||
@@ -181,7 +169,8 @@ class ComponentController extends Controller
|
|||||||
$componentData['order'],
|
$componentData['order'],
|
||||||
$componentData['group_id'],
|
$componentData['group_id'],
|
||||||
$componentData['enabled'],
|
$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) {
|
} catch (ValidationException $e) {
|
||||||
return cachet_redirect('dashboard.components.create')
|
return cachet_redirect('dashboard.components.create')
|
||||||
@@ -190,15 +179,6 @@ class ComponentController extends Controller
|
|||||||
->withErrors($e->getMessageBag());
|
->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')
|
return cachet_redirect('dashboard.components')
|
||||||
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,4 +55,31 @@ class Tag extends Model
|
|||||||
{
|
{
|
||||||
return $this->belongsToMany(Component::class);
|
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 Tag) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag = static::where('name', '=', $value)->first();
|
||||||
|
|
||||||
|
if (!$tag instanceof Tag) {
|
||||||
|
$tag = static::create([
|
||||||
|
'name' => $value,
|
||||||
|
'slug' => Str::slug($value),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tag;
|
||||||
|
});
|
||||||
|
|
||||||
|
return is_string($values) ? $tags->first() : $tags;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Models\Traits;
|
|||||||
|
|
||||||
use CachetHQ\Cachet\Models\Tag;
|
use CachetHQ\Cachet\Models\Tag;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the has tags trait.
|
* This is the has tags trait.
|
||||||
@@ -21,6 +22,33 @@ use Illuminate\Database\Eloquent\Builder;
|
|||||||
*/
|
*/
|
||||||
trait HasTags
|
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.
|
* Get the tags relation.
|
||||||
*
|
*
|
||||||
@@ -31,6 +59,20 @@ trait HasTags
|
|||||||
return $this->morphToMany(Tag::class, 'taggable');
|
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 \Illuminate\Database\Eloquent\Builder $query
|
||||||
* @param array|\ArrayAccess $tags
|
* @param array|\ArrayAccess $tags
|
||||||
@@ -61,12 +103,78 @@ trait HasTags
|
|||||||
$tags = static::convertToTags($tags);
|
$tags = static::convertToTags($tags);
|
||||||
|
|
||||||
return $query->whereHas('tags', function (Builder $query) use ($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.
|
* Convert a list of tags into a collection of \CachetHQ\Cachet\Models\Tag.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ class CreateComponentCommandTest extends AbstractTestCase
|
|||||||
'group_id' => 0,
|
'group_id' => 0,
|
||||||
'enabled' => true,
|
'enabled' => true,
|
||||||
'meta' => null,
|
'meta' => null,
|
||||||
|
'tags' => 'Foo, Bar',
|
||||||
];
|
];
|
||||||
$object = new CreateComponentCommand(
|
$object = new CreateComponentCommand(
|
||||||
$params['name'],
|
$params['name'],
|
||||||
@@ -46,7 +47,8 @@ class CreateComponentCommandTest extends AbstractTestCase
|
|||||||
$params['order'],
|
$params['order'],
|
||||||
$params['group_id'],
|
$params['group_id'],
|
||||||
$params['enabled'],
|
$params['enabled'],
|
||||||
$params['meta']
|
$params['meta'],
|
||||||
|
$params['tags']
|
||||||
);
|
);
|
||||||
|
|
||||||
return compact('params', 'object');
|
return compact('params', 'object');
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class UpdateComponentCommandTest extends AbstractTestCase
|
|||||||
'group_id' => 0,
|
'group_id' => 0,
|
||||||
'enabled' => true,
|
'enabled' => true,
|
||||||
'meta' => null,
|
'meta' => null,
|
||||||
|
'tags' => null,
|
||||||
'silent' => false,
|
'silent' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -52,6 +53,7 @@ class UpdateComponentCommandTest extends AbstractTestCase
|
|||||||
$params['group_id'],
|
$params['group_id'],
|
||||||
$params['enabled'],
|
$params['enabled'],
|
||||||
$params['meta'],
|
$params['meta'],
|
||||||
|
$params['tags'],
|
||||||
$params['silent']
|
$params['silent']
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user