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
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) return collect();
60 * Get tag name suggestions from scanning existing tag names.
61 * If no search term is given the 50 most popular tag names are provided.
65 public function getNameSuggestions($searchTerm = false)
67 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('name');
70 $query = $query->where('name', 'LIKE', $searchTerm . '%')->orderBy('name', 'desc');
72 $query = $query->orderBy('count', 'desc')->take(50);
75 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
76 return $query->get(['name'])->pluck('name');
80 * Get tag value suggestions from scanning existing tag values.
81 * If no search is given the 50 most popular values are provided.
82 * Passing a tagName will only find values for a tags with a particular name.
87 public function getValueSuggestions($searchTerm = false, $tagName = false)
89 $query = $this->tag->select('*', \DB::raw('count(*) as count'))->groupBy('value');
92 $query = $query->where('value', 'LIKE', $searchTerm . '%')->orderBy('value', 'desc');
94 $query = $query->orderBy('count', 'desc')->take(50);
97 if ($tagName !== false) $query = $query->where('name', '=', $tagName);
99 $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
100 return $query->get(['value'])->pluck('value');
104 * Save an array of tags to an entity
105 * @param Entity $entity
107 * @return array|\Illuminate\Database\Eloquent\Collection
109 public function saveTagsToEntity(Entity $entity, $tags = [])
111 $entity->tags()->delete();
113 foreach ($tags as $tag) {
114 if (trim($tag['name']) === '') continue;
115 $newTags[] = $this->newInstanceFromInput($tag);
118 return $entity->tags()->saveMany($newTags);
122 * Create a new Tag instance from user input.
126 protected function newInstanceFromInput($input)
128 $name = trim($input['name']);
129 $value = isset($input['value']) ? trim($input['value']) : '';
130 // Any other modification or cleanup required can go here
131 $values = ['name' => $name, 'value' => $value];
132 return $this->tag->newInstance($values);