]> BookStack Code Mirror - bookstack/blob - app/Actions/TagRepo.php
Updated additional relation queries to apply permissions correctly
[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         $entityTypeCol = DB::getTablePrefix() . 'tags.entity_type';
33         $query = Tag::query()
34             ->select([
35                 'name',
36                 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
37                 DB::raw('COUNT(id) as usages'),
38                 DB::raw("SUM(IF({$entityTypeCol} = 'page', 1, 0)) as page_count"),
39                 DB::raw("SUM(IF({$entityTypeCol} = 'chapter', 1, 0)) as chapter_count"),
40                 DB::raw("SUM(IF({$entityTypeCol} = 'book', 1, 0)) as book_count"),
41                 DB::raw("SUM(IF({$entityTypeCol} = 'bookshelf', 1, 0)) as shelf_count"),
42             ])
43             ->orderBy($sort, $listOptions->getOrder());
44
45         if ($nameFilter) {
46             $query->where('name', '=', $nameFilter);
47             $query->groupBy('value');
48         } elseif ($searchTerm) {
49             $query->groupBy('name', 'value');
50         } else {
51             $query->groupBy('name');
52         }
53
54         if ($searchTerm) {
55             $query->where(function (Builder $query) use ($searchTerm) {
56                 $query->where('name', 'like', '%' . $searchTerm . '%')
57                     ->orWhere('value', 'like', '%' . $searchTerm . '%');
58             });
59         }
60
61         return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
62     }
63
64     /**
65      * Get tag name suggestions from scanning existing tag names.
66      * If no search term is given the 50 most popular tag names are provided.
67      */
68     public function getNameSuggestions(string $searchTerm): Collection
69     {
70         $query = Tag::query()
71             ->select('*', DB::raw('count(*) as count'))
72             ->groupBy('name');
73
74         if ($searchTerm) {
75             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'asc');
76         } else {
77             $query = $query->orderBy('count', 'desc')->take(50);
78         }
79
80         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
81
82         return $query->pluck('name');
83     }
84
85     /**
86      * Get tag value suggestions from scanning existing tag values.
87      * If no search is given the 50 most popular values are provided.
88      * Passing a tagName will only find values for a tags with a particular name.
89      */
90     public function getValueSuggestions(string $searchTerm, string $tagName): Collection
91     {
92         $query = Tag::query()
93             ->select('*', DB::raw('count(*) as count'))
94             ->groupBy('value');
95
96         if ($searchTerm) {
97             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
98         } else {
99             $query = $query->orderBy('count', 'desc')->take(50);
100         }
101
102         if ($tagName) {
103             $query = $query->where('name', '=', $tagName);
104         }
105
106         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
107
108         return $query->pluck('value');
109     }
110
111     /**
112      * Save an array of tags to an entity.
113      */
114     public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
115     {
116         $entity->tags()->delete();
117
118         $newTags = collect($tags)->filter(function ($tag) {
119             return boolval(trim($tag['name']));
120         })->map(function ($tag) {
121             return $this->newInstanceFromInput($tag);
122         })->all();
123
124         return $entity->tags()->saveMany($newTags);
125     }
126
127     /**
128      * Create a new Tag instance from user input.
129      * Input must be an array with a 'name' and an optional 'value' key.
130      */
131     protected function newInstanceFromInput(array $input): Tag
132     {
133         return new Tag([
134             'name'  => trim($input['name']),
135             'value' => trim($input['value'] ?? ''),
136         ]);
137     }
138 }