diff options
Diffstat (limited to 'src/Controller/ContactFormController.php')
| -rw-r--r-- | src/Controller/ContactFormController.php | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/Controller/ContactFormController.php b/src/Controller/ContactFormController.php new file mode 100644 index 0000000..56ef402 --- /dev/null +++ b/src/Controller/ContactFormController.php | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | <?php | ||
| 2 | // src/Controller/ContactFormController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Controller; | ||
| 7 | |||
| 8 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
| 9 | use Doctrine\ORM\EntityManager; | ||
| 10 | use App\Entity\EmailForm; | ||
| 11 | use App\Entity\Email; | ||
| 12 | use App\Service\EmailService; | ||
| 13 | use App\Service\FormValidation; | ||
| 14 | use App\Service\ValidationStrategy; | ||
| 15 | |||
| 16 | class ContactFormController | ||
| 17 | { | ||
| 18 | static public function keepEmails(EntityManager $entityManager, array $json): JsonResponse | ||
| 19 | { | ||
| 20 | $form_data = $entityManager->find(EmailForm::class, $json['id']); | ||
| 21 | $form_data->updateData('keep_emails', $json['checked'] ? true : false); | ||
| 22 | $entityManager->persist($form_data); | ||
| 23 | $entityManager->flush(); | ||
| 24 | return new JsonResponse(['success' => true, 'checked' => $json['checked']]); | ||
| 25 | } | ||
| 26 | static public function setEmailsRetentionPeriod(EntityManager $entityManager, array $json): JsonResponse | ||
| 27 | { | ||
| 28 | $form_data = $entityManager->find(EmailForm::class, $json['id']); | ||
| 29 | $form_data->updateData($json['field'], (int)$json['months']); | ||
| 30 | $entityManager->persist($form_data); | ||
| 31 | $entityManager->flush(); | ||
| 32 | return new JsonResponse(['success' => true, 'months' => $json['months']]); | ||
| 33 | } | ||
| 34 | static public function setEmailParam(EntityManager $entityManager, array $json): JsonResponse | ||
| 35 | { | ||
| 36 | $form = new FormValidation($json, ValidationStrategy::EmailParam); | ||
| 37 | |||
| 38 | if($form->validate()){ | ||
| 39 | $form_data = $entityManager->find(EmailForm::class, $json['id']); | ||
| 40 | $form_data->updateData($json['what_param'], trim($json['value'])); | ||
| 41 | $entityManager->persist($form_data); // ?? | ||
| 42 | $entityManager->flush(); | ||
| 43 | return new JsonResponse(['success' => true]); | ||
| 44 | } | ||
| 45 | else{ | ||
| 46 | return new JsonResponse(['success' => false, 'error' => $form->getError()]); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | // les deux méthodes suivantes sont "factorisables", elles ne se distinguent que par la gestion ou non du formulaire rempli par le visiteur | ||
| 51 | static public function sendVisitorEmail(EntityManager $entityManager, array $json): JsonResponse | ||
| 52 | { | ||
| 53 | $form = new FormValidation($json, ValidationStrategy::EmailSend); | ||
| 54 | |||
| 55 | $error = ''; | ||
| 56 | if($form->validate()){ | ||
| 57 | // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur | ||
| 58 | $form_data = $entityManager->find(EmailForm::class, $json['id']); | ||
| 59 | if($form_data === null){ | ||
| 60 | return new JsonResponse(['success' => false, 'error' => 'server_error'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500 | ||
| 61 | } | ||
| 62 | |||
| 63 | if(!EmailService::send($entityManager, $form_data, false, $form->getField('name'), $form->getField('email'), $form->getField('message'))){ | ||
| 64 | $error = 'email_not_sent'; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | else{ | ||
| 68 | $error = $form->getError(); | ||
| 69 | } | ||
| 70 | |||
| 71 | if(empty($error)){ | ||
| 72 | return new JsonResponse(['success' => true]); | ||
| 73 | } | ||
| 74 | else{ | ||
| 75 | return new JsonResponse(['success' => false, 'error' => $error]); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | static public function sendTestEmail(EntityManager $entityManager, array $json): JsonResponse | ||
| 79 | { | ||
| 80 | // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur | ||
| 81 | $form_data = $entityManager->find(EmailForm::class, $json['id']); | ||
| 82 | if($form_data === null){ | ||
| 83 | return new JsonResponse(['success' => false, 'error' => 'server_error'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); | ||
| 84 | } | ||
| 85 | |||
| 86 | if(EmailService::send($entityManager, $form_data, true, 'nom du visiteur', 'adresse@du_visiteur.fr', "TEST d'un envoi d'e-mail depuis le site web")){ | ||
| 87 | return new JsonResponse(['success' => true]); | ||
| 88 | } | ||
| 89 | else{ | ||
| 90 | return new JsonResponse(['success' => false, 'error' => 'email_not_sent']); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | static public function deleteEmail(EntityManager $entityManager, array $json): JsonResponse | ||
| 94 | { | ||
| 95 | $email = $entityManager->find(Email::class, $json['id']); | ||
| 96 | $entityManager->remove($email); | ||
| 97 | $entityManager->flush(); | ||
| 98 | return new JsonResponse(['success' => true]); | ||
| 99 | } | ||
| 100 | static public function toggleSensitiveEmail(EntityManager $entityManager, array $json): JsonResponse | ||
| 101 | { | ||
| 102 | $email = $entityManager->find(Email::class, $json['id']); | ||
| 103 | $email->makeSensitive($json['checked']); | ||
| 104 | $entityManager->flush(); | ||
| 105 | return new JsonResponse(['success' => true, 'checked' => $json['checked'], 'deletion_date' => $email->getDeletionDate()->format('d/m/Y')]); | ||
| 106 | } | ||
| 107 | } \ No newline at end of file | ||
