Skip to content

Commit

Permalink
Merge pull request #43 from dmolineus/feature/show-frozen-surveys
Browse files Browse the repository at this point in the history
Allow previewing non-frozen surveys
  • Loading branch information
m-vo authored Mar 18, 2021
2 parents 48cc647 + d647bb9 commit dd2dfa1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/Controller/SurveyFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\Template;
use Mvo\ContaoSurvey\Entity\Record;
Expand All @@ -35,15 +36,17 @@ class SurveyFragment extends AbstractContentElementController
private ScopeMatcher $scopeMatcher;
private Registry $registry;
private EventDispatcherInterface $eventDispatcher;
private TokenChecker $tokenChecker;
private bool $protectEditing;

public function __construct(SurveyRepository $surveyRepository, SurveyManagerFactory $managerFactory, ScopeMatcher $scopeMatcher, Registry $registry, EventDispatcherInterface $eventDispatcher, bool $protectEditing)
public function __construct(SurveyRepository $surveyRepository, SurveyManagerFactory $managerFactory, ScopeMatcher $scopeMatcher, Registry $registry, TokenChecker $tokenChecker, EventDispatcherInterface $eventDispatcher, bool $protectEditing)
{
$this->surveyRepository = $surveyRepository;
$this->managerFactory = $managerFactory;
$this->scopeMatcher = $scopeMatcher;
$this->registry = $registry;
$this->eventDispatcher = $eventDispatcher;
$this->tokenChecker = $tokenChecker;
$this->protectEditing = $protectEditing;
}

Expand All @@ -66,7 +69,7 @@ protected function getResponse(Template $template, ContentModel $model, Request
]);
}

if ($this->protectEditing && !$survey->isFrozen()) {
if ($this->protectEditing && !$survey->isFrozen() && !$this->tokenChecker->isPreviewMode()) {
return $this->render('@MvoContaoSurvey/_frozen.html.twig', [
'headline' => $model->survey_headline ?: $survey->getTitle(),
'survey' => $survey,
Expand All @@ -78,7 +81,7 @@ protected function getResponse(Template $template, ContentModel $model, Request

$manager->form->handleRequest($request);

if ($this->proceedUntilCompleted($manager)) {
if ($this->proceedUntilCompleted($manager) && (!$this->protectEditing || $survey->isFrozen())) {
$record = new Record($survey, $manager->getAnswers());
$manager->reset();
$this->eventDispatcher->dispatch(new SurveySubmittedEvent($survey, $record, $model));
Expand Down
5 changes: 4 additions & 1 deletion src/Form/SurveyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ class SurveyManager

private int $currentStep;
private int $totalSteps;
private bool $protectEditing;

public function __construct(Survey $survey, FormFactoryInterface $formFactory, Registry $registry, NamespacedAttributeBag $storage, SessionInterface $session)
public function __construct(Survey $survey, FormFactoryInterface $formFactory, Registry $registry, NamespacedAttributeBag $storage, SessionInterface $session, bool $protectEditing)
{
$this->formFactory = $formFactory;
$this->registry = $registry;
$this->storage = $storage;
$this->session = $session;
$this->protectEditing = $protectEditing;

$this->bind($survey);
}
Expand Down Expand Up @@ -228,6 +230,7 @@ private function bind(Survey $survey): void
[
'first_step' => $this->isFirstStep(),
'last_step' => $this->isLastStep(),
'protect_editing' => $this->protectEditing && !$survey->isFrozen(),
]
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Form/SurveyManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class SurveyManagerFactory
private Registry $registry;
private NamespacedAttributeBag $namespacedAttributeBag;
private SessionInterface $session;
private bool $protectEditing;

public function __construct(FormFactoryInterface $formFactory, Registry $registry, NamespacedAttributeBag $namespacedAttributeBag, SessionInterface $session)
public function __construct(FormFactoryInterface $formFactory, Registry $registry, NamespacedAttributeBag $namespacedAttributeBag, SessionInterface $session, bool $protectEditing)
{
$this->formFactory = $formFactory;
$this->registry = $registry;
$this->namespacedAttributeBag = $namespacedAttributeBag;
$this->session = $session;
$this->protectEditing = $protectEditing;
}

public function __invoke(Survey $survey): SurveyManager
Expand All @@ -37,7 +39,8 @@ public function __invoke(Survey $survey): SurveyManager
$this->formFactory,
$this->registry,
$this->namespacedAttributeBag,
$this->session
$this->session,
$this->protectEditing
);
}
}
10 changes: 10 additions & 0 deletions src/Form/SurveyStepFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Valid;

Expand Down Expand Up @@ -62,9 +64,17 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults([
'first_step' => false,
'last_step' => false,
'protect_editing' => true,

// We're already using Contao's CSRF token mechanism
'csrf_protection' => false,
]);
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
parent::buildView($view, $form, $options);

$view->vars['protect_editing'] = $options['protect_editing'];
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ services:
- '@mvo.survey.registry'
- '@mvo.survey.session_storage'
- '@session'
- '%mvo_survey.protect_editing%'

# form types
mvo.survey.form.type.survey_step:
Expand Down Expand Up @@ -162,6 +163,7 @@ services:
- '@mvo.survey.form.manager_factory'
- '@contao.routing.scope_matcher'
- '@mvo.survey.registry'
- '@contao.security.token_checker'
- '@event_dispatcher'
- '%mvo_survey.protect_editing%'
tags:
Expand Down
10 changes: 6 additions & 4 deletions src/Resources/views/Step/Navigation.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
{% endif %}

{% if current_step.is_last %}
{{ form_widget(form.next, {
label: 'form.button.submit'|trans(),
attr: {class: button_classes}
}) }}
{% if not form.vars.protect_editing %}
{{ form_widget(form.next, {
label: 'form.button.submit'|trans(),
attr: {class: button_classes}
}) }}
{% endif %}
{% else %}
{{ form_widget(form.next, {
label: 'form.button.next'|trans(),
Expand Down

0 comments on commit dd2dfa1

Please sign in to comment.