Form & save into DB

This commit is contained in:
Jean-Francois Monfort 2021-05-06 09:36:03 +02:00
parent 82d8556f24
commit 4d8afd53ad
7 changed files with 109 additions and 134 deletions

View File

@ -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.
*

View File

@ -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;
}

View File

@ -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();
},

View File

@ -24,6 +24,7 @@
<h2 class="chill-red">{{ "Activity creation"|trans }}</h2>
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.user) }}
{{ form_row(form.scope) }}

View File

@ -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

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Activity;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210506071150 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

View File

@ -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);
}
}