Eine Vorlage ist reines Layout (Titelseite mit allein stehendem Titel, Kopf- und Fußgrafik aus dem Medienbestand, Herausgeber und Standardvermerk) und wird dem Dokument zugeordnet. Untertitel, Ausgabe, Veröffentlichungsdatum und Copyright-Vermerk gehören zur Dokumentfassung (document_meta). Ohne Vorlage bleibt das PDF unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
94 lines
3.5 KiB
PHP
94 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Http\LibraryCatalog;
|
|
use App\Models\User;
|
|
|
|
/**
|
|
* Das Dokument, wie ein Stand es festschreibt: die im Stand gebundene Dokumentfassung mit ihren Kapiteln und je Kapitel die
|
|
* Entscheidung der zentralen Rechteprüfung (`DecideAccess`). Ansicht und PDF lesen dieses Ergebnis; beide zeigen dieselbe
|
|
* Kapitelreihenfolge und dieselben gebundenen Textfassungen. Eine spätere Bearbeitung des Dokuments ändert einen älteren Stand
|
|
* nicht: Der Stand verweist auf die Fassung, die er festgeschrieben hat.
|
|
*/
|
|
class DocumentGuide
|
|
{
|
|
public function __construct(
|
|
private readonly DecideAccess $access,
|
|
private readonly DocumentReader $reader,
|
|
) {}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(User $user, LicenseArticle $article, ArticleRelease $release, CompositionVersion $document): array
|
|
{
|
|
$document->loadMissing('composition');
|
|
$library = app(LibraryCatalog::class);
|
|
$embedded = app(EmbeddedImages::class);
|
|
$chapters = [];
|
|
|
|
foreach ($this->reader->fromVersion($document) as $chapter) {
|
|
$version = $chapter['version'];
|
|
$view = ($this->access)($user, $article, $release, $version, 'ansehen');
|
|
$download = ($this->access)($user, $article, $release, $version, 'laden');
|
|
|
|
$chapters[] = [
|
|
'number' => $chapter['number'],
|
|
'level' => $chapter['level'],
|
|
'anchor' => $chapter['anchor'],
|
|
'title' => $chapter['title'],
|
|
'version' => $version,
|
|
'allowed' => $view->allowed,
|
|
'grant' => $view->grant,
|
|
'may_download' => $download->allowed,
|
|
'download_grant' => $download->grant,
|
|
'state' => $view->allowed ? 'ok' : ($version->status === ContentVersionStatus::Gesperrt ? 'gesperrt' : 'nicht_lizenziert'),
|
|
'html' => $view->allowed ? $this->shiftHeadings(FormattedText::html($version->body, $embedded->forView($user, $article, $release, $version))) : null,
|
|
'blocks' => $view->allowed ? FormattedText::blocks($version->body) : null,
|
|
];
|
|
}
|
|
|
|
$text = $library->text($document->composition);
|
|
|
|
return [
|
|
'document' => $document,
|
|
'title' => $text['title'],
|
|
'summary_html' => $text['summary_html'],
|
|
'pdf_template' => $document->composition->pdfTemplate,
|
|
'meta' => DocumentMeta::normalize($document->document_meta),
|
|
'chapters' => $chapters,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Zwischenüberschriften im Kapiteltext stehen eine Ebene unter dem Kapiteltitel (h3 wird h4), damit die
|
|
* Überschriftenfolge der Seite stimmt: Dokument h1, Kapitel h2, Unterkapitel h3, Zwischenüberschrift h4.
|
|
*/
|
|
public static function shiftHeadings(string $html): string
|
|
{
|
|
return (string) preg_replace('/<(\/?)h3(?=[\s>])/i', '<$1h4', $html);
|
|
}
|
|
|
|
/**
|
|
* Darf das PDF entstehen? Jedes Kapitel muss für `laden` zulässig sein; sonst fehlte ein Teil des Handbuchs.
|
|
*
|
|
* @param array<string, mixed> $guide
|
|
*/
|
|
public function pdfAllowed(array $guide): bool
|
|
{
|
|
if ($guide['chapters'] === []) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($guide['chapters'] as $chapter) {
|
|
if (! $chapter['allowed'] || ! $chapter['may_download']) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|