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..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\Taggable; 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..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\Taggable; 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/HasTags.php b/app/Models/Traits/HasTags.php new file mode 100644 index 000000000000..dbb62b75db63 --- /dev/null +++ b/app/Models/Traits/HasTags.php @@ -0,0 +1,87 @@ + + */ +trait HasTags +{ + /** + * Get the tags relation. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany + */ + public function tags() + { + 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); + + $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(); + + $query->whereIn('taggables.tag_id', $tagIds); + }); + } + + /** + * 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(); + }); + } +} diff --git a/app/Models/Traits/Taggable.php b/app/Models/Traits/Taggable.php deleted file mode 100644 index f7c07a06bb6a..000000000000 --- a/app/Models/Traits/Taggable.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ -trait Taggable -{ - /** - * Get the tags relation. - * - * @return \Illuminate\Database\Eloquent\Relations\MorphMany - */ - public function tags() - { - return $this->morphMany(TaggableModel::class, 'taggable'); - } -}