1 <?php namespace BookStack;
4 abstract 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 this entities restrictions.
60 public function permissions()
62 return $this->morphMany(EntityPermission::class, 'restrictable');
66 * Check if this entity has a specific restriction set against it.
71 public function hasRestriction($role_id, $action)
73 return $this->permissions()->where('role_id', '=', $role_id)
74 ->where('action', '=', $action)->count() > 0;
78 * Check if this entity has live (active) restrictions in place.
83 public function hasActiveRestriction($role_id, $action)
85 return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
89 * Get the entity jointPermissions this is connected to.
90 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
92 public function jointPermissions()
94 return $this->morphMany(JointPermission::class, 'entity');
98 * Allows checking of the exact class, Used to check entity type.
99 * Cleaner method for is_a.
103 public static function isA($type)
105 return static::getType() === strtolower($type);
112 public static function getType()
114 return strtolower(static::getClassName());
118 * Gets a limited-length version of the entities name.
122 public function getShortName($length = 25)
124 if (strlen($this->name) <= $length) return $this->name;
125 return substr($this->name, 0, $length - 3) . '...';
129 * Perform a full-text search on this entity.
130 * @param string[] $fieldsToSearch
131 * @param string[] $terms
132 * @param string[] array $wheres
135 public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
138 foreach ($terms as $key => $term) {
139 $term = htmlentities($term, ENT_QUOTES);
140 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
141 if (preg_match('/\s/', $term)) {
142 $exactTerms[] = '%' . $term . '%';
143 $term = '"' . $term . '"';
145 $term = '' . $term . '*';
147 if ($term !== '*') $terms[$key] = $term;
149 $termString = implode(' ', $terms);
150 $fields = implode(',', $fieldsToSearch);
151 $search = static::selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
152 $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
154 // Ensure at least one exact term matches if in search
155 if (count($exactTerms) > 0) {
156 $search = $search->where(function ($query) use ($exactTerms, $fieldsToSearch) {
157 foreach ($exactTerms as $exactTerm) {
158 foreach ($fieldsToSearch as $field) {
159 $query->orWhere($field, 'like', $exactTerm);
165 // Add additional where terms
166 foreach ($wheres as $whereTerm) {
167 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
170 if (static::isA('page')) {
171 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
172 } else if (static::isA('chapter')) {
173 $search = $search->with('book');
176 return $search->orderBy('title_relevance', 'desc');
180 * Get the url for this item.
183 abstract public function getUrl();