1 <?php namespace BookStack;
4 class Entity extends Ownable
7 protected $fieldsToSearch = ['name', 'description'];
10 * Compares this entity to another given entity.
11 * Matches by comparing class and id.
15 public function matches($entity)
17 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
21 * Checks if an entity matches or contains another given entity.
22 * @param Entity $entity
25 public function matchesOrContains(Entity $entity)
27 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
29 if ($matches) return true;
31 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
32 return $entity->book_id === $this->id;
35 if ($entity->isA('page') && $this->isA('chapter')) {
36 return $entity->chapter_id === $this->id;
43 * Gets the activity objects for this entity.
44 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
46 public function activity()
48 return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
52 * Get View objects for this entity.
54 public function views()
56 return $this->morphMany(View::class, 'viewable');
60 * Get the Tag models that have been user assigned to this entity.
61 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
63 public function tags()
65 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
69 * Get this entities restrictions.
71 public function permissions()
73 return $this->morphMany(EntityPermission::class, 'restrictable');
77 * Check if this entity has a specific restriction set against it.
82 public function hasRestriction($role_id, $action)
84 return $this->permissions()->where('role_id', '=', $role_id)
85 ->where('action', '=', $action)->count() > 0;
89 * Check if this entity has live (active) restrictions in place.
94 public function hasActiveRestriction($role_id, $action)
96 return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
100 * Get the entity jointPermissions this is connected to.
101 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
103 public function jointPermissions()
105 return $this->morphMany(JointPermission::class, 'entity');
109 * Allows checking of the exact class, Used to check entity type.
110 * Cleaner method for is_a.
114 public static function isA($type)
116 return static::getType() === strtolower($type);
123 public static function getType()
125 return strtolower(static::getClassName());
129 * Get an instance of an entity of the given type.
133 public static function getEntityInstance($type)
135 $types = ['Page', 'Book', 'Chapter'];
136 $className = str_replace([' ', '-', '_'], '', ucwords($type));
137 if (!in_array($className, $types)) {
141 return app('BookStack\\' . $className);
145 * Gets a limited-length version of the entities name.
149 public function getShortName($length = 25)
151 if (strlen($this->name) <= $length) return $this->name;
152 return substr($this->name, 0, $length - 3) . '...';
156 * Perform a full-text search on this entity.
157 * @param string[] $fieldsToSearch
158 * @param string[] $terms
159 * @param string[] array $wheres
162 public function fullTextSearchQuery($terms, $wheres = [])
166 $search = static::newQuery();
168 foreach ($terms as $key => $term) {
169 $term = htmlentities($term, ENT_QUOTES);
170 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
171 if (preg_match('/".*?"/', $term) || is_numeric($term)) {
172 $term = str_replace('"', '', $term);
173 $exactTerms[] = '%' . $term . '%';
175 $term = '' . $term . '*';
176 if ($term !== '*') $fuzzyTerms[] = $term;
180 $isFuzzy = count($exactTerms) === 0 && count($fuzzyTerms) > 0;
183 // Perform fulltext search if relevant terms exist.
185 $termString = implode(' ', $fuzzyTerms);
186 $fields = implode(',', $this->fieldsToSearch);
187 $search = $search->selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
188 $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
191 // Ensure at least one exact term matches if in search
192 if (count($exactTerms) > 0) {
193 $search = $search->where(function ($query) use ($exactTerms) {
194 foreach ($exactTerms as $exactTerm) {
195 foreach ($this->fieldsToSearch as $field) {
196 $query->orWhere($field, 'like', $exactTerm);
202 $orderBy = $isFuzzy ? 'title_relevance' : 'updated_at';
204 // Add additional where terms
205 foreach ($wheres as $whereTerm) {
206 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
210 if ($this->isA('page')) {
211 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
212 } else if ($this->isA('chapter')) {
213 $search = $search->with('book');
216 return $search->orderBy($orderBy, 'desc');