]> BookStack Code Mirror - bookstack/blob - app/Activity/TagRepo.php
Altered ldap_connect usage, cleaned up LDAP classes
[bookstack] / app / Activity / TagRepo.php
1 <?php
2
3 namespace BookStack\Activity;
4
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Util\SimpleListOptions;
9 use Illuminate\Database\Eloquent\Builder;
10 use Illuminate\Support\Collection;
11 use Illuminate\Support\Facades\DB;
12
13 class TagRepo
14 {
15     public function __construct(
16         protected PermissionApplicator $permissions
17     ) {
18     }
19
20     /**
21      * Start a query against all tags in the system.
22      */
23     public function queryWithTotals(SimpleListOptions $listOptions, string $nameFilter): Builder
24     {
25         $searchTerm = $listOptions->getSearch();
26         $sort = $listOptions->getSort();
27         if ($sort === 'name' && $nameFilter) {
28             $sort = 'value';
29         }
30
31         $query = Tag::query()
32             ->select([
33                 'name',
34                 ($searchTerm || $nameFilter) ? 'value' : DB::raw('COUNT(distinct value) as `values`'),
35                 DB::raw('COUNT(id) as usages'),
36                 DB::raw('SUM(IF(entity_type = \'page\', 1, 0)) as page_count'),
37                 DB::raw('SUM(IF(entity_type = \'chapter\', 1, 0)) as chapter_count'),
38                 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
39                 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
40             ])
41             ->orderBy($sort, $listOptions->getOrder());
42
43         if ($nameFilter) {
44             $query->where('name', '=', $nameFilter);
45             $query->groupBy('value');
46         } elseif ($searchTerm) {
47             $query->groupBy('name', 'value');
48         } else {
49             $query->groupBy('name');
50         }
51
52         if ($searchTerm) {
53             $query->where(function (Builder $query) use ($searchTerm) {
54                 $query->where('name', 'like', '%' . $searchTerm . '%')
55                     ->orWhere('value', 'like', '%' . $searchTerm . '%');
56             });
57         }
58
59         return $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
60     }
61
62     /**
63      * Get tag name suggestions from scanning existing tag names.
64      * If no search term is given the 50 most popular tag names are provided.
65      */
66     public function getNameSuggestions(string $searchTerm): Collection
67     {
68         $query = Tag::query()
69             ->select('*', DB::raw('count(*) as count'))
70             ->groupBy('name');
71
72         if ($searchTerm) {
73             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'asc');
74         } else {
75             $query = $query->orderBy('count', 'desc')->take(50);
76         }
77
78         $query = $this->permissions->restrictEntityRelationQuery($query, 'tags', 'entity_id', 'entity_type');
79
80         return $query->pluck('name');
81     }
82
83     /**
84      * Get tag value suggestions from scanning existing tag values.
85      * If no search is given the 50 most popular values are provided.
86      * Passing a tagName will only find values for a tags with a particular name.
87      */
88     public function getValueSuggestions(string $searchTerm, string $tagName): Collection
89     {
90         $query = Tag::query()
91             ->select('*', DB::raw('count(*) as count'))
92             ->where('value', '!=', '')
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 }