Files
Helpddesk/Modules/Documentation/Models/DocumentationDocument.php
T
2026-08-25 22:36:39 +01:00

157 lines
4.1 KiB
PHP

<?php
namespace Modules\Documentation\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
/**
* @property int $id
* @property int|null $documentation_category_id
* @property string $title
* @property string $slug
* @property string|null $summary
* @property string|null $body
* @property string $status
* @property bool $is_featured
* @property string $version
* @property string|null $reference
* @property-read string $rendered_body
*/
class DocumentationDocument extends Model
{
use HasFactory;
use SoftDeletes;
public const STATUS_DRAFT = 'draft';
public const STATUS_PUBLISHED = 'published';
public const STATUS_ARCHIVED = 'archived';
/** @var list<string> */
protected $fillable = [
'documentation_category_id',
'title',
'slug',
'summary',
'body',
'status',
'is_featured',
'version',
'reference',
'attachment_disk',
'attachment_path',
'attachment_name',
'attachment_mime',
'attachment_size',
'published_at',
'created_by',
'updated_by',
];
/** @var array<string, string> */
protected $casts = [
'is_featured' => 'boolean',
'published_at' => 'datetime',
'attachment_size' => 'integer',
];
/**
* Bootstrap the model.
*/
protected static function booted(): void
{
static::saving(function (DocumentationDocument $document): void {
if (blank($document->slug)) {
$document->slug = Str::slug($document->title);
}
if ($document->status === self::STATUS_PUBLISHED && blank($document->published_at)) {
$document->published_at = now();
}
});
}
/**
* Owning category.
*
* @return BelongsTo<DocumentationCategory, DocumentationDocument>
*/
public function category(): BelongsTo
{
return $this->belongsTo(DocumentationCategory::class, 'documentation_category_id');
}
/**
* Document attachments.
*
* @return HasMany<DocumentationAttachment>
*/
public function attachments(): HasMany
{
return $this->hasMany(DocumentationAttachment::class, 'documentation_document_id')->latest();
}
/**
* Published document scope.
*
* @param Builder<DocumentationDocument> $query
*
* @return Builder<DocumentationDocument>
*/
public function scopePublished(Builder $query): Builder
{
return $query->where('status', self::STATUS_PUBLISHED);
}
/**
* Featured document scope.
*
* @param Builder<DocumentationDocument> $query
*
* @return Builder<DocumentationDocument>
*/
public function scopeFeatured(Builder $query): Builder
{
return $query->where('is_featured', true);
}
/**
* Render the document body as safe Markdown HTML for display.
*/
public function getRenderedBodyAttribute(): string
{
$body = trim((string) $this->body);
if ($body === '') {
return '<p>No document body has been added yet.</p>';
}
if (method_exists(Str::class, 'markdown')) {
return (string) Str::markdown($body, [
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
}
return '<p>' . nl2br(e($body), false) . '</p>';
}
/**
* Determine whether the attached file is a Markdown document.
*/
public function hasMarkdownAttachment(): bool
{
$name = strtolower((string) $this->attachment_name);
$mime = strtolower((string) $this->attachment_mime);
return str_ends_with($name, '.md') || in_array($mime, ['text/markdown', 'text/x-markdown', 'text/plain'], true);
}
}