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 $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> $capabilities */ protected function rights(array $capabilities): RightsProfileVersion { return app(OfferService::class)->createRightsProfile('Profil '.md5(json_encode($capabilities)), 'ehrenamtlich', $capabilities); } /** * @param list $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 */ 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; } }