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 * Gets the activity objects for this entity.
41 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
43 public function activity()
45 return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
49 * Get View objects for this entity.
52 public function views()
54 return $this->morphMany('BookStack\View', 'viewable');
58 * Get just the views for the current user.
61 public function userViews()
63 return $this->views()->where('user_id', '=', auth()->user()->id);
67 * Allows checking of the exact class, Used to check entity type.
68 * Cleaner method for is_a.
72 public static function isA($type)
74 return static::getName() === strtolower($type);
78 * Gets the class name.
81 public static function getName()
83 return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
87 * Perform a full-text search on this entity.
88 * @param string[] $fieldsToSearch
89 * @param string[] $terms
90 * @param string[] array $wheres
93 public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
96 foreach ($terms as $term) {
97 $termString .= $term . '* ';
99 $fields = implode(',', $fieldsToSearch);
100 $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
101 foreach ($wheres as $whereTerm) {
102 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
105 if (!static::isA('book')) {
106 $search = $search->with('book');
109 if(static::isA('page')) {
110 $search = $search->with('chapter');
113 return $search->get();
117 * Get the url for this item.
120 abstract public function getUrl();