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 function isA($type)
74 return $this->getName() === strtolower($type);
78 * Gets the class name.
81 public function getName()
83 $fullClassName = get_class($this);
84 return strtolower(array_slice(explode('\\', $fullClassName), -1, 1)[0]);
88 * Perform a full-text search on this entity.
89 * @param string[] $fieldsToSearch
90 * @param string[] $terms
91 * @param string[] array $wheres
94 public static function fullTextSearch($fieldsToSearch, $terms, $wheres = [])
97 foreach ($terms as $term) {
98 $termString .= $term . '* ';
100 $fields = implode(',', $fieldsToSearch);
101 $search = static::whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
102 foreach ($wheres as $whereTerm) {
103 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
105 return $search->get();
109 * Get the url for this item.
112 abstract public function getUrl();