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