1 <?php namespace BookStack\Entities;
3 use BookStack\Actions\Activity;
4 use BookStack\Actions\Comment;
5 use BookStack\Actions\Tag;
6 use BookStack\Actions\View;
7 use BookStack\Auth\Permissions\EntityPermission;
8 use BookStack\Auth\Permissions\JointPermission;
11 use Illuminate\Database\Eloquent\Relations\MorphMany;
15 * The base class for book-like items such as pages, chapters & books.
16 * This is not a database model in itself but extended.
18 * @property integer $id
19 * @property string $name
20 * @property string $slug
21 * @property Carbon $created_at
22 * @property Carbon $updated_at
23 * @property int $created_by
24 * @property int $updated_by
25 * @property boolean $restricted
27 * @package BookStack\Entities
29 class Entity extends Ownable
33 * @var string - Name of property where the main text content is found
35 public $textField = 'description';
38 * @var float - Multiplier for search indexing.
40 public $searchFactor = 1.0;
43 * Get the morph class for this model.
44 * Set here since, due to folder changes, the namespace used
45 * in the database no longer matches the class namespace.
48 public function getMorphClass()
50 return 'BookStack\\Entity';
54 * Compares this entity to another given entity.
55 * Matches by comparing class and id.
59 public function matches($entity)
61 return [get_class($this), $this->id] === [get_class($entity), $entity->id];
65 * Checks if an entity matches or contains another given entity.
66 * @param Entity $entity
69 public function matchesOrContains(Entity $entity)
71 $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
77 if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
78 return $entity->book_id === $this->id;
81 if ($entity->isA('page') && $this->isA('chapter')) {
82 return $entity->chapter_id === $this->id;
89 * Gets the activity objects for this entity.
90 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
92 public function activity()
94 return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
98 * Get View objects for this entity.
100 public function views()
102 return $this->morphMany(View::class, 'viewable');
106 * Get the Tag models that have been user assigned to this entity.
107 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
109 public function tags()
111 return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
115 * Get the comments for an entity
116 * @param bool $orderByCreated
119 public function comments($orderByCreated = true)
121 $query = $this->morphMany(Comment::class, 'entity');
122 return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
126 * Get the related search terms.
127 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
129 public function searchTerms()
131 return $this->morphMany(SearchTerm::class, 'entity');
135 * Get this entities restrictions.
137 public function permissions()
139 return $this->morphMany(EntityPermission::class, 'restrictable');
143 * Check if this entity has a specific restriction set against it.
148 public function hasRestriction($role_id, $action)
150 return $this->permissions()->where('role_id', '=', $role_id)
151 ->where('action', '=', $action)->count() > 0;
155 * Get the entity jointPermissions this is connected to.
156 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
158 public function jointPermissions()
160 return $this->morphMany(JointPermission::class, 'entity');
164 * Allows checking of the exact class, Used to check entity type.
165 * Cleaner method for is_a.
169 public static function isA($type)
171 return static::getType() === strtolower($type);
178 public static function getType()
180 return strtolower(static::getClassName());
184 * Get an instance of an entity of the given type.
188 public static function getEntityInstance($type)
190 $types = ['Page', 'Book', 'Chapter', 'Bookshelf'];
191 $className = str_replace([' ', '-', '_'], '', ucwords($type));
192 if (!in_array($className, $types)) {
196 return app('BookStack\\Entities\\' . $className);
200 * Gets a limited-length version of the entities name.
204 public function getShortName($length = 25)
206 if (mb_strlen($this->name) <= $length) {
209 return mb_substr($this->name, 0, $length - 3) . '...';
213 * Get the body text of this entity.
216 public function getText()
218 return $this->{$this->textField};
222 * Return a generalised, common raw query that can be 'unioned' across entities.
225 public function entityRawQuery()
231 * Get the url of this entity
235 public function getUrl($path = '/')