3 namespace BookStack\Actions;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Entities\Models\Entity;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Support\Collection;
9 use Illuminate\Support\Facades\DB;
14 protected $permissionService;
16 public function __construct(PermissionService $ps)
18 $this->permissionService = $ps;
22 * Start a query against all tags in the system.
24 public function queryWithTotals(string $searchTerm, string $nameFilter): Builder
29 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
30 DB::raw('COUNT(id) as usages'),
31 DB::raw('SUM(IF(entity_type = \'BookStack\\\\Page\', 1, 0)) as page_count'),
32 DB::raw('SUM(IF(entity_type = \'BookStack\\\\Chapter\', 1, 0)) as chapter_count'),
33 DB::raw('SUM(IF(entity_type = \'BookStack\\\\Book\', 1, 0)) as book_count'),
34 DB::raw('SUM(IF(entity_type = \'BookStack\\\\BookShelf\', 1, 0)) as shelf_count'),
36 ->orderBy($nameFilter ? 'value' : 'name');
39 $query->where('name', '=', $nameFilter);
40 $query->groupBy('value');
41 } elseif ($searchTerm) {
42 $query->groupBy('name', 'value');
44 $query->groupBy('name');
48 $query->where(function (Builder $query) use ($searchTerm) {
49 $query->where('name', 'like', '%' . $searchTerm . '%')
50 ->orWhere('value', 'like', '%' . $searchTerm . '%');
54 return $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
58 * Get tag name suggestions from scanning existing tag names.
59 * If no search term is given the 50 most popular tag names are provided.
61 public function getNameSuggestions(?string $searchTerm): Collection
64 ->select('*', DB::raw('count(*) as count'))
68 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
70 $query = $query->orderBy('count', 'desc')->take(50);
73 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
75 return $query->get(['name'])->pluck('name');
79 * Get tag value suggestions from scanning existing tag values.
80 * If no search is given the 50 most popular values are provided.
81 * Passing a tagName will only find values for a tags with a particular name.
83 public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
86 ->select('*', DB::raw('count(*) as count'))
90 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
92 $query = $query->orderBy('count', 'desc')->take(50);
96 $query = $query->where('name', '=', $tagName);
99 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
101 return $query->get(['value'])->pluck('value');
105 * Save an array of tags to an entity.
107 public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
109 $entity->tags()->delete();
111 $newTags = collect($tags)->filter(function ($tag) {
112 return boolval(trim($tag['name']));
113 })->map(function ($tag) {
114 return $this->newInstanceFromInput($tag);
117 return $entity->tags()->saveMany($newTags);
121 * Create a new Tag instance from user input.
122 * Input must be an array with a 'name' and an optional 'value' key.
124 protected function newInstanceFromInput(array $input): Tag
127 'name' => trim($input['name']),
128 'value' => trim($input['value'] ?? ''),