3 namespace BookStack\Actions;
5 use BookStack\Auth\Permissions\PermissionApplicator;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Util\SimpleListOptions;
8 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Support\Collection;
10 use Illuminate\Support\Facades\DB;
14 protected PermissionApplicator $permissions;
16 public function __construct(PermissionApplicator $permissions)
18 $this->permissions = $permissions;
22 * Start a query against all tags in the system.
24 public function queryWithTotals(SimpleListOptions $listOptions, string $nameFilter): Builder
26 $searchTerm = $listOptions->getSearch();
27 $sort = $listOptions->getSort();
28 if ($sort === 'name' && $nameFilter) {
35 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
36 DB::raw('COUNT(id) as usages'),
37 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
38 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
39 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
40 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
42 ->orderBy($sort, $listOptions->getOrder());
45 $query->where('name', '=', $nameFilter);
46 $query->groupBy('value');
47 } elseif ($searchTerm) {
48 $query->groupBy('name', 'value');
50 $query->groupBy('name');
54 $query->where(function (Builder $query) use ($searchTerm) {
55 $query->where('name', 'like', '%' . $searchTerm . '%')
56 ->orWhere('value', 'like', '%' . $searchTerm . '%');
60 return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
64 * Get tag name suggestions from scanning existing tag names.
65 * If no search term is given the 50 most popular tag names are provided.
67 public function getNameSuggestions(string $searchTerm): Collection
70 ->select('*', DB::raw('count(*) as count'))
74 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'asc');
76 $query = $query->orderBy('count', 'desc')->take(50);
79 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
81 return $query->pluck('name');
85 * Get tag value suggestions from scanning existing tag values.
86 * If no search is given the 50 most popular values are provided.
87 * Passing a tagName will only find values for a tags with a particular name.
89 public function getValueSuggestions(string $searchTerm, string $tagName): Collection
92 ->select('*', DB::raw('count(*) as count'))
96 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
98 $query = $query->orderBy('count', 'desc')->take(50);
102 $query = $query->where('name', '=', $tagName);
105 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
107 return $query->pluck('value');
111 * Save an array of tags to an entity.
113 public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
115 $entity->tags()->delete();
117 $newTags = collect($tags)->filter(function ($tag) {
118 return boolval(trim($tag['name']));
119 })->map(function ($tag) {
120 return $this->newInstanceFromInput($tag);
123 return $entity->tags()->saveMany($newTags);
127 * Create a new Tag instance from user input.
128 * Input must be an array with a 'name' and an optional 'value' key.
130 protected function newInstanceFromInput(array $input): Tag
133 'name' => trim($input['name']),
134 'value' => trim($input['value'] ?? ''),