Ein Dokument ist eine Komposition der Art document, ein Kapitel ein Text mit der automatischen Materialart Dokumentkapitel; die Ebene steht in der Rolle. Der Aufbaueditor bekommt den Kapitelarbeitsplatz (Übersicht, Ordnen als Block, offene Eingaben bleiben beim Wechsel erhalten, Speichern als Entwurf, Bereitstellen als Dokumentfassung). Module und Programme binden eine Fassung über den gemeinsamen Picker ein. Mitglieder lesen sie mit anklickbarem Inhaltsverzeichnis und laden ein PDF mit Inhaltsverzeichnis, Seitenzahlen, Tabellen und Bildern. Ein älterer Stand liefert die gebundene Fassung weiter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
149 lines
5.7 KiB
PHP
149 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Support;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\ArticleService;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\CompositionKind;
|
|
use App\Domain\Catalog\CompositionVersion;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentService;
|
|
use App\Domain\Catalog\DocumentChapters;
|
|
use App\Domain\Catalog\EditorialContentKind;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\RightsProfileVersion;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Baut Dokumente für Tests: ein Handbuch mit Kapiteln, Tabellen und Bildern, Module, die eine Dokumentfassung einbinden,
|
|
* Artikel-Stände und Konten mit Rechten.
|
|
*/
|
|
trait BuildsDocuments
|
|
{
|
|
use BuildsExercises;
|
|
|
|
protected function documentComposition(string $title = 'Trainerhandbuch'): Composition
|
|
{
|
|
$composition = app(ContentService::class)->createComposition($title, CompositionKind::Document);
|
|
$composition->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'dokument')->value('id');
|
|
$composition->save();
|
|
|
|
return $composition;
|
|
}
|
|
|
|
/** Ein Kapitel als bereitgestellte Textfassung im Medienbestand. */
|
|
protected function chapter(Composition $document, string $title, string $body): ContentElementVersion
|
|
{
|
|
$version = $this->section($title, $body);
|
|
app(DocumentChapters::class)->mark($version->element, $document);
|
|
|
|
return $version;
|
|
}
|
|
|
|
/**
|
|
* Ein Dokument aus Kapiteln (Titel => Text; ein Titel mit führendem „-“ ist ein Unterkapitel).
|
|
*
|
|
* @param array<string, string> $chapters
|
|
*/
|
|
protected function document(array $chapters, string $title = 'Trainerhandbuch', ?Composition $composition = null): CompositionVersion
|
|
{
|
|
$composition ??= $this->documentComposition($title);
|
|
$members = [];
|
|
|
|
foreach ($chapters as $chapterTitle => $body) {
|
|
$sub = str_starts_with((string) $chapterTitle, '-');
|
|
$name = ltrim((string) $chapterTitle, '-');
|
|
$members[] = ['role' => $sub ? 'Unterkapitel' : 'Kapitel', 'target' => $this->chapter($composition, $name, $body)];
|
|
}
|
|
|
|
return app(ContentService::class)->publishComposition($composition, $members);
|
|
}
|
|
|
|
/** Ein Bild im Medienbestand mit echter Datei im abgeleiteten Bestand. */
|
|
protected function picture(string $title, int $width = 640, int $height = 360): ContentElementVersion
|
|
{
|
|
$content = app(ContentService::class);
|
|
$file = 'inhalte/'.str($title)->slug().'.png';
|
|
$image = imagecreatetruecolor($width, $height);
|
|
imagefill($image, 0, 0, (int) imagecolorallocate($image, 40, 120, 90));
|
|
ob_start();
|
|
imagepng($image);
|
|
Storage::disk('media_derived')->put($file, (string) ob_get_clean());
|
|
|
|
return $content->publishElementVersion($content->createElement('bild', 'bild', $title), null, ['file' => $file]);
|
|
}
|
|
|
|
protected function publishFor(string $code, CompositionVersion $root, ?LicenseArticle $article = null): array
|
|
{
|
|
$articles = app(ArticleService::class);
|
|
$article ??= $articles->create($code);
|
|
$release = $articles->publishRelease($article, $root, 'Stand '.$code.'.');
|
|
|
|
return [$article, $release];
|
|
}
|
|
|
|
protected function fullRights(): RightsProfileVersion
|
|
{
|
|
return $this->rights(['film' => ['streamen', 'laden'], 'text' => ['ansehen', 'laden'], 'document' => ['ansehen', 'laden'], 'bild' => ['ansehen', 'laden']]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, list<string>> $capabilities
|
|
*/
|
|
protected function rights(array $capabilities): RightsProfileVersion
|
|
{
|
|
return app(OfferService::class)->createRightsProfile('Profil '.md5(json_encode($capabilities)), 'ehrenamtlich', $capabilities);
|
|
}
|
|
|
|
/**
|
|
* @param list<LicenseArticle> $articles
|
|
*/
|
|
protected function memberWith(RightsProfileVersion $rights, array $articles): User
|
|
{
|
|
$offers = app(OfferService::class);
|
|
$offer = $offers->create('Angebot '.uniqid());
|
|
$offers->activate($offer, array_map(fn (LicenseArticle $article): array => ['target' => $article->fresh(), 'rights' => $rights], $articles), ['grant_kind' => 'auf_abruf']);
|
|
$user = User::factory()->create();
|
|
app(GrantIssuer::class)->claim($user, $offer);
|
|
|
|
return $user;
|
|
}
|
|
|
|
protected function documentPage(LicenseArticle $article, ArticleRelease $release, string $path = '1'): string
|
|
{
|
|
return route('member.document', ['article' => $article, 'release' => $release, 'path' => $path]);
|
|
}
|
|
|
|
/** Text der Seiten aus dem unkomprimierten Inhalt; Windows-1252 wird nach UTF-8 gewandelt. */
|
|
protected function pdfText(string $pdf): string
|
|
{
|
|
return implode("\n", $this->pdfPages($pdf));
|
|
}
|
|
|
|
/**
|
|
* Text je Seite. Die Seiteninhalte stehen in der Reihenfolge der Seiten im Dokument.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
protected function pdfPages(string $pdf): array
|
|
{
|
|
preg_match_all('/\/Type \/Page \/Parent 2 0 R.*?\/Contents (\d+) 0 R/s', $pdf, $contents);
|
|
$pages = [];
|
|
|
|
foreach ($contents[1] as $object) {
|
|
preg_match('/\n'.$object.' 0 obj\n<< \/Length \d+ >>\nstream\n(.*?)endstream/s', $pdf, $stream);
|
|
preg_match_all('/Td \((.*)\) Tj ET$/m', $stream[1] ?? '', $matches);
|
|
$pages[] = implode("\n", array_map(
|
|
fn (string $line): string => mb_convert_encoding(str_replace(['\\(', '\\)', '\\\\'], ['(', ')', '\\'], $line), 'UTF-8', 'Windows-1252'),
|
|
$matches[1],
|
|
));
|
|
}
|
|
|
|
return $pages;
|
|
}
|
|
}
|