X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ddb7f33868ea499ab8f48a7062f145e8c0fbe02f..refs/pull/2376/head:/app/Entities/Entity.php diff --git a/app/Entities/Entity.php b/app/Entities/Entity.php index 5013c39cf..99922bceb 100644 --- a/app/Entities/Entity.php +++ b/app/Entities/Entity.php @@ -12,6 +12,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\SoftDeletes; /** * Class Entity @@ -36,6 +37,7 @@ use Illuminate\Database\Eloquent\Relations\MorphMany; */ class Entity extends Ownable { + use SoftDeletes; /** * @var string - Name of property where the main text content is found @@ -193,20 +195,25 @@ class Entity extends Ownable /** * Get the entity jointPermissions this is connected to. - * @return MorphMany */ - public function jointPermissions() + public function jointPermissions(): MorphMany { return $this->morphMany(JointPermission::class, 'entity'); } /** - * Allows checking of the exact class, Used to check entity type. - * Cleaner method for is_a. - * @param $type - * @return bool + * Get the related delete records for this entity. */ - public static function isA($type) + public function deletions(): MorphMany + { + return $this->morphMany(Deletion::class, 'deletable'); + } + + /** + * Check if this instance or class is a certain type of entity. + * Examples of $type are 'page', 'book', 'chapter' + */ + public static function isA(string $type): bool { return static::getType() === strtolower($type); } @@ -238,10 +245,8 @@ class Entity extends Ownable /** * Gets a limited-length version of the entities name. - * @param int $length - * @return string */ - public function getShortName($length = 25) + public function getShortName(int $length = 25): string { if (mb_strlen($this->name) <= $length) { return $this->name; @@ -282,13 +287,29 @@ class Entity extends Ownable return $path; } + /** + * Get the parent entity if existing. + * This is the "static" parent and does not include dynamic + * relations such as shelves to books. + */ + public function getParent(): ?Entity + { + if ($this->isA('page')) { + return $this->chapter_id ? $this->chapter()->withTrashed()->first() : $this->book()->withTrashed()->first(); + } + if ($this->isA('chapter')) { + return $this->book()->withTrashed()->first(); + } + return null; + } + /** * Rebuild the permissions for this entity. */ public function rebuildPermissions() { /** @noinspection PhpUnhandledExceptionInspection */ - Permissions::buildJointPermissionsForEntity($this); + Permissions::buildJointPermissionsForEntity(clone $this); } /** @@ -297,7 +318,7 @@ class Entity extends Ownable public function indexForSearch() { $searchService = app()->make(SearchService::class); - $searchService->indexEntity($this); + $searchService->indexEntity(clone $this); } /**