diff --git a/config/routes.yaml b/config/routes.yaml index d957801..2681c1f 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -2,6 +2,10 @@ index: path: / defaults: { _controller: 'App\Controller\HomeController::hello' } +event_add: + path: /event/add + defaults: { _controller: 'App\Controller\AddEventController::process' } + event_list: path: /events defaults: { _controller: 'App\Controller\ListEventController::show' } diff --git a/config/services.yaml b/config/services.yaml index 7b2b40e..5b49fd0 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -27,6 +27,11 @@ services: # please note that last definitions always *replace* previous ones App\Form\EventListener\AddEventFieldSubscriber: + arguments: ['@pomm'] + tags: + - { name: kernel.event_subscriber} + + App\Form\EventListener\AddCategoryFieldSubscriber: arguments: ['@pomm'] tags: - { name: kernel.event_subscriber} \ No newline at end of file diff --git a/public/js/add-collection-widget.js b/public/js/add-collection-widget.js new file mode 100644 index 0000000..1f7ac6e --- /dev/null +++ b/public/js/add-collection-widget.js @@ -0,0 +1,26 @@ +jQuery(document).ready(function () { + jQuery('.add-another-collection-widget').click(function (e) { + e.preventDefault(); + var list = jQuery(jQuery(this).attr('data-list')); + + // Try to find the counter of the list + var counter = list.data('widget-counter') | list.children().length; + // If the counter does not exist, use the length of the list + if (!counter) { counter = list.children().length; } + + // grab the prototype template + var newWidget = list.data('prototype'); + // replace the "__name__" used in the id and name of the prototype + // with a number that's unique to your emails + // end name attribute looks like name="contact[emails][2]" + newWidget = newWidget.replace(/__name__/g, counter); + // Increase the counter + counter++; + // And store it, the length cannot be used if deleting widgets is allowed + list.data(' widget-counter', counter); + + // create a new list element and add it to the list + var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget); + newElem.appendTo(list); + }); +}); \ No newline at end of file diff --git a/sql/database.sql b/sql/database.sql index c284a3f..412216c 100644 --- a/sql/database.sql +++ b/sql/database.sql @@ -26,7 +26,7 @@ COMMENT ON TABLE application.event IS 'Event'; CREATE TABLE application.register ( register_id SERIAL PRIMARY KEY, - event_id INTEGER NOT NULL REFERENCES application.event, + event_id INTEGER NOT NULL REFERENCES application.event DEFERRABLE, created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT now(), updated_at TIMESTAMP WITHOUT TIME ZONE NULL, lastname VARCHAR(255) NOT NULL, diff --git a/src/Controller/AddEventController.php b/src/Controller/AddEventController.php new file mode 100644 index 0000000..a29890f --- /dev/null +++ b/src/Controller/AddEventController.php @@ -0,0 +1,49 @@ + + */ +class AddEventController extends Controller +{ + public function process(Request $request) + { + $event = new Event([ + 'name' => ['fr' => '', 'en' => ''], + 'category' => null, + 'registrations' => [] + ]); + + $form = $this->createForm(EventType::class, $event); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + + $eventLayer = $this->get('pomm') + ->getDefaultSession() + ->getModelLayer(EventLayer::class); + + $eventLayer->createEvent($event); + + return $this->redirectToRoute('event_list'); + } + + return $this->render('event/create.html.twig', array( + 'form' => $form->createView(), + )); + } +} \ No newline at end of file diff --git a/src/Controller/EditRegistrationController.php b/src/Controller/EditRegistrationController.php index 902a02c..ac2c5e2 100644 --- a/src/Controller/EditRegistrationController.php +++ b/src/Controller/EditRegistrationController.php @@ -24,7 +24,7 @@ public function process(Request $request, $registerId) ->getModel(RegisterModel::class) ->findWithEvent($registerId); - $form = $this->createForm(RegistrationType::class, $register); + $form = $this->createForm(RegistrationType::class, $register, ['active_subscriber' => true]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { diff --git a/src/Controller/ListRegistrationController.php b/src/Controller/ListRegistrationController.php index e4b4b24..d2bf60f 100644 --- a/src/Controller/ListRegistrationController.php +++ b/src/Controller/ListRegistrationController.php @@ -7,6 +7,7 @@ namespace App\Controller; use App\Db\ApplicationSchema\RegisterModel; +use PommProject\Foundation\Pomm; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -15,9 +16,9 @@ */ class ListRegistrationController extends Controller { - public function show() + public function show(Pomm $pomm) { - $registers = $this->get('pomm') + $registers = $pomm ->getDefaultSession() ->getModel(RegisterModel::class) ->findAll(); diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 0b1fc4d..19bb80a 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -27,7 +27,7 @@ public function process(Request $request) 'email' => null ]); - $form = $this->createForm(RegistrationType::class, $register); + $form = $this->createForm(RegistrationType::class, $register, ['active_subscriber' => true]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { diff --git a/src/Db/ApplicationSchema/ModelLayer/EventLayer.php b/src/Db/ApplicationSchema/ModelLayer/EventLayer.php new file mode 100644 index 0000000..5c7004a --- /dev/null +++ b/src/Db/ApplicationSchema/ModelLayer/EventLayer.php @@ -0,0 +1,62 @@ + + */ +class EventLayer extends ModelLayer +{ + public function createEvent(Event $event) + { + $this->startTransaction(); + try { + $this->setDeferrable(['application.register_event_id_fkey'], Connection::CONSTRAINTS_DEFERRED); + + $registrations = []; + + foreach ($event->getRegistrations() as $registration) { + $registration['event_id'] = '000'; + + $registrationPersist = $this->getModel(RegisterModel::class) + ->createAndSave($registration); + + $registrations[] = $registrationPersist; + } + + $this->getModel(EventModel::class) + ->insertOne($event); + + foreach ($registrations as $registration) { + $registration->setEventId($event->getEventId()); + + $this->getModel(RegisterModel::class) + ->updateOne( + $registration, + ['event_id'] + ); + } + + $event->set('registrations', $registrations); + + $this->commitTransaction(); + } catch (\Exception $e) { + $this->rollbackTransaction(); + + throw $e; + } + + return $event; + } +} \ No newline at end of file diff --git a/src/Form/EventListener/AddCategoryFieldSubscriber.php b/src/Form/EventListener/AddCategoryFieldSubscriber.php new file mode 100644 index 0000000..75851e5 --- /dev/null +++ b/src/Form/EventListener/AddCategoryFieldSubscriber.php @@ -0,0 +1,78 @@ + + */ +class AddCategoryFieldSubscriber implements EventSubscriberInterface +{ + protected $pomm; + + public function __construct(Pomm $pomm) + { + $this->pomm = $pomm; + } + + public static function getSubscribedEvents() + { + return [ + FormEvents::PRE_SET_DATA => 'preSetData', + FormEvents::POST_SUBMIT => 'postSubmit' + ]; + } + + public function preSetData(FormEvent $event) + { + $form = $event->getForm(); + + $form->add('category', ChoiceType::class, [ + 'label' => 'Catégorie', + 'choices' => $this->getCategorys(), + 'choice_label' => function($elt) { + return $elt->getName()['fr']; + }, + 'placeholder' => 'Choisir une catégorie', + 'choice_value' => function($elt) { + if ($elt !== null) { + return $elt->getCategoryId(); + } + }, + 'required' => true, + 'attr' => ['class'=>'form-control'] + ]); + } + + public function postSubmit(FormEvent $event) + { + $data = $event->getData(); + + if ($data->getCategory() != null) { + $data->set('category_id', $data->getCategory()->getCategoryId()); + } + + return; + } + + private function getCategorys() + { + return $this->pomm + ->getDefaultSession() + ->getModel(CategoryModel::class) + ->findAll(); + } + +} \ No newline at end of file diff --git a/src/Form/Type/EventType.php b/src/Form/Type/EventType.php new file mode 100644 index 0000000..bd99008 --- /dev/null +++ b/src/Form/Type/EventType.php @@ -0,0 +1,56 @@ + + */ +class EventType extends AbstractType +{ + protected $subscriber; + + public function __construct(AddCategoryFieldSubscriber $subscriber) + { + $this->subscriber = $subscriber; + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('name', CollectionType::class, + [ + 'entry_type' => TextType::class, + 'label' => 'Nom' + ]) + ->add('registrations', CollectionType::class, + [ + 'entry_type' => RegistrationType::class, + 'entry_options' => [ + 'active_subscriber' => false + ], + 'label' => 'Inscris', + 'prototype' => true, + 'allow_add' => true, + 'prototype_data' => new Register([ + 'lastname' => null, + 'firstname' => null, + 'email' => null + ]) + ]); + + $builder->addEventSubscriber($this->subscriber); + } +} \ No newline at end of file diff --git a/src/Form/Type/RegistrationType.php b/src/Form/Type/RegistrationType.php index a33da7c..51e3267 100644 --- a/src/Form/Type/RegistrationType.php +++ b/src/Form/Type/RegistrationType.php @@ -6,12 +6,13 @@ namespace App\Form\Type; +use App\Db\ApplicationSchema\Register; use App\Form\EventListener\AddEventFieldSubscriber; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\EmailType; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; /** * @author Mikael Paris @@ -30,11 +31,15 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder ->add('lastname', TextType::class, ['label' => 'Nom']) ->add('firstname', TextType::class, ['label' => 'Prénom']) - ->add('email', EmailType::class, ['label' => 'Email']) - ->add('save', SubmitType::class, array('label' => 'Enregistrement')); - - $builder->addEventSubscriber($this->subscriber); + ->add('email', EmailType::class, ['label' => 'Email']); + if ($options['active_subscriber']) { + $builder->addEventSubscriber($this->subscriber); + } } + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setRequired(['active_subscriber']); + } } \ No newline at end of file diff --git a/templates/event/create.html.twig b/templates/event/create.html.twig new file mode 100644 index 0000000..d330000 --- /dev/null +++ b/templates/event/create.html.twig @@ -0,0 +1,27 @@ +{% extends "base.html.twig" %} +{% block content_page %} +

Evenement

+ {{ form_start(form) }} + {{ form_row(form.name) }} + {{ form_row(form.category) }} + + + Add another registration + + {{ form_end(form) }} +{% endblock %} +{% block javascripts %} + + +{% endblock %} \ No newline at end of file diff --git a/templates/event/list.html.twig b/templates/event/list.html.twig index 5e3f1de..3015d89 100644 --- a/templates/event/list.html.twig +++ b/templates/event/list.html.twig @@ -1,6 +1,6 @@ {% extends "base.html.twig" %} {% block content_page %} - + Ajout Evénement

Liste des événements :

diff --git a/templates/register/create.html.twig b/templates/register/create.html.twig index e72246e..469c3f5 100644 --- a/templates/register/create.html.twig +++ b/templates/register/create.html.twig @@ -1,5 +1,9 @@ {% extends "base.html.twig" %} {% block content_page %}

Inscription

- {{ form(form) }} + + {{ form_start(form) }} + {{ form_row(form) }} + + {{ form_end(form) }} {% endblock %} \ No newline at end of file diff --git a/templates/register/edit.html.twig b/templates/register/edit.html.twig index e5ebe34..7915018 100644 --- a/templates/register/edit.html.twig +++ b/templates/register/edit.html.twig @@ -1,5 +1,8 @@ {% extends "base.html.twig" %} {% block content_page %}

Edition

- {{ form(form) }} + {{ form_start(form) }} + {{ form_row(form) }} + + {{ form_end(form) }} {% endblock %} \ No newline at end of file