1 <?php namespace BookStack;
4 class Entity extends Ownable
7 public $textField = '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 the related search terms.
70 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
72 public function searchTerms()
74 return $this->morphMany(SearchTerm::class, 'entity');
78 * Get this entities restrictions.
80 public function permissions()
82 return $this->morphMany(EntityPermission::class, 'restrictable');
86 * Check if this entity has a specific restriction set against it.
91 public function hasRestriction($role_id, $action)
93 return $this->permissions()->where('role_id', '=', $role_id)
94 ->where('action', '=', $action)->count() > 0;
98 * Check if this entity has live (active) restrictions in place.
103 public function hasActiveRestriction($role_id, $action)
105 return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
109 * Get the entity jointPermissions this is connected to.
110 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
112 public function jointPermissions()
114 return $this->morphMany(JointPermission::class, 'entity');
118 * Allows checking of the exact class, Used to check entity type.
119 * Cleaner method for is_a.
123 public static function isA($type)
125 return static::getType() === strtolower($type);
132 public static function getType()
134 return strtolower(static::getClassName());
138 * Get an instance of an entity of the given type.
142 public static function getEntityInstance($type)
144 $types = ['Page', 'Book', 'Chapter'];
145 $className = str_replace([' ', '-', '_'], '', ucwords($type));
146 if (!in_array($className, $types)) {
150 return app('BookStack\\' . $className);
154 * Gets a limited-length version of the entities name.
158 public function getShortName($length = 25)
160 if (strlen($this->name) <= $length) return $this->name;
161 return substr($this->name, 0, $length - 3) . '...';
165 * Get the body text of this entity.
168 public function getText()
170 return $this->{$this->textField};
174 * Return a generalised, common raw query that can be 'unioned' across entities.
177 public function entityRawQuery(){return '';}
180 * Perform a full-text search on this entity.
181 * @param string[] $terms
182 * @param string[] array $wheres
186 public function fullTextSearchQuery($terms, $wheres = [])
190 $search = static::newQuery();
192 foreach ($terms as $key => $term) {
193 $term = htmlentities($term, ENT_QUOTES);
194 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
195 if (preg_match('/".*?"/', $term) || is_numeric($term)) {
196 $term = str_replace('"', '', $term);
197 $exactTerms[] = '%' . $term . '%';
199 $term = '' . $term . '*';
200 if ($term !== '*') $fuzzyTerms[] = $term;
204 $isFuzzy = count($exactTerms) === 0 && count($fuzzyTerms) > 0;
205 $fieldsToSearch = ['name', $this->textField];
207 // Perform fulltext search if relevant terms exist.
209 $termString = implode(' ', $fuzzyTerms);
211 $search = $search->selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
212 $search = $search->whereRaw('MATCH(' . implode(',', $fieldsToSearch ). ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
215 // Ensure at least one exact term matches if in search
216 if (count($exactTerms) > 0) {
217 $search = $search->where(function ($query) use ($exactTerms, $fieldsToSearch) {
218 foreach ($exactTerms as $exactTerm) {
219 foreach ($fieldsToSearch as $field) {
220 $query->orWhere($field, 'like', $exactTerm);
226 $orderBy = $isFuzzy ? 'title_relevance' : 'updated_at';
228 // Add additional where terms
229 foreach ($wheres as $whereTerm) {
230 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
234 if ($this->isA('page')) {
235 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
236 } else if ($this->isA('chapter')) {
237 $search = $search->with('book');
240 return $search->orderBy($orderBy, 'desc');