3 namespace BookStack\Actions;
5 use BookStack\Auth\Permissions\PermissionApplicator;
6 use BookStack\Entities\Models\Entity;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Support\Collection;
9 use Illuminate\Support\Facades\DB;
13 protected PermissionApplicator $permissions;
15 public function __construct(PermissionApplicator $permissions)
17 $this->permissions = $permissions;
21 * Start a query against all tags in the system.
23 public function queryWithTotals(string $searchTerm, string $nameFilter): Builder
28 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
29 DB::raw('COUNT(id) as usages'),
30 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
31 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
32 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
33 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
35 ->orderBy($nameFilter ? 'value' : 'name');
38 $query->where('name', '=', $nameFilter);
39 $query->groupBy('value');
40 } elseif ($searchTerm) {
41 $query->groupBy('name', 'value');
43 $query->groupBy('name');
47 $query->where(function (Builder $query) use ($searchTerm) {
48 $query->where('name', 'like', '%' . $searchTerm . '%')
49 ->orWhere('value', 'like', '%' . $searchTerm . '%');
53 return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
57 * Get tag name suggestions from scanning existing tag names.
58 * If no search term is given the 50 most popular tag names are provided.
60 public function getNameSuggestions(?string $searchTerm): Collection
63 ->select('*', DB::raw('count(*) as count'))
67 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
69 $query = $query->orderBy('count', 'desc')->take(50);
72 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
74 return $query->get(['name'])->pluck('name');
78 * Get tag value suggestions from scanning existing tag values.
79 * If no search is given the 50 most popular values are provided.
80 * Passing a tagName will only find values for a tags with a particular name.
82 public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
85 ->select('*', DB::raw('count(*) as count'))
89 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
91 $query = $query->orderBy('count', 'desc')->take(50);
95 $query = $query->where('name', '=', $tagName);
98 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
100 return $query->get(['value'])->pluck('value');
104 * Save an array of tags to an entity.
106 public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
108 $entity->tags()->delete();
110 $newTags = collect($tags)->filter(function ($tag) {
111 return boolval(trim($tag['name']));
112 })->map(function ($tag) {
113 return $this->newInstanceFromInput($tag);
116 return $entity->tags()->saveMany($newTags);
120 * Create a new Tag instance from user input.
121 * Input must be an array with a 'name' and an optional 'value' key.
123 protected function newInstanceFromInput(array $input): Tag
126 'name' => trim($input['name']),
127 'value' => trim($input['value'] ?? ''),