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