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 public function __construct(
15 protected PermissionApplicator $permissions
20 * Start a query against all tags in the system.
22 public function queryWithTotals(SimpleListOptions $listOptions, string $nameFilter): Builder
24 $searchTerm = $listOptions->getSearch();
25 $sort = $listOptions->getSort();
26 if ($sort === 'name' && $nameFilter) {
33 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
34 DB::raw('COUNT(id) as usages'),
35 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
36 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
37 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
38 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
40 ->orderBy($sort, $listOptions->getOrder());
43 $query->where('name', '=', $nameFilter);
44 $query->groupBy('value');
45 } elseif ($searchTerm) {
46 $query->groupBy('name', 'value');
48 $query->groupBy('name');
52 $query->where(function (Builder $query) use ($searchTerm) {
53 $query->where('name', 'like', '%' . $searchTerm . '%')
54 ->orWhere('value', 'like', '%' . $searchTerm . '%');
58 return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
62 * Get tag name suggestions from scanning existing tag names.
63 * If no search term is given the 50 most popular tag names are provided.
65 public function getNameSuggestions(string $searchTerm): Collection
68 ->select('*', DB::raw('count(*) as count'))
72 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'asc');
74 $query = $query->orderBy('count', 'desc')->take(50);
77 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
79 return $query->pluck('name');
83 * Get tag value suggestions from scanning existing tag values.
84 * If no search is given the 50 most popular values are provided.
85 * Passing a tagName will only find values for a tags with a particular name.
87 public function getValueSuggestions(string $searchTerm, string $tagName): Collection
90 ->select('*', DB::raw('count(*) as count'))
91 ->where('value', '!=', '')
95 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
97 $query = $query->orderBy('count', 'desc')->take(50);
101 $query = $query->where('name', '=', $tagName);
104 $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
106 return $query->pluck('value');
110 * Save an array of tags to an entity.
112 public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
114 $entity->tags()->delete();
116 $newTags = collect($tags)->filter(function ($tag) {
117 return boolval(trim($tag['name']));
118 })->map(function ($tag) {
119 return $this->newInstanceFromInput($tag);
122 return $entity->tags()->saveMany($newTags);
126 * Create a new Tag instance from user input.
127 * Input must be an array with a 'name' and an optional 'value' key.
129 protected function newInstanceFromInput(array $input): Tag
132 'name' => trim($input['name']),
133 'value' => trim($input['value'] ?? ''),