<?php
namespace App\Controller\ContentElement;
use Contao\BackendTemplate;
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\File;
use Contao\FilesModel;
use Contao\Model;
use Contao\StringUtil;
use Contao\Template;
use Hofff\Contao\Consent\Bridge\ConsentId\ConsentIdParser;
use Hofff\Contao\Consent\Bridge\ConsentToolManager;
use Hofff\Contao\Consent\Bridge\Render\RenderInformation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* @ContentElement("video_hero", category="media")
*/
class VideoHero extends AbstractContentElementController
{
private ScopeMatcher $scopeMatcher;
private ConsentToolManager $consentToolManager;
private ConsentIdParser $consentIdParser;
public function __construct(
ScopeMatcher $scopeMatcher,
ConsentToolManager $consentToolManager,
ConsentIdParser $consentIdParser
) {
$this->scopeMatcher = $scopeMatcher;
$this->consentToolManager = $consentToolManager;
$this->consentIdParser = $consentIdParser;
}
protected function getResponse(Template $template, ContentModel $model, Request $request): ?Response
{
if ($this->scopeMatcher->isBackendRequest($request)) {
$template = new BackendTemplate('be_wildcard');
$template->wildcard = '### VIDEO HERO ###';
return new Response($template->parse());
}
// Add the video file to the template
$template->file = $this->videoFile($model, $request->getLocale());
// Add the youtube player to the template
$template->player = $this->playerTemplate($template, $model);
try {
return $template->getResponse();
} catch (\Exception $objError) {
return null;
}
}
private function videoFile(ContentModel $model, string $locale): ?File
{
if (!$model->playerSRC) {
return null;
}
$source = StringUtil::deserialize($model->playerSRC);
if (empty($source) || !\is_array($source)) {
return null;
}
$files = FilesModel::findMultipleByUuidsAndExtensions($source, ['mp4', 'm4v', 'mov', 'wmv', 'webm']);
if (null === $files) {
return null;
}
$videoModel = $files->current();
$videoFile = new File($videoModel->path);
$videoFile->title = StringUtil::specialchars($this->getMetaTitle($videoModel, $locale));
return $videoFile;
}
private function playerTemplate(Template $template, ContentModel $model): string
{
$playerTemplate = clone $template;
try {
$player = $playerTemplate->parse();
} catch (\Exception $objError) {
return '';
}
$consentTool = $this->consentToolManager->activeConsentTool();
if (null === $consentTool || !$model->hofff_consent_bridge_tag) {
return $player;
}
$consentId = $this->consentIdParser->parse($model->hofff_consent_bridge_tag);
if (!$consentTool->requiresConsent($consentId)) {
return $player;
}
$renderInformation = RenderInformation::autoRenderWithPlaceholder('hofff_consent_placeholder_youtube');
$player = $consentTool->renderContent(
$playerTemplate->parse(),
$consentId,
$model,
$renderInformation->placeholderTemplate()
);
return $player;
}
private function getMetaTitle(Model $videoModel, string $locale): string
{
$arrMeta = StringUtil::deserialize($videoModel->meta);
if (\is_array($arrMeta) && isset($arrMeta[$locale])) {
$title = $arrMeta[$locale]['title'];
} else {
$title = $videoModel->name;
}
return $title;
}
}