diff --git a/public/scripts/ajaxForm.js b/public/scripts/ajaxForm.js index 892473e..9121fe1 100644 --- a/public/scripts/ajaxForm.js +++ b/public/scripts/ajaxForm.js @@ -41,8 +41,8 @@ || confirm(event.target.getAttribute("data-form-confirmation"))) ) { // Get form data - const data = new URLSearchParams(); - for (const pair of new FormData(event.target)) { + let data = new URLSearchParams(); + for (let pair of new FormData(event.target)) { let pairKey = pair[0]; let pairValue = pair[1]; data.append(pairKey, pairValue); diff --git a/public/styles/form.css b/public/styles/form.css index aabfcaf..257ddc1 100644 --- a/public/styles/form.css +++ b/public/styles/form.css @@ -68,6 +68,15 @@ top: 0.1em; } +.icon-button, +.icon-button:hover { + border: none; + background: none; + box-shadow: none; + margin: 0; + padding: 0; +} + label { font-weight: bold; margin-bottom: 0; diff --git a/src/Controller/SettingsController.php b/src/Controller/SettingsController.php index 9dbfd14..f20a7a2 100644 --- a/src/Controller/SettingsController.php +++ b/src/Controller/SettingsController.php @@ -6,6 +6,7 @@ use App\Form\ActorAddForm; use App\Repository\ActorRepository; use App\Entity\ActorEntity; +use App\Form\ActorDeleteForm; class SettingsController extends AbstractExtendedController { @@ -22,7 +23,7 @@ $entityManager = $this->getDoctrine()->getManager(); /** @var ActorRepository $actorRepository */ $actorRepository = $entityManager->getRepository(ActorEntity::class); - + // Actor add form /** @var ActorAddForm $actorAddFom */ $actorAddFom = $this->createNamedCustomForm('actorAdd', ActorAddForm::class); @@ -33,7 +34,7 @@ $actor = $actorAddFom->getActor(); $entityManager->persist($actor); $entityManager->flush(); - + return $this->ajaxFormAnswer([ 'refresh' => true, 'reset' => true, @@ -45,12 +46,41 @@ ]); } } - + + // Actor delete form + /** @var ActorDeleteForm $actorDeleteForm */ + $actorDeleteForm = $this->createNamedCustomForm('actorDelete', ActorDeleteForm::class); + $actorDeleteForm->handleRequest($request); + if ($actorDeleteForm->isSubmitted() && $actorDeleteForm->isValid()) { + $errors = $actorAddFom->validate(); + if (empty($errors)) { + $actor = $actorDeleteForm->getActor($actorRepository); + if (!is_null($actor)) { + $entityManager->remove($actor); + $entityManager->flush(); + return $this->ajaxFormAnswer([ + 'refresh' => true, + ]); + } else { + return $this->ajaxFormAnswer([ + 'error' => 'Utilisateur non trouvé', + 'code' => Response::HTTP_BAD_REQUEST, + ]); + } + } 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, + 'actorDeleteForm' => $actorDeleteForm, 'actors' => $actors, ]); } diff --git a/src/Entity/ActorEntity.php b/src/Entity/ActorEntity.php index bd1a713..d52eadd 100644 --- a/src/Entity/ActorEntity.php +++ b/src/Entity/ActorEntity.php @@ -118,7 +118,7 @@ */ public function getFullName(): string { - return printf('%s %s', ucfirst(strtolower($this->firstName)), strtoupper($this->lastName)); + return sprintf('%s %s', ucfirst(strtolower($this->firstName)), strtoupper($this->lastName)); } /** @@ -126,7 +126,7 @@ * * @return string|null */ - public function getNickname(): ?string + public function getNickname(): string { return $this->nickname; } @@ -137,7 +137,7 @@ * @param string $nickname * @return self */ - public function setNickname(?string $nickname): self + public function setNickname(string $nickname): self { $this->nickname = $nickname; @@ -150,7 +150,7 @@ */ public function getDisplayName(): string { - return is_null($this->nickname) ? $this->nickname : $this->getFullName(); + return empty($this->nickname) ? $this->getFullName() : $this->nickname; } /** diff --git a/src/Form/ActorDeleteForm.php b/src/Form/ActorDeleteForm.php new file mode 100644 index 0000000..84106fe --- /dev/null +++ b/src/Form/ActorDeleteForm.php @@ -0,0 +1,39 @@ +form->getData(); + return $actorRepository->find($data['id']); + } + + /** + * {@inheritdoc} + */ + public function getTemplate(): string + { + return '_includes/html/form/actorDelete.html.twig'; + } + + /** + * {@inheritdoc} + */ + protected function addFields($formBuilder, $options): void + { + $formBuilder->add('id', HiddenType::class)->add('submit', SubmitType::class); + } +} \ No newline at end of file diff --git a/templates/_includes/html/form/actorDelete.html.twig b/templates/_includes/html/form/actorDelete.html.twig new file mode 100644 index 0000000..bd06022 --- /dev/null +++ b/templates/_includes/html/form/actorDelete.html.twig @@ -0,0 +1,16 @@ +{{ form_start(form, {'attr': {'class': 'ajax-form', 'data-form-confirmation': "Voulez-vous supprimer l'acteur "~data.actor.displayName~" ?\nCette action ne supprimera pas les activités et actions qui lui sont attribués."}}) }} +{{ form_widget(form.id, {'value': data.actor.id}) }} +{% if data.variant|default('icon') == 'icon' %} +{{ form_widget(form.submit, { + 'label': '', + 'label_html': true, + 'attr': { + 'class': 'icon-button', + } +})}} +{% elseif data.variant == 'full' %} +{{ form_widget(form.submit, {'label': 'Supprimer l\'acteur'})}} +{% endif %} +

+

+{{ form_end(form) }} \ No newline at end of file diff --git a/templates/_includes/html/genericForm.html.twig b/templates/_includes/html/genericForm.html.twig index f692c92..fa0800e 100644 --- a/templates/_includes/html/genericForm.html.twig +++ b/templates/_includes/html/genericForm.html.twig @@ -2,5 +2,5 @@ {{ form_start(form.formView) }} {{ form_end(form.formView) }} {% else %} -{% include form.template with {'form': form.formView} only %} +{% include form.template with {'form': form.formView, 'data': data|default([])} only %} {% endif %} \ No newline at end of file diff --git a/templates/actors.html.twig b/templates/actors.html.twig index 1d58cea..0b9e418 100644 --- a/templates/actors.html.twig +++ b/templates/actors.html.twig @@ -27,7 +27,7 @@ {{ actor.nickname }} {% if actor.active %} {% endif %} - + {% include '_includes/html/genericForm.html.twig' with {'form': actorDeleteForm, 'data': {'actor': actor, 'variant': 'icon'}} only %} {% endfor %}