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