Rewrite the tags implementation. Closes #391

This commit is contained in:
James Brooks
2015-01-16 09:51:22 +00:00
committed by Joseph Cohen
parent 65d0e4f5f1
commit 0fecc5dc8a
8 changed files with 202 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ namespace CachetHQ\Cachet\Http\Controllers;
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\Facades\Redirect;
@@ -104,6 +105,7 @@ class DashComponentController extends Controller
public function updateComponentAction(Component $component)
{
$_component = Binput::get('component');
$tags = array_pull($_component, 'tags');
$component->update($_component);
if (! $component->isValid()) {
@@ -116,6 +118,19 @@ class DashComponentController extends Controller
->with('errors', $component->getErrors());
}
// The component was added successfully, so now let's deal with the tags.
$tags = str_replace(', ', ',', $tags); // Clean up.
$tags = explode(',', $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);
$successMsg = sprintf(
'<strong>%s</strong> %s',
trans('dashboard.notifications.awesome'),
@@ -148,6 +163,9 @@ class DashComponentController extends Controller
public function createComponentAction()
{
$_component = Binput::get('component');
// We deal with tags separately.
$tags = array_pull($_component, 'tags');
$component = Component::create($_component);
if (! $component->isValid()) {
@@ -160,6 +178,19 @@ class DashComponentController extends Controller
->with('errors', $component->getErrors());
}
// The component was added successfully, so now let's deal with the tags.
$tags = str_replace(', ', ',', $tags); // Clean up.
$tags = explode(',', $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);
$successMsg = sprintf(
'<strong>%s</strong> %s',
trans('dashboard.notifications.awesome'),