*/ public function cover(): HasOne { return $this->hasOne(Image::class, 'image_id'); } /** * Returns a shelf cover image URL, if cover not exists return default cover image. */ public function getCoverUrl(int $width = 440, int $height = 250, string|null $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='): string|null { if (!$this->image_id) { return $default; } try { return $this->cover->getThumb($width, $height, false) ?? $default; } catch (Exception $err) { return $default; } } /** * Check if this data supports having a default template assigned. */ public function supportsDefaultTemplate(): bool { return in_array($this->entity_type, ['book', 'chapter']); } /** * Check this data supports having a cover image assigned. */ public function supportsCoverImage(): bool { return in_array($this->entity_type, ['book', 'bookshelf']); } /** * Get the description as a cleaned/handled HTML string. */ public function getDescriptionHtml(bool $raw = false): string { $html = $this->description_html ?: '

' . nl2br(e($this->description)) . '

'; if ($raw) { return $html; } return HtmlContentFilter::removeScriptsFromHtmlString($html); } /** * Update the description from HTML code. * Optionally takes plaintext to use for the model also. */ public function setDescriptionHtml(string $html, string|null $plaintext = null): void { $this->description_html = $html; if ($plaintext !== null) { $this->description = $plaintext; } if (empty($html) && !empty($plaintext)) { $this->description_html = $this->getDescriptionHtml(); } } }