From 4d8afd53ad1d6661509319bb3f6caec880ec7f0a Mon Sep 17 00:00:00 2001 From: Jean-Francois Monfort Date: Thu, 6 May 2021 09:36:03 +0200 Subject: [PATCH] Form & save into DB --- .../Controller/ActivityController.php | 128 ++++++++---------- .../ChillActivityBundle/Entity/Activity.php | 70 +++------- .../ChillActivityBundle/Form/ActivityType.php | 3 + .../Resources/views/Activity/new.html.twig | 1 + .../config/routes/activity.yaml | 1 + .../migrations/Version20210506071150.php | 30 ++++ .../ChillMarkdownRenderExtension.php | 10 +- 7 files changed, 109 insertions(+), 134 deletions(-) create mode 100644 src/Bundle/ChillActivityBundle/migrations/Version20210506071150.php diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php index 036646104..a30e23532 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php @@ -121,80 +121,6 @@ class ActivityController extends AbstractController ]); } - /** - * Creates a new Activity entity. - * - */ - public function createAction($person_id, Request $request) - { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); - - if ($person === NULL) { - throw $this->createNotFoundException('person not found'); - } - - $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); - - $entity = new Activity(); - $entity->setPerson($person); - $form = $this->createCreateForm($entity); - $form->handleRequest($request); - - if ($form->isValid()) { - $em = $this->getDoctrine()->getManager(); - - $this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity, - 'creation of this activity not allowed'); - - $em->persist($entity); - $em->flush(); - - $this->get('session') - ->getFlashBag() - ->add('success', - $this->get('translator') - ->trans('Success : activity created!') - ); - - return $this->redirect( - $this->generateUrl('chill_activity_activity_show', - array('id' => $entity->getId(), 'person_id' => $person_id))); - } - - $this->get('session') - ->getFlashBag()->add('danger', - $this->get('translator') - ->trans('The form is not valid. The activity has not been created !') - ); - - return $this->render('ChillActivityBundle:Activity:new.html.twig', array( - 'entity' => $entity, - 'form' => $form->createView(), - 'person' => $person - )); - } - - /** - * Creates a form to create a Activity entity. - * - * @param Activity $entity The entity - * - * @return \Symfony\Component\Form\Form The form - */ - private function createCreateForm(Activity $entity): FormInterface - { - return $this->createForm(ActivityType::class, $entity, [ - 'action' => $this->generateUrl('chill_activity_activity_create', [ - 'person_id' => $entity->getPerson()->getId(), - ]), - 'method' => 'POST', - 'center' => $entity->getCenter(), - 'role' => new Role('CHILL_ACTIVITY_CREATE'), - 'activityType' => $entity->getType(), - ]); - } - /** * Displays a form to create a new Activity entity. * @@ -229,7 +155,23 @@ class ActivityController extends AbstractController $this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity); - $form = $this->createCreateForm($entity); + $form = $this->createForm(ActivityType::class, $entity, [ + 'center' => $entity->getCenter(), + 'role' => new Role('CHILL_ACTIVITY_CREATE'), + 'activityType' => $entity->getType(), + ])->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em->persist($entity); + $em->flush(); + + $this->addFlash('success', $this->get('translator')->trans('Success : activity created!')); + + return $this->redirectToRoute('chill_activity_activity_show', [ + 'id' => $entity->getId(), + 'person_id' => $person_id + ]); + } return $this->render('ChillActivityBundle:Activity:new.html.twig', array( 'person' => $person, @@ -238,6 +180,42 @@ class ActivityController extends AbstractController )); } + + /** + * Creates a new Activity entity. + * + */ + public function createAction($person_id, Request $request) + { + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); + + if ($person === NULL) { + throw $this->createNotFoundException('person not found'); + } + + $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); + + $entity = new Activity(); + $entity->setPerson($person); + $form = $this->createCreateForm($entity); + $form->handleRequest($request); + + + + $this->get('session') + ->getFlashBag()->add('danger', + $this->get('translator') + ->trans('The form is not valid. The activity has not been created !') + ); + + return $this->render('ChillActivityBundle:Activity:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + 'person' => $person + )); + } + /** * Finds and displays a Activity entity. * diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index 005644670..cd00e503a 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -69,9 +69,9 @@ class Activity implements HasCenterInterface, HasScopeInterface private \DateTime $date; /** - * @ORM\Column(type="time") + * @ORM\Column(type="time", nullable=true) */ - private \DateTime $durationTime; + private ?\DateTime $durationTime = null; /** * @ORM\Column(type="time", nullable=true) @@ -81,7 +81,7 @@ class Activity implements HasCenterInterface, HasScopeInterface /** * @ORM\ManyToOne(targetEntity="Chill\ActivityBundle\Entity\ActivityPresence") */ - private ActivityPresence $attendee; + private ?ActivityPresence $attendee = null; /** * @ORM\ManyToMany(targetEntity="Chill\ActivityBundle\Entity\ActivityReason") @@ -148,17 +148,11 @@ class Activity implements HasCenterInterface, HasScopeInterface $this->users = new ArrayCollection(); } - /** - * Get id - */ public function getId(): int { return $this->id; } - /** - * Set user - */ public function setUser(User $user): self { $this->user = $user; @@ -166,17 +160,11 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - /** - * Get user - */ public function getUser(): User { return $this->user; } - /** - * Set date - */ public function setDate(\DateTime $date): self { $this->date = $date; @@ -184,28 +172,19 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - /** - * Get date - */ public function getDate(): \DateTime { return $this->date; } - /** - * Set durationTime - */ - public function setDurationTime(\DateTime $durationTime): self + public function setDurationTime(?\DateTime $durationTime): self { $this->durationTime = $durationTime; return $this; } - /** - * Get durationTime - */ - public function getDurationTime(): \DateTime + public function getDurationTime(): ?\DateTime { return $this->durationTime; } @@ -229,17 +208,14 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - public function getAttendee(): ActivityPresence + public function getAttendee(): ?ActivityPresence { return $this->attendee; } - /** - * Add a reason - */ public function addReason(ActivityReason $reason): self { - $this->reasons[] = $reason; + $this->reasons->add($reason); return $this; } @@ -249,17 +225,18 @@ class Activity implements HasCenterInterface, HasScopeInterface $this->reasons->removeElement($reason); } - /** - * Get reasons - */ public function getReasons(): Collection { return $this->reasons; } - /** - * Set type - */ + public function setReasons(?ArrayCollection $reasons): self + { + $this->reasons = $reasons; + + return $this; + } + public function setType(ActivityType $type): self { $this->type = $type; @@ -267,17 +244,11 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - /** - * Get type - */ public function getType(): ActivityType { return $this->type; } - /** - * Set scope - */ public function setScope(Scope $scope): self { $this->scope = $scope; @@ -285,17 +256,11 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - /** - * Get scope - */ public function getScope(): ?Scope { return $this->scope; } - /** - * Set person - */ public function setPerson(Person $person): self { $this->person = $person; @@ -303,9 +268,6 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - /** - * Get person - */ public function getPerson(): Person { return $this->person; @@ -453,9 +415,9 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this->sentReceived; } - public function setSentReceived(string $sentReceived): self + public function setSentReceived(?string $sentReceived): self { - $this->sentReceived = $sentReceived; + $this->sentReceived = (string) $sentReceived; return $this; } diff --git a/src/Bundle/ChillActivityBundle/Form/ActivityType.php b/src/Bundle/ChillActivityBundle/Form/ActivityType.php index 2378c6db5..16e768bb2 100644 --- a/src/Bundle/ChillActivityBundle/Form/ActivityType.php +++ b/src/Bundle/ChillActivityBundle/Form/ActivityType.php @@ -134,6 +134,7 @@ class ActivityType extends AbstractType 'label' => $activityType->getLabel('reasons'), 'required' => $activityType->isRequired('reasons'), 'class' => ActivityReason::class, + 'multiple' => true, 'choice_label' => function (ActivityReason $activityReason) { return $this->translatableStringHelper->localize($activityReason->getName()); }, @@ -172,6 +173,7 @@ class ActivityType extends AbstractType 'label' => $activityType->getLabel('thirdParties'), 'required' => $activityType->isRequired('thirdParties'), 'class' => ThirdParty::class, + 'multiple' => true, 'choice_label' => function (ThirdParty $thirdParty) { return $thirdParty->getName(); }, @@ -189,6 +191,7 @@ class ActivityType extends AbstractType 'label' => $activityType->getLabel('users'), 'required' => $activityType->isRequired('users'), 'class' => User::class, + 'multiple' => true, 'choice_label' => function (User $user) { return $user->getUsername(); }, diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/new.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/new.html.twig index 11edc2c4c..1c88f9494 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/new.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/new.html.twig @@ -24,6 +24,7 @@

{{ "Activity creation"|trans }}

{{ form_start(form) }} + {{ form_errors(form) }} {{ form_row(form.user) }} {{ form_row(form.scope) }} diff --git a/src/Bundle/ChillActivityBundle/config/routes/activity.yaml b/src/Bundle/ChillActivityBundle/config/routes/activity.yaml index 342ccf7d3..7c7e2bb85 100644 --- a/src/Bundle/ChillActivityBundle/config/routes/activity.yaml +++ b/src/Bundle/ChillActivityBundle/config/routes/activity.yaml @@ -13,6 +13,7 @@ chill_activity_activity_select_type: chill_activity_activity_new: path: /{_locale}/person/{person_id}/activity/new controller: Chill\ActivityBundle\Controller\ActivityController::newAction + methods: [POST, GET] chill_activity_activity_create: path: /{_locale}/person/{person_id}/activity/create diff --git a/src/Bundle/ChillActivityBundle/migrations/Version20210506071150.php b/src/Bundle/ChillActivityBundle/migrations/Version20210506071150.php new file mode 100644 index 000000000..3a1a4e513 --- /dev/null +++ b/src/Bundle/ChillActivityBundle/migrations/Version20210506071150.php @@ -0,0 +1,30 @@ +addSql('ALTER TABLE activity ALTER durationtime DROP NOT NULL'); + } + + public function down(Schema $schema) : void + { + $this->addSql('ALTER TABLE activity ALTER durationTime SET NOT NULL'); + } +} diff --git a/src/Bundle/ChillMainBundle/Templating/ChillMarkdownRenderExtension.php b/src/Bundle/ChillMainBundle/Templating/ChillMarkdownRenderExtension.php index 4db950e6c..29d189155 100644 --- a/src/Bundle/ChillMainBundle/Templating/ChillMarkdownRenderExtension.php +++ b/src/Bundle/ChillMainBundle/Templating/ChillMarkdownRenderExtension.php @@ -31,13 +31,13 @@ final class ChillMarkdownRenderExtension extends AbstractExtension * @var Parsedown */ protected $parsedown; - + public function __construct() { $this->parsedown = new Parsedown(); $this->parsedown->setSafeMode(true); } - + public function getFilters(): array { return [ @@ -46,9 +46,9 @@ final class ChillMarkdownRenderExtension extends AbstractExtension ]) ]; } - - public function renderMarkdownToHtml(string $var): string + + public function renderMarkdownToHtml(?string $var): string { - return $this->parsedown->parse($var); + return $this->parsedown->parse((string) $var); } }