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('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
50 * Get View objects for this entity.
53 public function views()
55 return $this->morphMany('BookStack\View', 'viewable');
59 * Allows checking of the exact class, Used to check entity type.
60 * Cleaner method for is_a.
64 public static function isA($type)
66 return static::getClassName() === strtolower($type);
70 * Gets a limited-length version of the entities name.
74 public function getShortName($length = 25)
76 if(strlen($this->name) <= $length) return $this->name;
77 return substr($this->name, 0, $length-3) . '...';
81 * Perform a full-text search on this entity.
82 * @param string[] $fieldsToSearch
83 * @param string[] $terms
84 * @param string[] array $wheres
87 public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
90 foreach ($terms as $term) {
91 $termString .= htmlentities($term) . '* ';
93 $fields = implode(',', $fieldsToSearch);
94 $termStringEscaped = \DB::connection()->getPdo()->quote($termString);
95 $search = static::addSelect(\DB::raw('*, MATCH(name) AGAINST('.$termStringEscaped.' IN BOOLEAN MODE) AS title_relevance'));
96 $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
98 // Add additional where terms
99 foreach ($wheres as $whereTerm) {
100 $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
104 if (static::isA('page')) {
105 $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
106 } else if (static::isA('chapter')) {
107 $search = $search->with('book');
110 return $search->orderBy('title_relevance', 'desc');
114 * Get the url for this item.
117 abstract public function getUrl();