Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
5 changes: 5 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
26 changes: 26 additions & 0 deletions public/js/add-collection-widget.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion sql/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions src/Controller/AddEventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This file is part of the evenement package.
*
*/

namespace App\Controller;

use App\Db\ApplicationSchema\Event;
use App\Db\ApplicationSchema\EventModel;
use App\Form\Type\EventType;
use App\Form\Type\RegistrationType;
use App\Db\ApplicationSchema\ModelLayer\EventLayer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


/**
* @author Mikael Paris <stood86@gmail.com>
*/
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(),
));
}
}
2 changes: 1 addition & 1 deletion src/Controller/EditRegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/ListRegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace App\Controller;

use App\Db\ApplicationSchema\RegisterModel;
use PommProject\Foundation\Pomm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;


Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
62 changes: 62 additions & 0 deletions src/Db/ApplicationSchema/ModelLayer/EventLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This file is part of the evenement package.
*
*/

namespace App\Db\ApplicationSchema\ModelLayer;

use App\Db\ApplicationSchema\Event;
use App\Db\ApplicationSchema\EventModel;
use App\Db\ApplicationSchema\RegisterModel;
use PommProject\Foundation\Session\Connection;
use PommProject\ModelManager\ModelLayer\ModelLayer;


/**
* @author Mikael Paris <stood86@gmail.com>
*/
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;
}
}
78 changes: 78 additions & 0 deletions src/Form/EventListener/AddCategoryFieldSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* This file is part of the evenement package.
*
*/

namespace App\Form\EventListener;

use App\Db\ApplicationSchema\CategoryModel;
use App\Db\ApplicationSchema\EventModel;
use PommProject\Foundation\Pomm;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;


/**
* @author Mikael Paris <stood86@gmail.com>
*/
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();
}

}
56 changes: 56 additions & 0 deletions src/Form/Type/EventType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* This file is part of the evenement package.
*
*/

namespace App\Form\Type;

use App\Db\ApplicationSchema\Event;
use App\Db\ApplicationSchema\Register;
use App\Form\EventListener\AddCategoryFieldSubscriber;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Mikael Paris <stood86@gmail.com>
*/
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);
}
}
15 changes: 10 additions & 5 deletions src/Form/Type/RegistrationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stood86@gmail.com>
Expand All @@ -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']);
}
}
Loading