X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/58fa7679bccafd00f9a50bcd4a87e96876331b03..refs/pull/5280/head:/app/Entities/Models/Entity.php diff --git a/app/Entities/Models/Entity.php b/app/Entities/Models/Entity.php index 561876769..0de83c938 100644 --- a/app/Entities/Models/Entity.php +++ b/app/Entities/Models/Entity.php @@ -1,21 +1,28 @@ -scopeHasPermission($query, 'view'); - } + public float $searchFactor = 1.0; /** - * Scope the query to those entities that the current user has the given permission for. + * Get the entities that are visible to the current user. */ - public function scopeHasPermission(Builder $query, string $permission) + public function scopeVisible(Builder $query): Builder { - return Permissions::restrictEntityQuery($query, $permission); + return app()->make(PermissionApplicator::class)->restrictEntityQuery($query); } /** @@ -103,7 +107,7 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable * Compares this entity to another given entity. * Matches by comparing class and id. */ - public function matches(Entity $entity): bool + public function matches(self $entity): bool { return [get_class($this), $this->id] === [get_class($entity), $entity->id]; } @@ -111,17 +115,17 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable /** * Checks if the current entity matches or contains the given. */ - public function matchesOrContains(Entity $entity): bool + public function matchesOrContains(self $entity): bool { if ($this->matches($entity)) { return true; } - if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) { + if (($entity instanceof BookChild) && $this instanceof Book) { return $entity->book_id === $this->id; } - if ($entity->isA('page') && $this->isA('chapter')) { + if ($entity instanceof Page && $this instanceof Chapter) { return $entity->chapter_id === $this->id; } @@ -133,7 +137,7 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable */ public function activity(): MorphMany { - return $this->morphMany(Activity::class, 'entity') + return $this->morphMany(Activity::class, 'loggable') ->orderBy('created_at', 'desc'); } @@ -154,11 +158,12 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable } /** - * Get the comments for an entity + * Get the comments for an entity. */ public function comments(bool $orderByCreated = true): MorphMany { $query = $this->morphMany(Comment::class, 'entity'); + return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query; } @@ -175,16 +180,15 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable */ public function permissions(): MorphMany { - return $this->morphMany(EntityPermission::class, 'restrictable'); + return $this->morphMany(EntityPermission::class, 'entity'); } /** * Check if this entity has a specific restriction set against it. */ - public function hasRestriction(int $role_id, string $action): bool + public function hasPermissions(): bool { - return $this->permissions()->where('role_id', '=', $role_id) - ->where('action', '=', $action)->count() > 0; + return $this->permissions()->count() > 0; } /** @@ -203,9 +207,27 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable return $this->morphMany(Deletion::class, 'deletable'); } + /** + * Get the references pointing from this entity to other items. + */ + public function referencesFrom(): MorphMany + { + return $this->morphMany(Reference::class, 'from'); + } + + /** + * Get the references pointing to this entity from other items. + */ + public function referencesTo(): MorphMany + { + return $this->morphMany(Reference::class, 'to'); + } + /** * Check if this instance or class is a certain type of entity. - * Examples of $type are 'page', 'book', 'chapter' + * Examples of $type are 'page', 'book', 'chapter'. + * + * @deprecated Use instanceof instead. */ public static function isA(string $type): bool { @@ -218,6 +240,7 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable public static function getType(): string { $className = array_slice(explode('\\', static::class), -1, 1)[0]; + return strtolower($className); } @@ -229,15 +252,8 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable if (mb_strlen($this->name) <= $length) { return $this->name; } - return mb_substr($this->name, 0, $length - 3) . '...'; - } - /** - * Get the body text of this entity. - */ - public function getText(): string - { - return $this->{$this->textField} ?? ''; + return mb_substr($this->name, 0, $length - 3) . '...'; } /** @@ -245,17 +261,17 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable */ public function getExcerpt(int $length = 100): string { - $text = $this->getText(); + $text = $this->{$this->textField} ?? ''; if (mb_strlen($text) > $length) { - $text = mb_substr($text, 0, $length-3) . '...'; + $text = mb_substr($text, 0, $length - 3) . '...'; } return trim($text); } /** - * Get the url of this entity + * Get the url of this entity. */ abstract public function getUrl(string $path = '/'): string; @@ -264,14 +280,15 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable * This is the "static" parent and does not include dynamic * relations such as shelves to books. */ - public function getParent(): ?Entity + public function getParent(): ?self { - if ($this->isA('page')) { + if ($this instanceof Page) { return $this->chapter_id ? $this->chapter()->withTrashed()->first() : $this->book()->withTrashed()->first(); } - if ($this->isA('chapter')) { + if ($this instanceof Chapter) { return $this->book()->withTrashed()->first(); } + return null; } @@ -280,29 +297,29 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable */ public function rebuildPermissions() { - /** @noinspection PhpUnhandledExceptionInspection */ - Permissions::buildJointPermissionsForEntity(clone $this); + app()->make(JointPermissionBuilder::class)->rebuildForEntity(clone $this); } /** - * Index the current entity for search + * Index the current entity for search. */ public function indexForSearch() { - app(SearchIndex::class)->indexEntity(clone $this); + app()->make(SearchIndex::class)->indexEntity(clone $this); } /** - * @inheritdoc + * {@inheritdoc} */ public function refreshSlug(): string { - $this->slug = app(SlugGenerator::class)->generate($this); + $this->slug = app()->make(SlugGenerator::class)->generate($this); + return $this->slug; } /** - * @inheritdoc + * {@inheritdoc} */ public function favourites(): MorphMany { @@ -318,4 +335,20 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable ->where('user_id', '=', user()->id) ->exists(); } + + /** + * Get the related watches for this entity. + */ + public function watches(): MorphMany + { + return $this->morphMany(Watch::class, 'watchable'); + } + + /** + * {@inheritdoc} + */ + public function logDescriptor(): string + { + return "({$this->id}) {$this->name}"; + } }