Skip to content

Commit

Permalink
Merge pull request #42 from dmolineus/feature/recorded-event
Browse files Browse the repository at this point in the history
Dispatch an event when a survey is submitted
  • Loading branch information
m-vo authored Mar 18, 2021
2 parents 7bddc3e + 4ef804e commit 48cc647
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 17 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"symfony/translation-contracts": "^2.3",
"symfony/twig-bundle": "^4.4|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
"symfony/event-dispatcher": "^4.4|^5.0",
"symfony/event-dispatcher-contracts": "^1.1|^2.0",
"symfony/expression-language": "^4.4|^5.0",
"symfony/form": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
Expand Down
22 changes: 7 additions & 15 deletions src/Controller/SurveyFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\Template;
use Doctrine\ORM\EntityManager;
use Mvo\ContaoSurvey\Entity\Record;
use Mvo\ContaoSurvey\Entity\Survey;
use Mvo\ContaoSurvey\Event\SurveySubmittedEvent;
use Mvo\ContaoSurvey\Form\SurveyManager;
use Mvo\ContaoSurvey\Form\SurveyManagerFactory;
use Mvo\ContaoSurvey\Registry;
use Mvo\ContaoSurvey\Repository\SurveyRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @ContentElement(category="includes")
Expand All @@ -32,17 +33,17 @@ class SurveyFragment extends AbstractContentElementController
private SurveyRepository $surveyRepository;
private SurveyManagerFactory $managerFactory;
private ScopeMatcher $scopeMatcher;
private EntityManager $entityManager;
private Registry $registry;
private EventDispatcherInterface $eventDispatcher;
private bool $protectEditing;

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

Expand Down Expand Up @@ -78,8 +79,9 @@ protected function getResponse(Template $template, ContentModel $model, Request
$manager->form->handleRequest($request);

if ($this->proceedUntilCompleted($manager)) {
$this->storeRecord($survey, $manager->getAnswers());
$record = new Record($survey, $manager->getAnswers());
$manager->reset();
$this->eventDispatcher->dispatch(new SurveySubmittedEvent($survey, $record, $model));

return $this->render('@MvoContaoSurvey/_thanks.html.twig', [
'headline' => $headline,
Expand Down Expand Up @@ -143,14 +145,4 @@ private function proceedUntilCompleted(SurveyManager $manager): bool
// form contains errors
return false;
}

private function storeRecord(Survey $survey, array $answers): void
{
$record = new Record($survey, $answers);

// todo validate

$this->entityManager->persist($record);
$this->entityManager->flush();
}
}
10 changes: 10 additions & 0 deletions src/Entity/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class Survey extends DcaDefault
*/
private string $buttonLabel = '';

/**
* @ORM\Column(name="store_records", type="boolean", options={"default": true})
*/
private bool $storeRecords = true;

/**
* @ORM\OneToMany(targetEntity="Section", mappedBy="survey")
*
Expand Down Expand Up @@ -132,6 +137,11 @@ public function isFrozen(): bool
return $this->frozen;
}

public function isStoreRecords(): bool
{
return $this->storeRecords;
}

public function clearRecords(): void
{
$this->records->clear();
Expand Down
51 changes: 51 additions & 0 deletions src/Event/SurveySubmittedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* @author Moritz Vondano
* @license MIT
*/

namespace Mvo\ContaoSurvey\Event;

use Contao\Model;
use Mvo\ContaoSurvey\Entity\Record;
use Mvo\ContaoSurvey\Entity\Survey;

/**
* @psalm-immutable
*/
class SurveySubmittedEvent
{
private Survey $survey;

private Record $record;

private Model $context;

public function __construct(Survey $survey, Record $record, Model $context)
{
$this->survey = $survey;
$this->record = $record;
$this->context = $context;
}

public function getSurvey(): Survey
{
return $this->survey;
}

public function getRecord(): Record
{
return $this->record;
}

/**
* Get the context where the survey was stored. Usually a content element or module model.
*/
public function getContext(): Model
{
return $this->context;
}
}
1 change: 1 addition & 0 deletions src/EventListener/DataContainer/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function listSurveys(array $row, string $label): string
'count' => $submittedRecordsCount,
'frozen' => (bool) $row['frozen'],
'protect_editing' => $this->protectEditing,
'store_records' => (bool) $row['store_records'],
]
);
}
Expand Down
37 changes: 37 additions & 0 deletions src/EventListener/StoreRecordListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* @author Moritz Vondano
* @license MIT
*/

namespace Mvo\ContaoSurvey\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use Mvo\ContaoSurvey\Event\SurveySubmittedEvent;
use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTag;

class StoreRecordListener
{
private EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @ServiceTag(name="kernel_event_listener", event="Mvo\ContaoSurvey\Event\SurveySubmittedEvent", priority="128")
*/
public function __invoke(SurveySubmittedEvent $event): void
{
if (!$event->getSurvey()->isStoreRecords()) {
return;
}

$this->entityManager->persist($event->getRecord());
$this->entityManager->flush();
}
}
9 changes: 8 additions & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ services:
tags:
- { name: 'kernel.event_listener', event: 'kernel.request', priority: -800 }

mvo.survey.listener.store_record:
class: Mvo\ContaoSurvey\EventListener\StoreRecordListener
arguments:
- '@doctrine.orm.entity_manager'
tags:
- { name: 'kernel.event_listener', event: 'Mvo\ContaoSurvey\Event\SurveySubmittedEvent', priority: 128 }

mvo.survey.form.manager_factory:
class: Mvo\ContaoSurvey\Form\SurveyManagerFactory
arguments:
Expand Down Expand Up @@ -154,8 +161,8 @@ services:
- '@mvo.survey.repository.survey'
- '@mvo.survey.form.manager_factory'
- '@contao.routing.scope_matcher'
- '@doctrine.orm.entity_manager'
- '@mvo.survey.registry'
- '@event_dispatcher'
- '%mvo_survey.protect_editing%'
tags:
- { name: 'terminal42_service_annotation' }
Expand Down
13 changes: 12 additions & 1 deletion src/Resources/contao/dca/tl_survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
],
'palettes' => [
'__selector__' => [],
'default' => 'title,frozen;{details_legend},note_submission,button_label,button_href',
'default' => 'title,frozen,store_records;{details_legend},note_submission,button_label,button_href',
],
'subpalettes' => [
],
Expand Down Expand Up @@ -93,5 +93,16 @@
// Keep this for MySQL Strict mode. Otherwise Contao would save an empty string
'sql' => ['type' => 'boolean', 'default' => false],
],
'store_records' => [
'inputType' => 'checkbox',
'filter' => true,
'eval' => [
'tl_class' => 'clr w50 m12',
],
'default' => true,
'save_callback' => [static fn ($v) => '1' === $v],
// Keep this for MySQL Strict mode. Otherwise Contao would save an empty string
'sql' => ['type' => 'boolean', 'default' => true],
],
],
];
1 change: 1 addition & 0 deletions src/Resources/contao/languages/de/tl_survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

$GLOBALS['TL_LANG']['tl_survey']['title'] = ['Titel', 'Geben Sie den Titel dieser Umfrage ein.'];
$GLOBALS['TL_LANG']['tl_survey']['frozen'] = ['Umfrage einfrieren', 'Bearbeitung dieser Umfrage sperren. Nur eingefrorene Umfragen werden im Frontend ausgegeben.'];
$GLOBALS['TL_LANG']['tl_survey']['store_records'] = ['Ergebnisse speichern', 'Ergebnisse werden in der Datenbank gespeichert.'];
$GLOBALS['TL_LANG']['tl_survey']['details_legend'] = 'Details zur Danke-Seite';
$GLOBALS['TL_LANG']['tl_survey']['note_submission'] = ['Nachricht', 'Fügen Sie eine "Danke"-Nachricht hinzu, die nach erfolgreichem Absenden angezeigt wird.'];
$GLOBALS['TL_LANG']['tl_survey']['button_label'] = ['Button Bezeichner', 'Fügen Sie einen optionalen Button hinzu.'];
Expand Down
1 change: 1 addition & 0 deletions src/Resources/contao/languages/en/tl_survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

$GLOBALS['TL_LANG']['tl_survey']['title'] = ['Title', 'Enter the title of this survey.'];
$GLOBALS['TL_LANG']['tl_survey']['frozen'] = ['Freeze survey', 'Lock editing of this survey. Only frozen surveys will be output in the frontend.'];
$GLOBALS['TL_LANG']['tl_survey']['store_records'] = ['Store records', 'Store submitted records in the database.'];
$GLOBALS['TL_LANG']['tl_survey']['details_legend'] = 'Submission details';
$GLOBALS['TL_LANG']['tl_survey']['note_submission'] = ['Submission note', 'Add a "thank you" message that will be shown after successful submission.'];
$GLOBALS['TL_LANG']['tl_survey']['button_label'] = ['Button label', 'Add an optional button.'];
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/views/Backend/survey_record.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
{% endif %}
<div>
<div>{{ name }}</div>
{% if store_records or count > 0 %}
<div class="survey_records survey_flex-col">
<div class="survey_records_count">
{{ 'records.count'|trans({'%count%': count}) }}
Expand All @@ -45,5 +46,6 @@
</a>
</div>
</div>
{% endif %}
</div>
</div>

0 comments on commit 48cc647

Please sign in to comment.