5 use Illuminate\Database\Eloquent\Model;
7 abstract class Entity extends Model
13 * Compares this entity to another given entity.
14 * Matches by comparing class and id.
18 public function matches($entity)
20 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
24 * Checks if an entity matches or contains another given entity.
25 * @param Entity $entity
28 public function matchesOrContains(Entity $entity)
30 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
32 if ($matches) return true;
34 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
35 return $entity->book_id === $this->id;
38 if ($entity->isA('page') && $this->isA('chapter')) {
39 return $entity->chapter_id === $this->id;
46 * Gets the activity objects for this entity.
47 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
49 public function activity()
51 return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
55 * Get View objects for this entity.
58 public function views()
60 return $this->morphMany('BookStack\View', 'viewable');
64 * Allows checking of the exact class, Used to check entity type.
65 * Cleaner method for is_a.
69 public static function isA($type)
71 return static::getClassName() === strtolower($type);
75 * Gets the class name.
78 public static function getClassName()
80 return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
84 *Gets a limited-length version of the entities name.
88 public function getShortName($length = 25)
90 if (strlen($this->name) <= $length) return $this->name;
91 return substr($this->name, 0, $length - 3) . '...';
95 * Perform a full-text search on this entity.
96 * @param string[] $fieldsToSearch
97 * @param string[] $terms
98 * @param string[] array $wheres
101 public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
104 foreach ($terms as $key => $term) {
105 $term = htmlentities($term, ENT_QUOTES);
106 $term = preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
107 if (preg_match('/\s/', $term)) {
108 $exactTerms[] = '%' . $term . '%';
109 $term = '"' . $term . '"';
111 $term = '' . $term . '*';
113 if ($term !== '*') $terms[$key] = $term;
115 $termString = implode(' ', $terms);
116 $fields = implode(',', $fieldsToSearch);
117 $search = static::selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
118 $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
120 // Ensure at least one exact term matches if in search
121 if (count($exactTerms) > 0) {
122 $search = $search->where(function($query) use ($exactTerms, $fieldsToSearch) {
123 foreach ($exactTerms as $exactTerm) {
124 foreach ($fieldsToSearch as $field) {
125 $query->orWhere($field, 'like', $exactTerm);
131 // Add additional where terms
132 foreach ($wheres as $whereTerm) {
133 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
136 if (static::isA('page')) {
137 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
138 } else if (static::isA('chapter')) {
139 $search = $search->with('book');
142 return $search->orderBy('title_relevance', 'desc');
146 * Get the url for this item.
149 abstract public function getUrl();