3 namespace BookStack\Activity;
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Util\SimpleListOptions;
9 use Illuminate\Database\Eloquent\Builder;
10 use Illuminate\Support\Collection;
11 use Illuminate\Support\Facades\DB;
15 public function __construct(
16 protected PermissionApplicator $permissions
21 * Start a query against all tags in the system.
23 public function queryWithTotals(SimpleListOptions $listOptions, string $nameFilter): Builder
25 $searchTerm = $listOptions->getSearch();
26 $sort = $listOptions->getSort();
27 if ($sort === 'name' && $nameFilter) {
34 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
35 DB::raw('COUNT(id) as usages'),
36 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
37 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
38 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
39 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
41 ->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'))
93 ->where('value', '!=', '')
97 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
99 $query = $query->orderBy('count', 'desc')->take(50);
103 $query = $query->where('name', '=', $tagName);
106 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
108 return $query->pluck('value');
112 * Save an array of tags to an entity.
114 public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
116 $entity->tags()->delete();
118 $newTags = collect($tags)->filter(function ($tag) {
119 return boolval(trim($tag['name']));
120 })->map(function ($tag) {
121 return $this->newInstanceFromInput($tag);
124 return $entity->tags()->saveMany($newTags);
128 * Create a new Tag instance from user input.
129 * Input must be an array with a 'name' and an optional 'value' key.
131 protected function newInstanceFromInput(array $input): Tag
134 'name' => trim($input['name']),
135 'value' => trim($input['value'] ?? ''),