<?php
namespace App\Controller;
use App\Services\ConsentApiService;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends FrontendController
{
/**
* @param ConsentApiService $consentApi
*/
public function __construct(
private ConsentApiService $consentApi
)
{
}
/**
* @Template
* @param Request $request
* @return array
*/
public function defaultAction(Request $request)
{
$documentId = $request->get('realDocument') ?: null;
$blogPostId = $request->get('blogPostId') ?: null;
$shouldFetchConsentData =
$request->get('routeDocument')?->getDocument()->getProperty('fetch_terms_and_conditions');
if ($documentId) {
return ['realDocument' => $documentId];
}
if ($blogPostId) {
return ['blogPostId' => $blogPostId,];
}
if ($shouldFetchConsentData) {
$consentName = $request->get('contentDocument')?->getEditables()['privola_name']->getData();
if (empty($consentName)) {
return [];
}
$language = $request->getLocale();
$consentData = $this->consentApi->getConsentData($consentName, $language);
if (array_key_exists('errors', $consentData)) {
return [];
}
return ['fetchData' => $consentData['data']];
}
return [];
}
}