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