]> BookStack Code Mirror - bookstack/blob - app/Actions/TagRepo.php
fix image delete confirm text
[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->select('*', DB::raw('count(*) as count'))->groupBy('name');
30
31         if ($searchTerm) {
32             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
33         } else {
34             $query = $query->orderBy('count', 'desc')->take(50);
35         }
36
37         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
38         return $query->get(['name'])->pluck('name');
39     }
40
41     /**
42      * Get tag value suggestions from scanning existing tag values.
43      * If no search is given the 50 most popular values are provided.
44      * Passing a tagName will only find values for a tags with a particular name.
45      */
46     public function getValueSuggestions(?string $searchTerm, ?string $tagName): Collection
47     {
48         $query = $this->tag->select('*', DB::raw('count(*) as count'))->groupBy('value');
49
50         if ($searchTerm) {
51             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
52         } else {
53             $query = $query->orderBy('count', 'desc')->take(50);
54         }
55
56         if ($tagName) {
57             $query = $query->where('name', '=', $tagName);
58         }
59
60         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
61         return $query->get(['value'])->pluck('value');
62     }
63
64     /**
65      * Save an array of tags to an entity
66      */
67     public function saveTagsToEntity(Entity $entity, array $tags = []): iterable
68     {
69         $entity->tags()->delete();
70
71         $newTags = collect($tags)->filter(function ($tag) {
72             return boolval(trim($tag['name']));
73         })->map(function ($tag) {
74             return $this->newInstanceFromInput($tag);
75         })->all();
76
77         return $entity->tags()->saveMany($newTags);
78     }
79
80     /**
81      * Create a new Tag instance from user input.
82      * Input must be an array with a 'name' and an optional 'value' key.
83      */
84     protected function newInstanceFromInput(array $input): Tag
85     {
86         $name = trim($input['name']);
87         $value = isset($input['value']) ? trim($input['value']) : '';
88         return $this->tag->newInstance(['name' => $name, 'value' => $value]);
89     }
90 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.