Initial survey bundle import
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\Controller\FrontendModule;
|
||||
|
||||
use Contao\Config;
|
||||
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
|
||||
use Contao\CoreBundle\DependencyInjection\Attribute\AsFrontendModule;
|
||||
use Contao\CoreBundle\Twig\FragmentTemplate;
|
||||
use Contao\ModuleModel;
|
||||
use Mummert\SurveyBundle\Repository\SurveyRepository;
|
||||
use Mummert\SurveyBundle\Service\SurveyResultsViewService;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
#[AsFrontendModule(type: 'survey_results', category: 'survey', template: 'frontend/show_survey_results')]
|
||||
final class ShowSurveyResultsController extends AbstractFrontendModuleController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SurveyRepository $surveyRepository,
|
||||
private readonly SurveyResultsViewService $surveyResultsViewService,
|
||||
private readonly UrlGeneratorInterface $urlGenerator,
|
||||
) {
|
||||
}
|
||||
|
||||
protected function getResponse(FragmentTemplate $template, ModuleModel $model, Request $request): Response
|
||||
{
|
||||
$publicAlias = $this->resolvePublicAlias($request);
|
||||
|
||||
if ('' === $publicAlias) {
|
||||
$template->set('errorMessage', 'Es wurde kein Umfrage-Link auf der Ergebnisseite gefunden.');
|
||||
$template->set('survey', null);
|
||||
$template->set('questionResults', []);
|
||||
$template->set('completedSubmissionCount', 0);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
$survey = $this->surveyRepository->findByPublicAlias($publicAlias, false);
|
||||
|
||||
if (null === $survey) {
|
||||
$template->set('errorMessage', 'Die Umfrage wurde nicht gefunden.');
|
||||
$template->set('survey', null);
|
||||
$template->set('questionResults', []);
|
||||
$template->set('completedSubmissionCount', 0);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
if ($htmlHeadBag = $this->getHtmlHeadBag()) {
|
||||
$htmlHeadBag->setTitle((string) $survey->title.' - Ergebnisse');
|
||||
}
|
||||
|
||||
$resultsData = $this->surveyResultsViewService->buildForSurvey($survey);
|
||||
|
||||
$template->set('errorMessage', null);
|
||||
$template->set('survey', $survey);
|
||||
$template->set('completedSubmissionCount', $resultsData['completedSubmissionCount']);
|
||||
$template->set('downloadUrl', $this->buildDownloadUrl((string) $survey->alias));
|
||||
$template->set('pdfDownloadUrl', $this->hasGotenbergConfiguration() ? $this->buildPdfDownloadUrl((string) $survey->alias) : null);
|
||||
$template->set('questionResults', $resultsData['questionResults']);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
private function buildDownloadUrl(string $alias): string
|
||||
{
|
||||
return $this->urlGenerator->generate('mummert_survey_results_export', ['alias' => $alias]);
|
||||
}
|
||||
|
||||
private function buildPdfDownloadUrl(string $alias): string
|
||||
{
|
||||
return $this->urlGenerator->generate('mummert_survey_results_pdf_export', ['alias' => $alias]);
|
||||
}
|
||||
|
||||
private function hasGotenbergConfiguration(): bool
|
||||
{
|
||||
return '' !== trim((string) Config::get('surveyGotenbergUrl'));
|
||||
}
|
||||
|
||||
private function resolvePublicAlias(Request $request): string
|
||||
{
|
||||
$candidate = $request->attributes->get('auto_item');
|
||||
|
||||
if (\is_string($candidate) && '' !== trim($candidate)) {
|
||||
return trim($candidate);
|
||||
}
|
||||
|
||||
return trim((string) $request->query->get('auto_item', ''));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user