2018-09-25 13:30:50 +02:00
|
|
|
<?php namespace BookStack\Actions;
|
2016-05-06 21:33:08 +02:00
|
|
|
|
2018-09-25 13:30:50 +02:00
|
|
|
use BookStack\Auth\Permissions\PermissionService;
|
2020-11-22 01:17:45 +01:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2020-06-29 23:11:03 +02:00
|
|
|
use DB;
|
|
|
|
use Illuminate\Support\Collection;
|
2016-05-06 21:33:08 +02:00
|
|
|
|
2016-05-13 22:20:21 +02:00
|
|
|
class TagRepo
|
2016-05-06 21:33:08 +02:00
|
|
|
{
|
|
|
|
|
2016-05-13 22:20:21 +02:00
|
|
|
protected $tag;
|
2016-05-06 21:33:08 +02:00
|
|
|
protected $permissionService;
|
|
|
|
|
|
|
|
/**
|
2016-05-13 22:20:21 +02:00
|
|
|
* TagRepo constructor.
|
2016-05-06 21:33:08 +02:00
|
|
|
*/
|
2020-06-29 23:11:03 +02:00
|
|
|
public function __construct(Tag $tag, PermissionService $ps)
|
2016-05-06 21:33:08 +02:00
|
|
|
{
|
2020-06-29 23:11:03 +02:00
|
|
|
$this->tag = $tag;
|
2016-05-06 21:33:08 +02:00
|
|
|
$this->permissionService = $ps;
|
|
|
|
}
|
|
|
|
|
2016-05-07 15:29:43 +02:00
|
|
|
/**
|
2016-05-13 22:20:21 +02:00
|
|
|
* Get tag name suggestions from scanning existing tag names.
|
2016-06-04 16:37:28 +02:00
|
|
|
* If no search term is given the 50 most popular tag names are provided.
|
2016-05-07 15:29:43 +02:00
|
|
|
*/
|
2020-06-29 23:11:03 +02:00
|
|
|
public function getNameSuggestions(?string $searchTerm): Collection
|
2016-05-07 15:29:43 +02:00
|
|
|
{
|
2021-03-14 20:52:07 +01:00
|
|
|
$query = $this->tag->newQuery()
|
|
|
|
->select('*', DB::raw('count(*) as count'))
|
|
|
|
->groupBy('name');
|
2016-06-04 16:37:28 +02:00
|
|
|
|
|
|
|
if ($searchTerm) {
|
|
|
|
$query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
|
|
|
|
} else {
|
|
|
|
$query = $query->orderBy('count', 'desc')->take(50);
|
|
|
|
}
|
|
|
|
|
2016-05-13 22:20:21 +02:00
|
|
|
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
|
2016-05-07 15:29:43 +02:00
|
|
|
return $query->get(['name'])->pluck('name');
|
|
|
|
}
|
|
|
|
|
2016-05-15 21:12:53 +02:00
|
|
|
/**
|
|
|
|
* Get tag value suggestions from scanning existing tag values.
|
2016-06-04 16:37:28 +02:00
|
|
|
* If no search is given the 50 most popular values are provided.
|
|
|
|
* Passing a tagName will only find values for a tags with a particular name.
|
2016-05-15 21:12:53 +02:00
|
|
|
*/
|
2020-06-29 23:11:03 +02:00
|
|
|
public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
|
2016-05-15 21:12:53 +02:00
|
|
|
{
|
2021-03-14 20:52:07 +01:00
|
|
|
$query = $this->tag->newQuery()
|
|
|
|
->select('*', DB::raw('count(*) as count'))
|
|
|
|
->groupBy('value');
|
2016-06-04 16:37:28 +02:00
|
|
|
|
|
|
|
if ($searchTerm) {
|
|
|
|
$query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
|
|
|
|
} else {
|
|
|
|
$query = $query->orderBy('count', 'desc')->take(50);
|
2016-06-04 15:54:31 +02:00
|
|
|
}
|
2016-06-04 16:37:28 +02:00
|
|
|
|
2020-06-29 23:11:03 +02:00
|
|
|
if ($tagName) {
|
2018-01-28 17:58:52 +01:00
|
|
|
$query = $query->where('name', '=', $tagName);
|
|
|
|
}
|
2016-06-04 16:37:28 +02:00
|
|
|
|
2016-05-15 21:12:53 +02:00
|
|
|
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
|
|
|
|
return $query->get(['value'])->pluck('value');
|
|
|
|
}
|
2016-06-04 16:37:28 +02:00
|
|
|
|
2016-05-07 15:29:43 +02:00
|
|
|
/**
|
2016-05-13 22:20:21 +02:00
|
|
|
* Save an array of tags to an entity
|
2016-05-07 15:29:43 +02:00
|
|
|
*/
|
2020-06-29 23:11:03 +02:00
|
|
|
public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
|
2016-05-07 15:29:43 +02:00
|
|
|
{
|
2016-05-13 22:20:21 +02:00
|
|
|
$entity->tags()->delete();
|
2020-05-23 01:28:41 +02:00
|
|
|
|
2020-06-29 23:11:03 +02:00
|
|
|
$newTags = collect($tags)->filter(function ($tag) {
|
|
|
|
return boolval(trim($tag['name']));
|
|
|
|
})->map(function ($tag) {
|
|
|
|
return $this->newInstanceFromInput($tag);
|
|
|
|
})->all();
|
2016-05-07 15:29:43 +02:00
|
|
|
|
2016-05-13 22:20:21 +02:00
|
|
|
return $entity->tags()->saveMany($newTags);
|
2016-05-07 15:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-13 22:20:21 +02:00
|
|
|
* Create a new Tag instance from user input.
|
2020-06-29 23:11:03 +02:00
|
|
|
* Input must be an array with a 'name' and an optional 'value' key.
|
2016-05-07 15:29:43 +02:00
|
|
|
*/
|
2020-06-29 23:11:03 +02:00
|
|
|
protected function newInstanceFromInput(array $input): Tag
|
2016-05-07 15:29:43 +02:00
|
|
|
{
|
|
|
|
$name = trim($input['name']);
|
|
|
|
$value = isset($input['value']) ? trim($input['value']) : '';
|
2020-06-29 23:11:03 +02:00
|
|
|
return $this->tag->newInstance(['name' => $name, 'value' => $value]);
|
2016-05-07 15:29:43 +02:00
|
|
|
}
|
2018-01-28 17:58:52 +01:00
|
|
|
}
|