]> BookStack Code Mirror - bookstack/blob - app/Actions/TagRepo.php
Merge pull request #3556 from GongMingCai/development
[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 Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Support\Collection;
9 use Illuminate\Support\Facades\DB;
10
11 class TagRepo
12 {
13     protected PermissionApplicator $permissions;
14
15     public function __construct(PermissionApplicator $permissions)
16     {
17         $this->permissions = $permissions;
18     }
19
20     /**
21      * Start a query against all tags in the system.
22      */
23     public function queryWithTotals(string $searchTerm, string $nameFilter): Builder
24     {
25         $query = Tag::query()
26             ->select([
27                 'name',
28                 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
29                 DB::raw('COUNT(id) as usages'),
30                 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
31                 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
32                 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
33                 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
34             ])
35             ->orderBy($nameFilter ? 'value' : 'name');
36
37         if ($nameFilter) {
38             $query->where('name', '=', $nameFilter);
39             $query->groupBy('value');
40         } elseif ($searchTerm) {
41             $query->groupBy('name', 'value');
42         } else {
43             $query->groupBy('name');
44         }
45
46         if ($searchTerm) {
47             $query->where(function (Builder $query) use ($searchTerm) {
48                 $query->where('name', 'like', '%' . $searchTerm . '%')
49                     ->orWhere('value', 'like', '%' . $searchTerm . '%');
50             });
51         }
52
53         return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
54     }
55
56     /**
57      * Get tag name suggestions from scanning existing tag names.
58      * If no search term is given the 50 most popular tag names are provided.
59      */
60     public function getNameSuggestions(?string $searchTerm): Collection
61     {
62         $query = Tag::query()
63             ->select('*', DB::raw('count(*) as count'))
64             ->groupBy('name');
65
66         if ($searchTerm) {
67             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
68         } else {
69             $query = $query->orderBy('count', 'desc')->take(50);
70         }
71
72         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
73
74         return $query->get(['name'])->pluck('name');
75     }
76
77     /**
78      * Get tag value suggestions from scanning existing tag values.
79      * If no search is given the 50 most popular values are provided.
80      * Passing a tagName will only find values for a tags with a particular name.
81      */
82     public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
83     {
84         $query = Tag::query()
85             ->select('*', DB::raw('count(*) as count'))
86             ->groupBy('value');
87
88         if ($searchTerm) {
89             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
90         } else {
91             $query = $query->orderBy('count', 'desc')->take(50);
92         }
93
94         if ($tagName) {
95             $query = $query->where('name', '=', $tagName);
96         }
97
98         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
99
100         return $query->get(['value'])->pluck('value');
101     }
102
103     /**
104      * Save an array of tags to an entity.
105      */
106     public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
107     {
108         $entity->tags()->delete();
109
110         $newTags = collect($tags)->filter(function ($tag) {
111             return boolval(trim($tag['name']));
112         })->map(function ($tag) {
113             return $this->newInstanceFromInput($tag);
114         })->all();
115
116         return $entity->tags()->saveMany($newTags);
117     }
118
119     /**
120      * Create a new Tag instance from user input.
121      * Input must be an array with a 'name' and an optional 'value' key.
122      */
123     protected function newInstanceFromInput(array $input): Tag
124     {
125         return new Tag([
126             'name'  => trim($input['name']),
127             'value' => trim($input['value'] ?? ''),
128         ]);
129     }
130 }