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
26 $groupingAttribute = $nameFilter ? 'value' : 'name';
30 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
31 DB::raw('COUNT(id) as usages'),
32 DB::raw('SUM(IF(entity_type = \'BookStack\\\\Page\', 1, 0)) as page_count'),
33 DB::raw('SUM(IF(entity_type = \'BookStack\\\\Chapter\', 1, 0)) as chapter_count'),
34 DB::raw('SUM(IF(entity_type = \'BookStack\\\\Book\', 1, 0)) as book_count'),
35 DB::raw('SUM(IF(entity_type = \'BookStack\\\\BookShelf\', 1, 0)) as shelf_count'),
37 ->groupBy($groupingAttribute)
38 ->orderBy($groupingAttribute);
41 $query->where('name', '=', $nameFilter);
45 $query->where(function (Builder $query) use ($searchTerm) {
46 $query->where('name', 'like', '%' . $searchTerm . '%')
47 ->orWhere('value', 'like', '%' . $searchTerm . '%');
51 return $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
55 * Get tag name suggestions from scanning existing tag names.
56 * If no search term is given the 50 most popular tag names are provided.
58 public function getNameSuggestions(?string $searchTerm): Collection
61 ->select('*', DB::raw('count(*) as count'))
65 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
67 $query = $query->orderBy('count', 'desc')->take(50);
70 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
72 return $query->get(['name'])->pluck('name');
76 * Get tag value suggestions from scanning existing tag values.
77 * If no search is given the 50 most popular values are provided.
78 * Passing a tagName will only find values for a tags with a particular name.
80 public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
83 ->select('*', DB::raw('count(*) as count'))
87 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
89 $query = $query->orderBy('count', 'desc')->take(50);
93 $query = $query->where('name', '=', $tagName);
96 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
98 return $query->get(['value'])->pluck('value');
102 * Save an array of tags to an entity.
104 public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
106 $entity->tags()->delete();
108 $newTags = collect($tags)->filter(function ($tag) {
109 return boolval(trim($tag['name']));
110 })->map(function ($tag) {
111 return $this->newInstanceFromInput($tag);
114 return $entity->tags()->saveMany($newTags);
118 * Create a new Tag instance from user input.
119 * Input must be an array with a 'name' and an optional 'value' key.
121 protected function newInstanceFromInput(array $input): Tag
124 'name' => trim($input['name']),
125 'value' => trim($input['value'] ?? ''),