]> BookStack Code Mirror - bookstack/blob - app/Actions/TagRepo.php
Fixed occurances of altered titles in search results
[bookstack] / app / Actions / TagRepo.php
1 <?php
2
3 namespace BookStack\Actions;
4
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;
10
11 class TagRepo
12 {
13     protected $tag;
14     protected $permissionService;
15
16     public function __construct(PermissionService $ps)
17     {
18         $this->permissionService = $ps;
19     }
20
21     /**
22      * Start a query against all tags in the system.
23      */
24     public function queryWithTotals(string $searchTerm, string $nameFilter): Builder
25     {
26         $groupingAttribute = $nameFilter ? 'value' : 'name';
27         $query = Tag::query()
28             ->select([
29                 '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'),
36             ])
37             ->groupBy($groupingAttribute)
38             ->orderBy($groupingAttribute);
39
40         if ($nameFilter) {
41             $query->where('name', '=', $nameFilter);
42         }
43
44         if ($searchTerm) {
45             $query->where(function (Builder $query) use ($searchTerm) {
46                 $query->where('name', 'like', '%' . $searchTerm . '%')
47                     ->orWhere('value', 'like', '%' . $searchTerm . '%');
48             });
49         }
50
51         return $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
52     }
53
54     /**
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.
57      */
58     public function getNameSuggestions(?string $searchTerm): Collection
59     {
60         $query = Tag::query()
61             ->select('*', DB::raw('count(*) as count'))
62             ->groupBy('name');
63
64         if ($searchTerm) {
65             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
66         } else {
67             $query = $query->orderBy('count', 'desc')->take(50);
68         }
69
70         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
71
72         return $query->get(['name'])->pluck('name');
73     }
74
75     /**
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.
79      */
80     public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
81     {
82         $query = Tag::query()
83             ->select('*', DB::raw('count(*) as count'))
84             ->groupBy('value');
85
86         if ($searchTerm) {
87             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
88         } else {
89             $query = $query->orderBy('count', 'desc')->take(50);
90         }
91
92         if ($tagName) {
93             $query = $query->where('name', '=', $tagName);
94         }
95
96         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
97
98         return $query->get(['value'])->pluck('value');
99     }
100
101     /**
102      * Save an array of tags to an entity.
103      */
104     public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
105     {
106         $entity->tags()->delete();
107
108         $newTags = collect($tags)->filter(function ($tag) {
109             return boolval(trim($tag['name']));
110         })->map(function ($tag) {
111             return $this->newInstanceFromInput($tag);
112         })->all();
113
114         return $entity->tags()->saveMany($newTags);
115     }
116
117     /**
118      * Create a new Tag instance from user input.
119      * Input must be an array with a 'name' and an optional 'value' key.
120      */
121     protected function newInstanceFromInput(array $input): Tag
122     {
123         return new Tag([
124             'name'  => trim($input['name']),
125             'value' => trim($input['value'] ?? ''),
126         ]);
127     }
128 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.