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();
165 foreach ($terms as $key => $term) {
166 $safeTerm = htmlentities($term, ENT_QUOTES);
167 $safeTerm = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $safeTerm);
168 if (preg_match('/".*?"/', $safeTerm) || is_numeric($safeTerm)) {
169 $safeTerm = preg_replace('/^"(.*?)"$/', '$1', $term);
170 $exactTerms[] = '%' . $safeTerm . '%';
172 $safeTerm = '' . $safeTerm . '*';
173 if (trim($safeTerm) !== '*') $fuzzyTerms[] = $safeTerm;
176 $isFuzzy = count($exactTerms) === 0 || count($fuzzyTerms) > 0;
178 // Perform fulltext search if relevant terms exist.
180 $termString = implode(' ', $fuzzyTerms);
181 $fields = implode(',', $fieldsToSearch);
182 $search = $search->selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
183 $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
186 // Ensure at least one exact term matches if in search
187 if (count($exactTerms) > 0) {
188 $search = $search->where(function ($query) use ($exactTerms, $fieldsToSearch) {
189 foreach ($exactTerms as $exactTerm) {
190 foreach ($fieldsToSearch as $field) {
191 $query->orWhere($field, 'like', $exactTerm);
196 $orderBy = $isFuzzy ? 'title_relevance' : 'updated_at';
198 // Add additional where terms
199 foreach ($wheres as $whereTerm) {
200 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
204 if ($this->isA('page')) {
205 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
206 } else if ($this->isA('chapter')) {
207 $search = $search->with('book');
210 return $search->orderBy($orderBy, 'desc');