]> BookStack Code Mirror - bookstack/blob - app/Repos/TagRepo.php
PSR2 fixes after running `./vendor/bin/phpcbf`
[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      * @return \Illuminate\Database\Eloquent\Model|null|static
37      */
38     public function getEntity($entityType, $entityId, $action = 'view')
39     {
40         $entityInstance = $this->entity->getEntityInstance($entityType);
41         $searchQuery = $entityInstance->where('id', '=', $entityId)->with('tags');
42         $searchQuery = $this->permissionService->enforceEntityRestrictions($entityType, $searchQuery, $action);
43         return $searchQuery->first();
44     }
45
46     /**
47      * Get all tags for a particular entity.
48      * @param string $entityType
49      * @param int $entityId
50      * @return mixed
51      */
52     public function getForEntity($entityType, $entityId)
53     {
54         $entity = $this->getEntity($entityType, $entityId);
55         if ($entity === null) {
56             return collect();
57         }
58
59         return $entity->tags;
60     }
61
62     /**
63      * Get tag name suggestions from scanning existing tag names.
64      * If no search term is given the 50 most popular tag names are provided.
65      * @param $searchTerm
66      * @return array
67      */
68     public function getNameSuggestions($searchTerm = false)
69     {
70         $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
71
72         if ($searchTerm) {
73             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
74         } else {
75             $query = $query->orderBy('count', 'desc')->take(50);
76         }
77
78         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
79         return $query->get(['name'])->pluck('name');
80     }
81
82     /**
83      * Get tag value suggestions from scanning existing tag values.
84      * If no search is given the 50 most popular values are provided.
85      * Passing a tagName will only find values for a tags with a particular name.
86      * @param $searchTerm
87      * @param $tagName
88      * @return array
89      */
90     public function getValueSuggestions($searchTerm = false, $tagName = false)
91     {
92         $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
93
94         if ($searchTerm) {
95             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
96         } else {
97             $query = $query->orderBy('count', 'desc')->take(50);
98         }
99
100         if ($tagName !== false) {
101             $query = $query->where('name', '=', $tagName);
102         }
103
104         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
105         return $query->get(['value'])->pluck('value');
106     }
107
108     /**
109      * Save an array of tags to an entity
110      * @param Entity $entity
111      * @param array $tags
112      * @return array|\Illuminate\Database\Eloquent\Collection
113      */
114     public function saveTagsToEntity(Entity $entity, $tags = [])
115     {
116         $entity->tags()->delete();
117         $newTags = [];
118         foreach ($tags as $tag) {
119             if (trim($tag['name']) === '') {
120                 continue;
121             }
122             $newTags[] = $this->newInstanceFromInput($tag);
123         }
124
125         return $entity->tags()->saveMany($newTags);
126     }
127
128     /**
129      * Create a new Tag instance from user input.
130      * @param $input
131      * @return Tag
132      */
133     protected function newInstanceFromInput($input)
134     {
135         $name = trim($input['name']);
136         $value = isset($input['value']) ? trim($input['value']) : '';
137         // Any other modification or cleanup required can go here
138         $values = ['name' => $name, 'value' => $value];
139         return $this->tag->newInstance($values);
140     }
141 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.