From 77281a7cc57f5899d48a300d1f9afeaac1b8cc27 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 18 Jun 2018 07:13:10 +0100 Subject: [PATCH 1/3] Start testing searching of tags --- app/Models/Traits/Taggable.php | 63 ++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/app/Models/Traits/Taggable.php b/app/Models/Traits/Taggable.php index f7c07a06bb6a..7a3e00249a3a 100644 --- a/app/Models/Traits/Taggable.php +++ b/app/Models/Traits/Taggable.php @@ -11,7 +11,10 @@ namespace CachetHQ\Cachet\Models\Traits; +use CachetHQ\Cachet\Models\Tag; use CachetHQ\Cachet\Models\Taggable as TaggableModel; +use Illuminate\Database\Eloquent\Builder; +use InvalidArgumentException; /** * This is the taggable trait. @@ -23,10 +26,66 @@ trait Taggable /** * Get the tags relation. * - * @return \Illuminate\Database\Eloquent\Relations\MorphMany + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function tags() { - return $this->morphMany(TaggableModel::class, 'taggable'); + return $this->morphToMany(Tag::class, 'taggable'); + } + + /** + * @param \Illuminate\Database\Eloquent\Builder $query + * @param array|\ArrayAccess $tags + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeWithAllTags(Builder $query, $tags) + { + $tags = static::convertToTags($tags); + + return $tags->each(function ($tag) use ($query) { + $query->whereHas('tags', function (Builder $query) use ($tag) { + return $query->where('id', $tag ? $tag->id : 0); + }); + }); + + return $query; + } + + /** + * @param \Illuminate\Database\Eloquent\Builder $query + * @param array|\ArrayAccess $tags + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeWithAnyTags(Builder $query, $tags) + { + $tags = static::convertToTags($tags); + + return $query->whereHas('tags', function (Builder $query) use ($tags) { + $tagIds = $tags->pluck('id')->toArray(); + + // dd($tagIds); + + $query->where('taggables.tag_id', '=', 1); + }); + } + + /** + * Convert a list of tags into a collection of \CachetHQ\Cachet\Models\Tag + * + * @param array|\ArrayAccess $values + * + * @return \Illuminate\Support\Collection + */ + protected static function convertToTags($values) + { + return collect($values)->map(function ($value) { + if ($value instanceof Tag) { + return $value; + } + + return Tag::where('slug', '=', $value)->first(); + }); } } From ab645722744d781266aba57a498f756417b6cda9 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 18 Jun 2018 21:22:43 +0100 Subject: [PATCH 2/3] Add the ability to filter components by tags. Closes #2920 --- app/Http/Controllers/Api/ComponentController.php | 4 ++++ app/Models/Component.php | 4 ++-- app/Models/Incident.php | 4 ++-- app/Models/Traits/{Taggable.php => HasTags.php} | 11 ++++------- 4 files changed, 12 insertions(+), 11 deletions(-) rename app/Models/Traits/{Taggable.php => HasTags.php} (89%) diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 4b9d2964b948..93da74ddb148 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -37,6 +37,10 @@ public function index() $components = Component::enabled(); } + if ($tags = Binput::get('tags')) { + $components->withAnyTags($tags); + } + $components->search(Binput::except(['sort', 'order', 'per_page'])); if ($sortBy = Binput::get('sort')) { diff --git a/app/Models/Component.php b/app/Models/Component.php index 49bfa8750e9c..2504e99c2de4 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -14,7 +14,7 @@ use AltThree\Validator\ValidatingTrait; use CachetHQ\Cachet\Models\Traits\SearchableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait; -use CachetHQ\Cachet\Models\Traits\Taggable; +use CachetHQ\Cachet\Models\Traits\HasTags; use CachetHQ\Cachet\Presenters\ComponentPresenter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -23,7 +23,7 @@ class Component extends Model implements HasPresenter { - use SearchableTrait, SoftDeletes, SortableTrait, Taggable, ValidatingTrait; + use HasTags, SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait; /** * List of attributes that have default values. diff --git a/app/Models/Incident.php b/app/Models/Incident.php index 8e41e95b4f08..37401ded8e93 100644 --- a/app/Models/Incident.php +++ b/app/Models/Incident.php @@ -14,7 +14,7 @@ use AltThree\Validator\ValidatingTrait; use CachetHQ\Cachet\Models\Traits\SearchableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait; -use CachetHQ\Cachet\Models\Traits\Taggable; +use CachetHQ\Cachet\Models\Traits\HasTags; use CachetHQ\Cachet\Presenters\IncidentPresenter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -30,7 +30,7 @@ */ class Incident extends Model implements HasPresenter { - use SearchableTrait, SoftDeletes, SortableTrait, Taggable, ValidatingTrait; + use HasTags, SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait; /** * Status for incident being investigated. diff --git a/app/Models/Traits/Taggable.php b/app/Models/Traits/HasTags.php similarity index 89% rename from app/Models/Traits/Taggable.php rename to app/Models/Traits/HasTags.php index 7a3e00249a3a..a2fae75f51ab 100644 --- a/app/Models/Traits/Taggable.php +++ b/app/Models/Traits/HasTags.php @@ -12,16 +12,15 @@ namespace CachetHQ\Cachet\Models\Traits; use CachetHQ\Cachet\Models\Tag; -use CachetHQ\Cachet\Models\Taggable as TaggableModel; use Illuminate\Database\Eloquent\Builder; use InvalidArgumentException; /** - * This is the taggable trait. + * This is the has tags trait. * * @author James Brooks */ -trait Taggable +trait HasTags { /** * Get the tags relation. @@ -43,7 +42,7 @@ public function scopeWithAllTags(Builder $query, $tags) { $tags = static::convertToTags($tags); - return $tags->each(function ($tag) use ($query) { + $tags->each(function ($tag) use ($query) { $query->whereHas('tags', function (Builder $query) use ($tag) { return $query->where('id', $tag ? $tag->id : 0); }); @@ -65,9 +64,7 @@ public function scopeWithAnyTags(Builder $query, $tags) return $query->whereHas('tags', function (Builder $query) use ($tags) { $tagIds = $tags->pluck('id')->toArray(); - // dd($tagIds); - - $query->where('taggables.tag_id', '=', 1); + $query->whereIn('taggables.tag_id', $tagIds); }); } From a9558d35c5842eb7f9efb0395ae682af46fe27cf Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 18 Jun 2018 20:23:05 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI [ci skip] [skip ci] --- app/Models/Component.php | 2 +- app/Models/Incident.php | 2 +- app/Models/Traits/HasTags.php | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/Models/Component.php b/app/Models/Component.php index 2504e99c2de4..1b7901ac7ce8 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -12,9 +12,9 @@ namespace CachetHQ\Cachet\Models; use AltThree\Validator\ValidatingTrait; +use CachetHQ\Cachet\Models\Traits\HasTags; use CachetHQ\Cachet\Models\Traits\SearchableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait; -use CachetHQ\Cachet\Models\Traits\HasTags; use CachetHQ\Cachet\Presenters\ComponentPresenter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; diff --git a/app/Models/Incident.php b/app/Models/Incident.php index 37401ded8e93..740d4c16ab3f 100644 --- a/app/Models/Incident.php +++ b/app/Models/Incident.php @@ -12,9 +12,9 @@ namespace CachetHQ\Cachet\Models; use AltThree\Validator\ValidatingTrait; +use CachetHQ\Cachet\Models\Traits\HasTags; use CachetHQ\Cachet\Models\Traits\SearchableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait; -use CachetHQ\Cachet\Models\Traits\HasTags; use CachetHQ\Cachet\Presenters\IncidentPresenter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; diff --git a/app/Models/Traits/HasTags.php b/app/Models/Traits/HasTags.php index a2fae75f51ab..dbb62b75db63 100644 --- a/app/Models/Traits/HasTags.php +++ b/app/Models/Traits/HasTags.php @@ -13,7 +13,6 @@ use CachetHQ\Cachet\Models\Tag; use Illuminate\Database\Eloquent\Builder; -use InvalidArgumentException; /** * This is the has tags trait. @@ -69,7 +68,7 @@ public function scopeWithAnyTags(Builder $query, $tags) } /** - * 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. * * @param array|\ArrayAccess $values *