77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Mummert\FlipbookBundle\Controller\ContentElement;
|
|
|
|
use Contao\ContentModel;
|
|
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
|
|
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
|
|
use Contao\CoreBundle\Twig\FragmentTemplate;
|
|
use Contao\FilesModel;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
#[AsContentElement(type: 'blatterbares_pdf', category: 'media', template: 'content_element/blatterbares_pdf')]
|
|
class BlatterbaresPdfController extends AbstractContentElementController
|
|
{
|
|
protected function getResponse(FragmentTemplate $template, ContentModel $model, Request $request): Response
|
|
{
|
|
$template->set('pdfUrl', $this->resolvePdfUrl($model));
|
|
$template->set('showNavigation', '1' === (string) ($model->flipbookShowNavigation ?? '1'));
|
|
$template->set('playTurnSound', '1' === (string) ($model->flipbookPlaySound ?? '1'));
|
|
$template->set('splitSpreads', '1' === (string) ($model->flipbookSplitSpreads ?? '0'));
|
|
$template->set('initialRenderPages', $this->normalizeInitialRenderPages((string) ($model->flipbookInitialPages ?? '4')));
|
|
$template->set('startMode', $this->normalizeStartMode((string) ($model->flipbookStartMode ?? 'center')));
|
|
|
|
return $template->getResponse();
|
|
}
|
|
|
|
private function resolvePdfUrl(ContentModel $model): string
|
|
{
|
|
$uuid = (string) ($model->flipbookPdfSrc ?? '');
|
|
|
|
if ('' === $uuid) {
|
|
return '';
|
|
}
|
|
|
|
$fileModel = FilesModel::findByUuid($uuid);
|
|
|
|
if (null === $fileModel) {
|
|
return '';
|
|
}
|
|
|
|
$path = trim((string) $fileModel->path, '/');
|
|
|
|
if ('' === $path || !str_ends_with(strtolower($path), '.pdf')) {
|
|
return '';
|
|
}
|
|
|
|
return '/'.implode('/', array_map('rawurlencode', explode('/', $path)));
|
|
}
|
|
|
|
private function normalizeInitialRenderPages(string $value): int
|
|
{
|
|
$count = (int) $value;
|
|
|
|
if ($count < 2) {
|
|
return 2;
|
|
}
|
|
|
|
if ($count > 4) {
|
|
return 4;
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
private function normalizeStartMode(string $value): string
|
|
{
|
|
if ('spread' === $value || 'cover' === $value) {
|
|
return 'spread';
|
|
}
|
|
|
|
return 'center';
|
|
}
|
|
}
|