1 <?php namespace BookStack;
3 use Illuminate\Database\Eloquent\Relations\MorphMany;
5 class Entity extends Ownable
8 public $textField = 'description';
11 * Compares this entity to another given entity.
12 * Matches by comparing class and id.
16 public function matches($entity)
18 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
22 * Checks if an entity matches or contains another given entity.
23 * @param Entity $entity
26 public function matchesOrContains(Entity $entity)
28 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
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(Activity::class, 'entity')->orderBy('created_at', 'desc');
55 * Get View objects for this entity.
57 public function views()
59 return $this->morphMany(View::class, 'viewable');
63 * Get the Tag models that have been user assigned to this entity.
64 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
66 public function tags()
68 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
72 * Get the comments for an entity
73 * @param bool $orderByCreated
76 public function comments($orderByCreated = true)
78 $query = $this->morphMany(Comment::class, 'entity');
79 return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
83 * Get the related search terms.
84 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
86 public function searchTerms()
88 return $this->morphMany(SearchTerm::class, 'entity');
92 * Get this entities restrictions.
94 public function permissions()
96 return $this->morphMany(EntityPermission::class, 'restrictable');
100 * Check if this entity has a specific restriction set against it.
105 public function hasRestriction($role_id, $action)
107 return $this->permissions()->where('role_id', '=', $role_id)
108 ->where('action', '=', $action)->count() > 0;
112 * Get the entity jointPermissions this is connected to.
113 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
115 public function jointPermissions()
117 return $this->morphMany(JointPermission::class, 'entity');
121 * Allows checking of the exact class, Used to check entity type.
122 * Cleaner method for is_a.
126 public static function isA($type)
128 return static::getType() === strtolower($type);
135 public static function getType()
137 return strtolower(static::getClassName());
141 * Get an instance of an entity of the given type.
145 public static function getEntityInstance($type)
147 $types = ['Page', 'Book', 'Chapter'];
148 $className = str_replace([' ', '-', '_'], '', ucwords($type));
149 if (!in_array($className, $types)) {
153 return app('BookStack\\' . $className);
157 * Gets a limited-length version of the entities name.
161 public function getShortName($length = 25)
163 if (strlen($this->name) <= $length) {
166 return substr($this->name, 0, $length - 3) . '...';
170 * Get the body text of this entity.
173 public function getText()
175 return $this->{$this->textField};
179 * Return a generalised, common raw query that can be 'unioned' across entities.
182 public function entityRawQuery()
188 * Get the url of this entity
192 public function getUrl($path)