Rewrite the tags implementation. Closes #391
This commit is contained in:
committed by
Joseph Cohen
parent
65d0e4f5f1
commit
0fecc5dc8a
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateTagsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('tags', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('slug');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['name', 'slug']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::drop('tags');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateComponentTagTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('component_tag', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->integer('component_id');
|
||||||
|
$table->integer('tag_id');
|
||||||
|
|
||||||
|
$table->index('component_id');
|
||||||
|
$table->index('tag_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::drop('component_tag');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AlterTableComponentsDropTagsColumn extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('components', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('tags');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('components', function (Blueprint $table) {
|
||||||
|
$table->text('tags')->after('link');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,7 +50,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>{{ trans('forms.components.tags') }}</label>
|
<label>{{ trans('forms.components.tags') }}</label>
|
||||||
<textarea name="component[tags]" class="form-control" rows="2"></textarea>
|
<input name="component[tags]" class="form-control" />
|
||||||
<span class="help-block">{{ trans('forms.components.tags-help') }}</span>
|
<span class="help-block">{{ trans('forms.components.tags-help') }}</span>
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|||||||
@@ -50,7 +50,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>{{ trans('forms.components.tags') }}</label>
|
<label>{{ trans('forms.components.tags') }}</label>
|
||||||
<textarea name="component[tags]" class="form-control" rows="2">{{ $component->tags }}</textarea>
|
<input name="component[tags]" class="form-control" value="{{ $component->tagsList }}" />
|
||||||
<span class="help-block">{{ trans('forms.components.tags-help') }}</span>
|
<span class="help-block">{{ trans('forms.components.tags-help') }}</span>
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace CachetHQ\Cachet\Http\Controllers;
|
|||||||
|
|
||||||
use CachetHQ\Cachet\Models\Component;
|
use CachetHQ\Cachet\Models\Component;
|
||||||
use CachetHQ\Cachet\Models\ComponentGroup;
|
use CachetHQ\Cachet\Models\ComponentGroup;
|
||||||
|
use CachetHQ\Cachet\Models\Tag;
|
||||||
use GrahamCampbell\Binput\Facades\Binput;
|
use GrahamCampbell\Binput\Facades\Binput;
|
||||||
use Illuminate\Routing\Controller;
|
use Illuminate\Routing\Controller;
|
||||||
use Illuminate\Support\Facades\Redirect;
|
use Illuminate\Support\Facades\Redirect;
|
||||||
@@ -104,6 +105,7 @@ class DashComponentController extends Controller
|
|||||||
public function updateComponentAction(Component $component)
|
public function updateComponentAction(Component $component)
|
||||||
{
|
{
|
||||||
$_component = Binput::get('component');
|
$_component = Binput::get('component');
|
||||||
|
$tags = array_pull($_component, 'tags');
|
||||||
$component->update($_component);
|
$component->update($_component);
|
||||||
|
|
||||||
if (! $component->isValid()) {
|
if (! $component->isValid()) {
|
||||||
@@ -116,6 +118,19 @@ class DashComponentController extends Controller
|
|||||||
->with('errors', $component->getErrors());
|
->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(
|
$successMsg = sprintf(
|
||||||
'<strong>%s</strong> %s',
|
'<strong>%s</strong> %s',
|
||||||
trans('dashboard.notifications.awesome'),
|
trans('dashboard.notifications.awesome'),
|
||||||
@@ -148,6 +163,9 @@ class DashComponentController extends Controller
|
|||||||
public function createComponentAction()
|
public function createComponentAction()
|
||||||
{
|
{
|
||||||
$_component = Binput::get('component');
|
$_component = Binput::get('component');
|
||||||
|
// We deal with tags separately.
|
||||||
|
$tags = array_pull($_component, 'tags');
|
||||||
|
|
||||||
$component = Component::create($_component);
|
$component = Component::create($_component);
|
||||||
|
|
||||||
if (! $component->isValid()) {
|
if (! $component->isValid()) {
|
||||||
@@ -160,6 +178,19 @@ class DashComponentController extends Controller
|
|||||||
->with('errors', $component->getErrors());
|
->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(
|
$successMsg = sprintf(
|
||||||
'<strong>%s</strong> %s',
|
'<strong>%s</strong> %s',
|
||||||
trans('dashboard.notifications.awesome'),
|
trans('dashboard.notifications.awesome'),
|
||||||
|
|||||||
@@ -85,6 +85,16 @@ class Component extends Model implements TransformableInterface
|
|||||||
return $this->hasMany('CachetHQ\Cachet\Models\Incident', 'component_id', 'id');
|
return $this->hasMany('CachetHQ\Cachet\Models\Incident', 'component_id', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Components can have many tags.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function tags()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany('CachetHQ\Cachet\Models\Tag');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds all components by status.
|
* Finds all components by status.
|
||||||
*
|
*
|
||||||
@@ -121,6 +131,20 @@ class Component extends Model implements TransformableInterface
|
|||||||
return trans('cachet.components.status.'.$this->status);
|
return trans('cachet.components.status.'.$this->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all of the tags on this component.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTagsListAttribute()
|
||||||
|
{
|
||||||
|
$tags = $this->tags->map(function ($tag) {
|
||||||
|
return $tag->name;
|
||||||
|
});
|
||||||
|
|
||||||
|
return implode(', ', $tags->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the transformer instance.
|
* Get the transformer instance.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace CachetHQ\Cachet\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
|
class Tag extends Model
|
||||||
|
{
|
||||||
|
use ValidatingTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The fillable properties.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $fillable = ['name'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overrides the models boot method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function boot()
|
||||||
|
{
|
||||||
|
parent::boot();
|
||||||
|
|
||||||
|
self::creating(function ($tag) {
|
||||||
|
$tag->slug = Str::slug($tag->name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tags can have many components.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function components()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany('CachetHQ\Cachet\Models\Component');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user