1 <?php namespace BookStack;
4 class Entity extends Ownable
7 public $textField = 'description';
10 * Compares this entity to another given entity.
11 * Matches by comparing class and id.
15 public function matches($entity)
17 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
21 * Checks if an entity matches or contains another given entity.
22 * @param Entity $entity
25 public function matchesOrContains(Entity $entity)
27 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
29 if ($matches) return true;
31 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
32 return $entity->book_id === $this->id;
35 if ($entity->isA('page') && $this->isA('chapter')) {
36 return $entity->chapter_id === $this->id;
43 * Gets the activity objects for this entity.
44 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
46 public function activity()
48 return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
52 * Get View objects for this entity.
54 public function views()
56 return $this->morphMany(View::class, 'viewable');
60 * Get the Tag models that have been user assigned to this entity.
61 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
63 public function tags()
65 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
69 * Get the related search terms.
70 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
72 public function searchTerms()
74 return $this->morphMany(SearchTerm::class, 'entity');
78 * Get this entities restrictions.
80 public function permissions()
82 return $this->morphMany(EntityPermission::class, 'restrictable');
86 * Check if this entity has a specific restriction set against it.
91 public function hasRestriction($role_id, $action)
93 return $this->permissions()->where('role_id', '=', $role_id)
94 ->where('action', '=', $action)->count() > 0;
98 * Check if this entity has live (active) restrictions in place.
103 public function hasActiveRestriction($role_id, $action)
105 return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
109 * Get the entity jointPermissions this is connected to.
110 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
112 public function jointPermissions()
114 return $this->morphMany(JointPermission::class, 'entity');
118 * Allows checking of the exact class, Used to check entity type.
119 * Cleaner method for is_a.
123 public static function isA($type)
125 return static::getType() === strtolower($type);
132 public static function getType()
134 return strtolower(static::getClassName());
138 * Get an instance of an entity of the given type.
142 public static function getEntityInstance($type)
144 $types = ['Page', 'Book', 'Chapter'];
145 $className = str_replace([' ', '-', '_'], '', ucwords($type));
146 if (!in_array($className, $types)) {
150 return app('BookStack\\' . $className);
154 * Gets a limited-length version of the entities name.
158 public function getShortName($length = 25)
160 if (strlen($this->name) <= $length) return $this->name;
161 return substr($this->name, 0, $length - 3) . '...';
165 * Get the body text of this entity.
168 public function getText()
170 return $this->{$this->textField};
174 * Return a generalised, common raw query that can be 'unioned' across entities.
177 public function entityRawQuery(){return '';}