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 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
74 public function comments()
76 return $this->morphMany(Comment::class, 'entity')->orderBy('created_at', 'asc');
81 * Get the related search terms.
82 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
84 public function searchTerms()
86 return $this->morphMany(SearchTerm::class, 'entity');
90 * Get this entities restrictions.
92 public function permissions()
94 return $this->morphMany(EntityPermission::class, 'restrictable');
98 * Check if this entity has a specific restriction set against it.
103 public function hasRestriction($role_id, $action)
105 return $this->permissions()->where('role_id', '=', $role_id)
106 ->where('action', '=', $action)->count() > 0;
110 * Get the entity jointPermissions this is connected to.
111 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
113 public function jointPermissions()
115 return $this->morphMany(JointPermission::class, 'entity');
119 * Allows checking of the exact class, Used to check entity type.
120 * Cleaner method for is_a.
124 public static function isA($type)
126 return static::getType() === strtolower($type);
133 public static function getType()
135 return strtolower(static::getClassName());
139 * Get an instance of an entity of the given type.
143 public static function getEntityInstance($type)
145 $types = ['Page', 'Book', 'Chapter'];
146 $className = str_replace([' ', '-', '_'], '', ucwords($type));
147 if (!in_array($className, $types)) {
151 return app('BookStack\\' . $className);
155 * Gets a limited-length version of the entities name.
159 public function getShortName($length = 25)
161 if (strlen($this->name) <= $length) return $this->name;
162 return substr($this->name, 0, $length - 3) . '...';
166 * Get the body text of this entity.
169 public function getText()
171 return $this->{$this->textField};
175 * Return a generalised, common raw query that can be 'unioned' across entities.
178 public function entityRawQuery(){return '';}
181 * Get the url of this entity
185 public function getUrl($path){return '/';}