1 <?php namespace BookStack\Repos;
5 use BookStack\Services\PermissionService;
9 * @package BookStack\Repos
16 protected $permissionService;
19 * TagRepo constructor.
22 * @param PermissionService $ps
24 public function __construct(Tag $attr, Entity $ent, PermissionService $ps)
28 $this->permissionService = $ps;
32 * Get an entity instance of its particular type.
35 * @param string $action
36 * @return \Illuminate\Database\Eloquent\Model|null|static
38 public function getEntity($entityType, $entityId, $action = 'view')
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();
47 * Get all tags for a particular entity.
48 * @param string $entityType
49 * @param int $entityId
52 public function getForEntity($entityType, $entityId)
54 $entity = $this->getEntity($entityType, $entityId);
55 if ($entity === null) {
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.
68 public function getNameSuggestions($searchTerm = false)
70 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
73 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
75 $query = $query->orderBy('count', 'desc')->take(50);
78 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
79 return $query->get(['name'])->pluck('name');
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.
90 public function getValueSuggestions($searchTerm = false, $tagName = false)
92 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
95 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
97 $query = $query->orderBy('count', 'desc')->take(50);
100 if ($tagName !== false) {
101 $query = $query->where('name', '=', $tagName);
104 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
105 return $query->get(['value'])->pluck('value');
109 * Save an array of tags to an entity
110 * @param Entity $entity
112 * @return array|\Illuminate\Database\Eloquent\Collection
114 public function saveTagsToEntity(Entity $entity, $tags = [])
116 $entity->tags()->delete();
118 foreach ($tags as $tag) {
119 if (trim($tag['name']) === '') {
122 $newTags[] = $this->newInstanceFromInput($tag);
125 return $entity->tags()->saveMany($newTags);
129 * Create a new Tag instance from user input.
133 protected function newInstanceFromInput($input)
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);