X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/78bf044a7acf39dfc91588099435cd27038b61b2..refs/pull/2376/head:/app/Entities/Entity.php diff --git a/app/Entities/Entity.php b/app/Entities/Entity.php index 120290d8f..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 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($type) + public static function isA(string $type): bool { return static::getType() === strtolower($type); } @@ -280,6 +287,22 @@ 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. */