]> BookStack Code Mirror - bookstack/blob - app/Actions/TagRepo.php
Updated tags list to new responsive format
[bookstack] / app / Actions / TagRepo.php
1 <?php
2
3 namespace BookStack\Actions;
4
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;
11
12 class TagRepo
13 {
14     protected PermissionApplicator $permissions;
15
16     public function __construct(PermissionApplicator $permissions)
17     {
18         $this->permissions = $permissions;
19     }
20
21     /**
22      * Start a query against all tags in the system.
23      */
24     public function queryWithTotals(SimpleListOptions $listOptions, string $nameFilter): Builder
25     {
26         $searchTerm = $listOptions->getSearch();
27         $sort = $listOptions->getSort();
28         if ($sort === 'name' && $nameFilter) {
29             $sort = 'value';
30         }
31
32         $query = Tag::query()
33             ->select([
34                 'name',
35                 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
36                 DB::raw('COUNT(id) as usages'),
37                 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
38                 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
39                 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
40                 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
41             ])
42             ->orderBy($sort, $listOptions->getOrder());
43
44         if ($nameFilter) {
45             $query->where('name', '=', $nameFilter);
46             $query->groupBy('value');
47         } elseif ($searchTerm) {
48             $query->groupBy('name', 'value');
49         } else {
50             $query->groupBy('name');
51         }
52
53         if ($searchTerm) {
54             $query->where(function (Builder $query) use ($searchTerm) {
55                 $query->where('name', 'like', '%' . $searchTerm . '%')
56                     ->orWhere('value', 'like', '%' . $searchTerm . '%');
57             });
58         }
59
60         return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
61     }
62
63     /**
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.
66      */
67     public function getNameSuggestions(string $searchTerm): Collection
68     {
69         $query = Tag::query()
70             ->select('*', DB::raw('count(*) as count'))
71             ->groupBy('name');
72
73         if ($searchTerm) {
74             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'asc');
75         } else {
76             $query = $query->orderBy('count', 'desc')->take(50);
77         }
78
79         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
80
81         return $query->pluck('name');
82     }
83
84     /**
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.
88      */
89     public function getValueSuggestions(string $searchTerm, string $tagName): Collection
90     {
91         $query = Tag::query()
92             ->select('*', DB::raw('count(*) as count'))
93             ->groupBy('value');
94
95         if ($searchTerm) {
96             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
97         } else {
98             $query = $query->orderBy('count', 'desc')->take(50);
99         }
100
101         if ($tagName) {
102             $query = $query->where('name', '=', $tagName);
103         }
104
105         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
106
107         return $query->pluck('value');
108     }
109
110     /**
111      * Save an array of tags to an entity.
112      */
113     public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
114     {
115         $entity->tags()->delete();
116
117         $newTags = collect($tags)->filter(function ($tag) {
118             return boolval(trim($tag['name']));
119         })->map(function ($tag) {
120             return $this->newInstanceFromInput($tag);
121         })->all();
122
123         return $entity->tags()->saveMany($newTags);
124     }
125
126     /**
127      * Create a new Tag instance from user input.
128      * Input must be an array with a 'name' and an optional 'value' key.
129      */
130     protected function newInstanceFromInput(array $input): Tag
131     {
132         return new Tag([
133             'name'  => trim($input['name']),
134             'value' => trim($input['value'] ?? ''),
135         ]);
136     }
137 }