1 <?php namespace BookStack;
4 use Illuminate\Database\Eloquent\Relations\MorphMany;
6 class Entity extends Ownable
9 public $textField = 'description';
12 * Compares this entity to another given entity.
13 * Matches by comparing class and id.
17 public function matches($entity)
19 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
23 * Checks if an entity matches or contains another given entity.
24 * @param Entity $entity
27 public function matchesOrContains(Entity $entity)
29 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
31 if ($matches) return true;
33 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
34 return $entity->book_id === $this->id;
37 if ($entity->isA('page') && $this->isA('chapter')) {
38 return $entity->chapter_id === $this->id;
45 * Gets the activity objects for this entity.
46 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
48 public function activity()
50 return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
54 * Get View objects for this entity.
56 public function views()
58 return $this->morphMany(View::class, 'viewable');
62 * Get the Tag models that have been user assigned to this entity.
63 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
65 public function tags()
67 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
71 * Get the comments for an entity
72 * @param bool $orderByCreated
75 public function comments($orderByCreated = true)
77 $query = $this->morphMany(Comment::class, 'entity');
78 return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
82 * Get the related search terms.
83 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
85 public function searchTerms()
87 return $this->morphMany(SearchTerm::class, 'entity');
91 * Get this entities restrictions.
93 public function permissions()
95 return $this->morphMany(EntityPermission::class, 'restrictable');
99 * Check if this entity has a specific restriction set against it.
104 public function hasRestriction($role_id, $action)
106 return $this->permissions()->where('role_id', '=', $role_id)
107 ->where('action', '=', $action)->count() > 0;
111 * Get the entity jointPermissions this is connected to.
112 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
114 public function jointPermissions()
116 return $this->morphMany(JointPermission::class, 'entity');
120 * Allows checking of the exact class, Used to check entity type.
121 * Cleaner method for is_a.
125 public static function isA($type)
127 return static::getType() === strtolower($type);
134 public static function getType()
136 return strtolower(static::getClassName());
140 * Get an instance of an entity of the given type.
144 public static function getEntityInstance($type)
146 $types = ['Page', 'Book', 'Chapter'];
147 $className = str_replace([' ', '-', '_'], '', ucwords($type));
148 if (!in_array($className, $types)) {
152 return app('BookStack\\' . $className);
156 * Gets a limited-length version of the entities name.
160 public function getShortName($length = 25)
162 if (strlen($this->name) <= $length) return $this->name;
163 return substr($this->name, 0, $length - 3) . '...';
167 * Get the body text of this entity.
170 public function getText()
172 return $this->{$this->textField};
176 * Return a generalised, common raw query that can be 'unioned' across entities.
179 public function entityRawQuery(){return '';}
182 * Get the url of this entity
186 public function getUrl($path){return '/';}