use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphMany;
+use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Entity
*/
class Entity extends Ownable
{
+ use SoftDeletes;
/**
* @var string - Name of property where the main text content is found
/**
* 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);
}
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.
*/