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) return collect();
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.
66 public function getNameSuggestions($searchTerm = false)
68 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
71 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
73 $query = $query->orderBy('count', 'desc')->take(50);
76 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
77 return $query->get(['name'])->pluck('name');
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.
88 public function getValueSuggestions($searchTerm = false, $tagName = false)
90 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
93 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
95 $query = $query->orderBy('count', 'desc')->take(50);
98 if ($tagName !== false) $query = $query->where('name', '=', $tagName);
100 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
101 return $query->get(['value'])->pluck('value');
105 * Save an array of tags to an entity
106 * @param Entity $entity
108 * @return array|\Illuminate\Database\Eloquent\Collection
110 public function saveTagsToEntity(Entity $entity, $tags = [])
112 $entity->tags()->delete();
114 foreach ($tags as $tag) {
115 if (trim($tag['name']) === '') continue;
116 $newTags[] = $this->newInstanceFromInput($tag);
119 return $entity->tags()->saveMany($newTags);
123 * Create a new Tag instance from user input.
127 protected function newInstanceFromInput($input)
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);