1 <?php namespace BookStack\Actions;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Entity;
8 * @package BookStack\Repos
15 protected $permissionService;
18 * TagRepo constructor.
19 * @param \BookStack\Actions\Tag $attr
20 * @param \BookStack\Entities\Entity $ent
21 * @param \BookStack\Auth\Permissions\PermissionService $ps
23 public function __construct(Tag $attr, Entity $ent, PermissionService $ps)
27 $this->permissionService = $ps;
31 * Get an entity instance of its particular type.
34 * @param string $action
35 * @return \Illuminate\Database\Eloquent\Model|null|static
37 public function getEntity($entityType, $entityId, $action = 'view')
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();
46 * Get all tags for a particular entity.
47 * @param string $entityType
48 * @param int $entityId
51 public function getForEntity($entityType, $entityId)
53 $entity = $this->getEntity($entityType, $entityId);
54 if ($entity === null) {
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.
67 public function getNameSuggestions($searchTerm = false)
69 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
72 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
74 $query = $query->orderBy('count', 'desc')->take(50);
77 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
78 return $query->get(['name'])->pluck('name');
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.
89 public function getValueSuggestions($searchTerm = false, $tagName = false)
91 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
94 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
96 $query = $query->orderBy('count', 'desc')->take(50);
99 if ($tagName !== false) {
100 $query = $query->where('name', '=', $tagName);
103 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
104 return $query->get(['value'])->pluck('value');
108 * Save an array of tags to an entity
109 * @param \BookStack\Entities\Entity $entity
111 * @return array|\Illuminate\Database\Eloquent\Collection
113 public function saveTagsToEntity(Entity $entity, $tags = [])
115 $entity->tags()->delete();
117 foreach ($tags as $tag) {
118 if (trim($tag['name']) === '') {
121 $newTags[] = $this->newInstanceFromInput($tag);
124 return $entity->tags()->saveMany($newTags);
128 * Create a new Tag instance from user input.
130 * @return \BookStack\Actions\Tag
132 protected function newInstanceFromInput($input)
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);