+
+ /**
+ * 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(clone $this);
+ }
+
+ /**
+ * Index the current entity for search
+ */
+ public function indexForSearch()
+ {
+ $searchService = app()->make(SearchService::class);
+ $searchService->indexEntity(clone $this);
+ }
+
+ /**
+ * Generate and set a new URL slug for this model.
+ */
+ public function refreshSlug(): string
+ {
+ $generator = new SlugGenerator($this);
+ $this->slug = $generator->generate();
+ return $this->slug;
+ }