]> BookStack Code Mirror - bookstack/blob - app/Repos/TagRepo.php
Refactored image picker to js component
[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) return collect();
56
57         return $entity->tags;
58     }
59
60     /**
61      * Get tag name suggestions from scanning existing tag names.
62      * If no search term is given the 50 most popular tag names are provided.
63      * @param $searchTerm
64      * @return array
65      */
66     public function getNameSuggestions($searchTerm = false)
67     {
68         $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
69
70         if ($searchTerm) {
71             $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
72         } else {
73             $query = $query->orderBy('count', 'desc')->take(50);
74         }
75
76         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
77         return $query->get(['name'])->pluck('name');
78     }
79
80     /**
81      * Get tag value suggestions from scanning existing tag values.
82      * If no search is given the 50 most popular values are provided.
83      * Passing a tagName will only find values for a tags with a particular name.
84      * @param $searchTerm
85      * @param $tagName
86      * @return array
87      */
88     public function getValueSuggestions($searchTerm = false, $tagName = false)
89     {
90         $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
91
92         if ($searchTerm) {
93             $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
94         } else {
95             $query = $query->orderBy('count', 'desc')->take(50);
96         }
97
98         if ($tagName !== false) $query = $query->where('name', '=', $tagName);
99
100         $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
101         return $query->get(['value'])->pluck('value');
102     }
103
104     /**
105      * Save an array of tags to an entity
106      * @param Entity $entity
107      * @param array $tags
108      * @return array|\Illuminate\Database\Eloquent\Collection
109      */
110     public function saveTagsToEntity(Entity $entity, $tags = [])
111     {
112         $entity->tags()->delete();
113         $newTags = [];
114         foreach ($tags as $tag) {
115             if (trim($tag['name']) === '') continue;
116             $newTags[] = $this->newInstanceFromInput($tag);
117         }
118
119         return $entity->tags()->saveMany($newTags);
120     }
121
122     /**
123      * Create a new Tag instance from user input.
124      * @param $input
125      * @return Tag
126      */
127     protected function newInstanceFromInput($input)
128     {
129         $name = trim($input['name']);
130         $value = isset($input['value']) ? trim($input['value']) : '';
131         // Any other modification or cleanup required can go here
132         $values = ['name' => $name, 'value' => $value];
133         return $this->tag->newInstance($values);
134     }
135
136 }