Merge pull request #3087 from CachetHQ/tag-commands
Fix how tags are created
This commit is contained in:
51
app/Bus/Commands/Tag/ApplyTagCommand.php
Normal file
51
app/Bus/Commands/Tag/ApplyTagCommand.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
48
app/Bus/Commands/Tag/CreateTagCommand.php
Normal file
48
app/Bus/Commands/Tag/CreateTagCommand.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
41
app/Bus/Commands/Tag/DeleteTagCommand.php
Normal file
41
app/Bus/Commands/Tag/DeleteTagCommand.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
59
app/Bus/Commands/Tag/UpdateTagCommand.php
Normal file
59
app/Bus/Commands/Tag/UpdateTagCommand.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
39
app/Bus/Handlers/Commands/Tag/ApplyTagCommandHandler.php
Normal file
39
app/Bus/Handlers/Commands/Tag/ApplyTagCommandHandler.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
39
app/Bus/Handlers/Commands/Tag/CreateTagCommandHandler.php
Normal file
39
app/Bus/Handlers/Commands/Tag/CreateTagCommandHandler.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
34
app/Bus/Handlers/Commands/Tag/DeleteTagCommandHandler.php
Normal file
34
app/Bus/Handlers/Commands/Tag/DeleteTagCommandHandler.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
38
app/Bus/Handlers/Commands/Tag/UpdateTagCommandHandler.php
Normal file
38
app/Bus/Handlers/Commands/Tag/UpdateTagCommandHandler.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,13 @@ 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 CachetHQ\Cachet\Models\Tag;
|
||||
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;
|
||||
|
||||
@@ -89,17 +91,16 @@ class ComponentController extends AbstractApiController
|
||||
}
|
||||
|
||||
if (Binput::has('tags')) {
|
||||
$component->tags()->delete();
|
||||
|
||||
// The component was added successfully, so now let's deal with the tags.
|
||||
$tags = preg_split('/ ?, ?/', Binput::get('tags'));
|
||||
|
||||
// For every tag, do we need to create it?
|
||||
$componentTags = array_map(function ($taggable) use ($component) {
|
||||
return Tag::firstOrCreate([
|
||||
'name' => $taggable,
|
||||
])->id;
|
||||
}, $tags);
|
||||
|
||||
$component->tags()->sync($componentTags);
|
||||
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
|
||||
return trim($tag);
|
||||
})->map(function ($tag) {
|
||||
return dispatch(new CreateTagCommand($tag));
|
||||
})->each(function ($tag) use ($component) {
|
||||
dispatch(new ApplyTagCommand($component, $tag));
|
||||
});
|
||||
}
|
||||
|
||||
return $this->item($component);
|
||||
@@ -132,14 +133,16 @@ class ComponentController extends AbstractApiController
|
||||
}
|
||||
|
||||
if (Binput::has('tags')) {
|
||||
$tags = preg_split('/ ?, ?/', Binput::get('tags'));
|
||||
$component->tags()->delete();
|
||||
|
||||
// For every tag, do we need to create it?
|
||||
$componentTags = array_map(function ($taggable) use ($component) {
|
||||
return Tag::firstOrCreate(['name' => $taggable])->id;
|
||||
}, $tags);
|
||||
|
||||
$component->tags()->sync($componentTags);
|
||||
// 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 dispatch(new CreateTagCommand($tag));
|
||||
})->each(function ($tag) use ($component) {
|
||||
dispatch(new ApplyTagCommand($component, $tag));
|
||||
});
|
||||
}
|
||||
|
||||
return $this->item($component);
|
||||
|
||||
@@ -15,11 +15,13 @@ 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 CachetHQ\Cachet\Models\Tag;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
||||
/**
|
||||
@@ -132,15 +134,16 @@ class ComponentController extends Controller
|
||||
->withErrors($e->getMessageBag());
|
||||
}
|
||||
|
||||
$component->tags()->delete();
|
||||
|
||||
// The component was added successfully, so now let's deal with the tags.
|
||||
$tags = preg_split('/ ?, ?/', $tags);
|
||||
|
||||
// For every tag, do we need to create it?
|
||||
$componentTags = array_map(function ($taggable) {
|
||||
return Tag::firstOrCreate(['name' => $taggable])->id;
|
||||
}, $tags);
|
||||
|
||||
$component->tags()->sync($componentTags);
|
||||
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
|
||||
return trim($tag);
|
||||
})->map(function ($tag) {
|
||||
return dispatch(new CreateTagCommand($tag));
|
||||
})->each(function ($tag) use ($component) {
|
||||
dispatch(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')));
|
||||
@@ -187,14 +190,13 @@ class ComponentController extends Controller
|
||||
}
|
||||
|
||||
// The component was added successfully, so now let's deal with the tags.
|
||||
$tags = preg_split('/ ?, ?/', $tags);
|
||||
|
||||
// For every tag, do we need to create it?
|
||||
$componentTags = array_map(function ($taggable) {
|
||||
return Tag::firstOrCreate(['name' => $taggable])->id;
|
||||
}, $tags);
|
||||
|
||||
$component->tags()->sync($componentTags);
|
||||
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
|
||||
return trim($tag);
|
||||
})->map(function ($tag) {
|
||||
return dispatch(new CreateTagCommand($tag));
|
||||
})->each(function ($tag) use ($component) {
|
||||
dispatch(new ApplyTagCommand($component, $tag));
|
||||
});
|
||||
|
||||
return cachet_redirect('dashboard.components')
|
||||
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
|
||||
|
||||
Reference in New Issue
Block a user