Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searchable tags #3113

Merged
merged 3 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
87 changes: 87 additions & 0 deletions app/Models/Traits/HasTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Models\Traits;

use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Builder;

/**
* This is the has tags trait.
*
* @author James Brooks <[email protected]>
*/
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();
});
}
}
32 changes: 0 additions & 32 deletions app/Models/Traits/Taggable.php

This file was deleted.