]> BookStack Code Mirror - bookstack/blob - app/Repos/TagRepo.php
Fixed role permission removal bug
[bookstack] / app / Repos / TagRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Tag;
4 use BookStack\Entity;
5 use BookStack\Services\PermissionService;
6
7 /**
8  * Class TagRepo
9  * @package BookStack\Repos
10  */
11 class TagRepo
12 {
13
14     protected $tag;
15     protected $entity;
16     protected $permissionService;
17
18     /**
19      * TagRepo constructor.
20      * @param Tag $attr
21      * @param Entity $ent
22      * @param PermissionService $ps
23      */
24     public function __construct(Tag $attr, Entity $ent, PermissionService $ps)
25     {
26         $this->tag = $attr;
27         $this->entity = $ent;
28         $this->permissionService = $ps;
29     }
30
31     /**
32      * Get an entity instance of its particular type.
33      * @param $entityType
34      * @param $entityId
35      * @param string $action
36      */
37     public function getEntity($entityType, $entityId, $action = 'view')
38     {
39         $entityInstance = $this->entity->getEntityInstance($entityType);
40         $searchQuery = $entityInstance->where('id', '=', $entityId)->with('tags');
41         $searchQuery = $this->permissionService->enforceEntityRestrictions($entityType, $searchQuery, $action);
42         return $searchQuery->first();
43     }
44
45     /**
46      * Get all tags for a particular entity.
47      * @param string $entityType
48      * @param int $entityId
49      * @return mixed
50      */
51     public function getForEntity($entityType, $entityId)
52     {
53         $entity = $this->getEntity($entityType, $entityId);
54         if ($entity === null) return collect();
55
56         return $entity->tags;
57     }
58
59     /**
60      * Get tag name suggestions from scanning existing tag names.
61      * If no search term is given the 50 most popular tag names are provided.
62      * @param $searchTerm
63      * @return array
64      */
65     public function getNameSuggestions($searchTerm = false)
66     {
67         $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
68
69         if ($searchTerm) {
70             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
71         } else {
72             $query = $query->orderBy('count', 'desc')->take(50);
73         }
74
75         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
76         return $query->get(['name'])->pluck('name');
77     }
78
79     /**
80      * Get tag value suggestions from scanning existing tag values.
81      * If no search is given the 50 most popular values are provided.
82      * Passing a tagName will only find values for a tags with a particular name.
83      * @param $searchTerm
84      * @param $tagName
85      * @return array
86      */
87     public function getValueSuggestions($searchTerm = false, $tagName = false)
88     {
89         $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
90
91         if ($searchTerm) {
92             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
93         } else {
94             $query = $query->orderBy('count', 'desc')->take(50);
95         }
96
97         if ($tagName !== false) $query = $query->where('name', '=', $tagName);
98
99         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
100         return $query->get(['value'])->pluck('value');
101     }
102
103     /**
104      * Save an array of tags to an entity
105      * @param Entity $entity
106      * @param array $tags
107      * @return array|\Illuminate\Database\Eloquent\Collection
108      */
109     public function saveTagsToEntity(Entity $entity, $tags = [])
110     {
111         $entity->tags()->delete();
112         $newTags = [];
113         foreach ($tags as $tag) {
114             if (trim($tag['name']) === '') continue;
115             $newTags[] = $this->newInstanceFromInput($tag);
116         }
117
118         return $entity->tags()->saveMany($newTags);
119     }
120
121     /**
122      * Create a new Tag instance from user input.
123      * @param $input
124      * @return Tag
125      */
126     protected function newInstanceFromInput($input)
127     {
128         $name = trim($input['name']);
129         $value = isset($input['value']) ? trim($input['value']) : '';
130         // Any other modification or cleanup required can go here
131         $values = ['name' => $name, 'value' => $value];
132         return $this->tag->newInstance($values);
133     }
134
135 }