]> BookStack Code Mirror - bookstack/blob - app/Actions/TagRepo.php
Allow to use DB tables prefix
[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 DB;
8 use Illuminate\Support\Collection;
9
10 class TagRepo
11 {
12     protected $tag;
13     protected $permissionService;
14
15     /**
16      * TagRepo constructor.
17      */
18     public function __construct(Tag $tag, PermissionService $ps)
19     {
20         $this->tag = $tag;
21         $this->permissionService = $ps;
22     }
23
24     /**
25      * Get tag name suggestions from scanning existing tag names.
26      * If no search term is given the 50 most popular tag names are provided.
27      */
28     public function getNameSuggestions(?string $searchTerm): Collection
29     {
30         $query = $this->tag->newQuery()
31             ->select('*', DB::raw('count(*) as count'))
32             ->groupBy('name');
33
34         if ($searchTerm) {
35             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
36         } else {
37             $query = $query->orderBy('count', 'desc')->take(50);
38         }
39
40         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
41
42         return $query->get(['name'])->pluck('name');
43     }
44
45     /**
46      * Get tag value suggestions from scanning existing tag values.
47      * If no search is given the 50 most popular values are provided.
48      * Passing a tagName will only find values for a tags with a particular name.
49      */
50     public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
51     {
52         $query = $this->tag->newQuery()
53             ->select('*', DB::raw('count(*) as count'))
54             ->groupBy('value');
55
56         if ($searchTerm) {
57             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
58         } else {
59             $query = $query->orderBy('count', 'desc')->take(50);
60         }
61
62         if ($tagName) {
63             $query = $query->where('name', '=', $tagName);
64         }
65
66         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
67
68         return $query->get(['value'])->pluck('value');
69     }
70
71     /**
72      * Save an array of tags to an entity.
73      */
74     public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
75     {
76         $entity->tags()->delete();
77
78         $newTags = collect($tags)->filter(function ($tag) {
79             return boolval(trim($tag['name']));
80         })->map(function ($tag) {
81             return $this->newInstanceFromInput($tag);
82         })->all();
83
84         return $entity->tags()->saveMany($newTags);
85     }
86
87     /**
88      * Create a new Tag instance from user input.
89      * Input must be an array with a 'name' and an optional 'value' key.
90      */
91     protected function newInstanceFromInput(array $input): Tag
92     {
93         $name = trim($input['name']);
94         $value = isset($input['value']) ? trim($input['value']) : '';
95
96         return $this->tag->newInstance(['name' => $name, 'value' => $value]);
97     }
98 }