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 * Get the entity jointPermissions this is connected to.
99 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
101 public function jointPermissions()
103 return $this->morphMany(JointPermission::class, 'entity');
107 * Allows checking of the exact class, Used to check entity type.
108 * Cleaner method for is_a.
112 public static function isA($type)
114 return static::getType() === strtolower($type);
121 public static function getType()
123 return strtolower(static::getClassName());
127 * Get an instance of an entity of the given type.
131 public static function getEntityInstance($type)
133 $types = ['Page', 'Book', 'Chapter'];
134 $className = str_replace([' ', '-', '_'], '', ucwords($type));
135 if (!in_array($className, $types)) {
139 return app('BookStack\\' . $className);
143 * Gets a limited-length version of the entities name.
147 public function getShortName($length = 25)
149 if (strlen($this->name) <= $length) return $this->name;
150 return substr($this->name, 0, $length - 3) . '...';
154 * Get the body text of this entity.
157 public function getText()
159 return $this->{$this->textField};
163 * Return a generalised, common raw query that can be 'unioned' across entities.
166 public function entityRawQuery(){return '';}
169 * Get the url of this entity
173 public function getUrl($path){return '/';}