X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a6633642232efd164d4708967ab59e498fbff896..refs/pull/3042/head:/app/Entities/Models/Entity.php diff --git a/app/Entities/Models/Entity.php b/app/Entities/Models/Entity.php index c6b2468b0..f5f9d91f0 100644 --- a/app/Entities/Models/Entity.php +++ b/app/Entities/Models/Entity.php @@ -1,7 +1,10 @@ -id] === [get_class($entity), $entity->id]; } @@ -107,7 +114,7 @@ abstract class Entity extends Model /** * 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; @@ -150,11 +157,12 @@ abstract class Entity extends Model } /** - * 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; } @@ -201,7 +209,7 @@ abstract class Entity extends Model /** * 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'. */ public static function isA(string $type): bool { @@ -214,6 +222,7 @@ abstract class Entity extends Model public static function getType(): string { $className = array_slice(explode('\\', static::class), -1, 1)[0]; + return strtolower($className); } @@ -225,6 +234,7 @@ abstract class Entity extends Model if (mb_strlen($this->name) <= $length) { return $this->name; } + return mb_substr($this->name, 0, $length - 3) . '...'; } @@ -244,14 +254,14 @@ abstract class Entity extends Model $text = $this->getText(); 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; @@ -260,14 +270,15 @@ abstract class Entity extends Model * 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; } @@ -281,7 +292,7 @@ abstract class Entity extends Model } /** - * Index the current entity for search + * Index the current entity for search. */ public function indexForSearch() { @@ -289,11 +300,30 @@ abstract class Entity extends Model } /** - * Generate and set a new URL slug for this model. + * {@inheritdoc} */ public function refreshSlug(): string { - $this->slug = (new SlugGenerator)->generate($this); + $this->slug = app(SlugGenerator::class)->generate($this); + return $this->slug; } + + /** + * {@inheritdoc} + */ + public function favourites(): MorphMany + { + return $this->morphMany(Favourite::class, 'favouritable'); + } + + /** + * Check if the entity is a favourite of the current user. + */ + public function isFavourite(): bool + { + return $this->favourites() + ->where('user_id', '=', user()->id) + ->exists(); + } }