1 <?php namespace BookStack;
3 use Illuminate\Database\Eloquent\Relations\MorphMany;
5 class Entity extends Ownable
9 * @var string - Name of property where the main text content is found
11 public $textField = 'description';
14 * @var float - Multiplier for search indexing.
16 public $searchFactor = 1.0;
19 * Compares this entity to another given entity.
20 * Matches by comparing class and id.
24 public function matches($entity)
26 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
30 * Checks if an entity matches or contains another given entity.
31 * @param Entity $entity
34 public function matchesOrContains(Entity $entity)
36 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
42 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
43 return $entity->book_id === $this->id;
46 if ($entity->isA('page') && $this->isA('chapter')) {
47 return $entity->chapter_id === $this->id;
54 * Gets the activity objects for this entity.
55 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
57 public function activity()
59 return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
63 * Get View objects for this entity.
65 public function views()
67 return $this->morphMany(View::class, 'viewable');
71 * Get the Tag models that have been user assigned to this entity.
72 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
74 public function tags()
76 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
80 * Get the comments for an entity
81 * @param bool $orderByCreated
84 public function comments($orderByCreated = true)
86 $query = $this->morphMany(Comment::class, 'entity');
87 return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
91 * Get the related search terms.
92 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
94 public function searchTerms()
96 return $this->morphMany(SearchTerm::class, 'entity');
100 * Get this entities restrictions.
102 public function permissions()
104 return $this->morphMany(EntityPermission::class, 'restrictable');
108 * Check if this entity has a specific restriction set against it.
113 public function hasRestriction($role_id, $action)
115 return $this->permissions()->where('role_id', '=', $role_id)
116 ->where('action', '=', $action)->count() > 0;
120 * Get the entity jointPermissions this is connected to.
121 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
123 public function jointPermissions()
125 return $this->morphMany(JointPermission::class, 'entity');
129 * Allows checking of the exact class, Used to check entity type.
130 * Cleaner method for is_a.
134 public static function isA($type)
136 return static::getType() === strtolower($type);
143 public static function getType()
145 return strtolower(static::getClassName());
149 * Get an instance of an entity of the given type.
153 public static function getEntityInstance($type)
155 $types = ['Page', 'Book', 'Chapter'];
156 $className = str_replace([' ', '-', '_'], '', ucwords($type));
157 if (!in_array($className, $types)) {
161 return app('BookStack\\' . $className);
165 * Gets a limited-length version of the entities name.
169 public function getShortName($length = 25)
171 if (strlen($this->name) <= $length) {
174 return substr($this->name, 0, $length - 3) . '...';
178 * Get the body text of this entity.
181 public function getText()
183 return $this->{$this->textField};
187 * Return a generalised, common raw query that can be 'unioned' across entities.
190 public function entityRawQuery()
196 * Get the url of this entity
200 public function getUrl($path)