diff --git a/src/Controller/SettingsController.php b/src/Controller/SettingsController.php index 3626066..87b5bff 100644 --- a/src/Controller/SettingsController.php +++ b/src/Controller/SettingsController.php @@ -1,23 +1,56 @@ createNamedCustomForm("actorAdd", ActorAddForm::class); + // Get repositories + $entityManager = $this->getDoctrine()->getManager(); + /** @var ActorRepository $actorRepository */ + $actorRepository = $entityManager->getRepository(ActorEntity::class); - return $this->render("actors.html.twig", [ - "actorAddForm" => $actorAddFom, + // Actor add form + /** @var ActorAddForm $actorAddFom */ + $actorAddFom = $this->createNamedCustomForm('actorAdd', ActorAddForm::class); + $actorAddFom->handleRequest($request); + if ($actorAddFom->isSubmitted() && $actorAddFom->isValid()) { + $errors = $actorAddFom->validate(); + if (empty($errors)) { + $actor = $actorAddFom->getActor(); + $entityManager->persist($actor); + $entityManager->flush(); + + return $this->ajaxFormAnswer([ + 'refresh' => true, + ]); + } else { + return $this->ajaxFormAnswer([ + 'error' => join('
', $errors), + 'code' => Response::HTTP_BAD_REQUEST, + ]); + } + } + + // Get actors + $actors = $actorRepository->findAll(); + + return $this->render('actors.html.twig', [ + 'actorAddForm' => $actorAddFom, + 'actors' => $actors, ]); } } diff --git a/src/Entity/ActorEntity.php b/src/Entity/ActorEntity.php index 78f7075..bd1a713 100644 --- a/src/Entity/ActorEntity.php +++ b/src/Entity/ActorEntity.php @@ -8,13 +8,13 @@ { /** @var string The unique identifier of the entity */ - protected $id = ""; + protected $id = ''; /** @var string The first name of the actof */ - protected $firstName = ""; + protected $firstName = ''; /** @var string The last name of the actor */ - protected $lastName = ""; + protected $lastName = ''; /** @var string|null The optional nickname of the actor, if not set, the display name will be Firstname LASTNAME */ protected $nickname = null; @@ -118,7 +118,7 @@ */ public function getFullName(): string { - return printf("%s %s", ucfirst(strtolower($this->firstName)), strtoupper($this->lastName)); + return printf('%s %s', ucfirst(strtolower($this->firstName)), strtoupper($this->lastName)); } /** diff --git a/src/Form/ActorAddForm.php b/src/Form/ActorAddForm.php index f887505..2d62e4c 100644 --- a/src/Form/ActorAddForm.php +++ b/src/Form/ActorAddForm.php @@ -3,9 +3,28 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; +use App\Entity\ActorEntity; class ActorAddForm extends AbstractFormManager { + + /** + * Get the actor in the form data + * + * @return ActorEntity + */ + public function getActor(): ActorEntity + { + $data = $this->form->getData(); + $actor = new ActorEntity(); + $actor->generateId(); + $actor->setFirstName($data['firstName']); + $actor->setLastName($data['lastName']); + $actor->setNickname($data['nickname']); + + return $actor; + } + /** * {@inheritdoc} */ @@ -13,7 +32,7 @@ { return '_includes/html/form/actorAdd.html.twig'; } - + /** * {@inheritdoc} */ @@ -21,10 +40,13 @@ { $formBuilder->add('firstName', TextType::class, [ 'required' => true - ])->add('lastName', TextType::class, [ + ]) + ->add('lastName', TextType::class, [ 'required' => true - ])->add('nickname', TextType::class, [ + ]) + ->add('nickname', TextType::class, [ 'required' => false - ])->add('submit', SubmitType::class); + ]) + ->add('submit', SubmitType::class); } } \ No newline at end of file diff --git a/templates/actors.html.twig b/templates/actors.html.twig index 0069c88..1d58cea 100644 --- a/templates/actors.html.twig +++ b/templates/actors.html.twig @@ -12,7 +12,6 @@ - @@ -21,6 +20,16 @@ + {% for actor in actors %} + + + + + + + + {% endfor %}
ID Nom Prénom Surnom
{{ actor.firstName }}{{ actor.lastName }}{{ actor.nickname }}{% if actor.active %} {% endif %}