1 <?php namespace BookStack;
4 class Entity extends Ownable
8 * Compares this entity to another given entity.
9 * Matches by comparing class and id.
13 public function matches($entity)
15 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
19 * Checks if an entity matches or contains another given entity.
20 * @param Entity $entity
23 public function matchesOrContains(Entity $entity)
25 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
27 if ($matches) return true;
29 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
30 return $entity->book_id === $this->id;
33 if ($entity->isA('page') && $this->isA('chapter')) {
34 return $entity->chapter_id === $this->id;
41 * Gets the activity objects for this entity.
42 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
44 public function activity()
46 return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
50 * Get View objects for this entity.
52 public function views()
54 return $this->morphMany(View::class, 'viewable');
58 * Get the Tag models that have been user assigned to this entity.
59 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
61 public function tags()
63 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
67 * Get this entities restrictions.
69 public function permissions()
71 return $this->morphMany(EntityPermission::class, 'restrictable');
75 * Check if this entity has a specific restriction set against it.
80 public function hasRestriction($role_id, $action)
82 return $this->permissions()->where('role_id', '=', $role_id)
83 ->where('action', '=', $action)->count() > 0;
87 * Check if this entity has live (active) restrictions in place.
92 public function hasActiveRestriction($role_id, $action)
94 return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
98 * Get the entity jointPermissions this is connected to.
99 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
101 public function jointPermissions()
103 return $this->morphMany(JointPermission::class, 'entity');
107 * Allows checking of the exact class, Used to check entity type.
108 * Cleaner method for is_a.
112 public static function isA($type)
114 return static::getType() === strtolower($type);
121 public static function getType()
123 return strtolower(static::getClassName());
127 * Get an instance of an entity of the given type.
131 public static function getEntityInstance($type)
133 $types = ['Page', 'Book', 'Chapter'];
134 $className = str_replace([' ', '-', '_'], '', ucwords($type));
135 if (!in_array($className, $types)) {
139 return app('BookStack\\' . $className);
143 * Gets a limited-length version of the entities name.
147 public function getShortName($length = 25)
149 if (strlen($this->name) <= $length) return $this->name;
150 return substr($this->name, 0, $length - 3) . '...';
154 * Perform a full-text search on this entity.
155 * @param string[] $fieldsToSearch
156 * @param string[] $terms
157 * @param string[] array $wheres
160 public function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
164 $search = static::newQuery();
166 foreach ($terms as $key => $term) {
167 $term = htmlentities($term, ENT_QUOTES);
168 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
169 if (preg_match('/".*?"/', $term) || is_numeric($term)) {
170 $term = str_replace('"', '', $term);
171 $exactTerms[] = '%' . $term . '%';
173 $term = '' . $term . '*';
174 if ($term !== '*') $fuzzyTerms[] = $term;
178 $isFuzzy = count($exactTerms) === 0 && count($fuzzyTerms) > 0;
181 // Perform fulltext search if relevant terms exist.
183 $termString = implode(' ', $fuzzyTerms);
184 $fields = implode(',', $fieldsToSearch);
185 $search = $search->selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
186 $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
189 // Ensure at least one exact term matches if in search
190 if (count($exactTerms) > 0) {
191 $search = $search->where(function ($query) use ($exactTerms, $fieldsToSearch) {
192 foreach ($exactTerms as $exactTerm) {
193 foreach ($fieldsToSearch as $field) {
194 $query->orWhere($field, 'like', $exactTerm);
200 $orderBy = $isFuzzy ? 'title_relevance' : 'updated_at';
202 // Add additional where terms
203 foreach ($wheres as $whereTerm) {
204 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
208 if ($this->isA('page')) {
209 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
210 } else if ($this->isA('chapter')) {
211 $search = $search->with('book');
214 return $search->orderBy($orderBy, 'desc');