5 use Illuminate\Database\Eloquent\Model;
7 abstract class Entity extends Model
11 * Relation for the user that created this entity.
12 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
14 public function createdBy()
16 return $this->belongsTo('BookStack\User', 'created_by');
20 * Relation for the user that updated this entity.
21 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
23 public function updatedBy()
25 return $this->belongsTo('BookStack\User', 'updated_by');
29 * Compares this entity to another given entity.
30 * Matches by comparing class and id.
34 public function matches($entity)
36 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
40 * Checks if an entity matches or contains another given entity.
41 * @param Entity $entity
44 public function matchesOrContains(Entity $entity)
46 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
48 if ($matches) return true;
50 if ($entity->isA('chapter') && $this->isA('book')) {
51 return $entity->book_id === $this->id;
54 if ($entity->isA('page') && $this->isA('book')) {
55 return $entity->book_id === $this->id;
58 if ($entity->isA('page') && $this->isA('chapter')) {
59 return $entity->chapter_id === $this->id;
66 * Gets the activity objects for this entity.
67 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
69 public function activity()
71 return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
75 * Get View objects for this entity.
78 public function views()
80 return $this->morphMany('BookStack\View', 'viewable');
84 * Get just the views for the current user.
87 public function userViews()
89 return $this->views()->where('user_id', '=', auth()->user()->id);
93 * Allows checking of the exact class, Used to check entity type.
94 * Cleaner method for is_a.
98 public static function isA($type)
100 return static::getClassName() === strtolower($type);
104 * Gets the class name.
107 public static function getClassName()
109 return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
113 *Gets a limited-length version of the entities name.
117 public function getShortName($length = 25)
119 if(strlen($this->name) <= $length) return $this->name;
120 return substr($this->name, 0, $length-3) . '...';
124 * Perform a full-text search on this entity.
125 * @param string[] $fieldsToSearch
126 * @param string[] $terms
127 * @param string[] array $wheres
130 public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
133 foreach ($terms as $term) {
134 $termString .= $term . '* ';
136 $fields = implode(',', $fieldsToSearch);
137 $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
138 foreach ($wheres as $whereTerm) {
139 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
142 if (!static::isA('book')) {
143 $search = $search->with('book');
146 if (static::isA('page')) {
147 $search = $search->with('chapter');
150 return $search->get();
154 * Get the url for this item.
157 abstract public function getUrl();