diff options
Diffstat (limited to 'src/controller/ContactFormController.php')
-rw-r--r-- | src/controller/ContactFormController.php | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/controller/ContactFormController.php b/src/controller/ContactFormController.php index dcea868..181e93c 100644 --- a/src/controller/ContactFormController.php +++ b/src/controller/ContactFormController.php | |||
@@ -7,29 +7,34 @@ use Doctrine\ORM\EntityManager; | |||
7 | 7 | ||
8 | class ContactFormController | 8 | class ContactFormController |
9 | { | 9 | { |
10 | static public function updateRecipient(EntityManager $entityManager, array $json): void | 10 | static public function setEmailParam(EntityManager $entityManager, array $json): void |
11 | { | 11 | { |
12 | $email = htmlspecialchars(trim($json['email'])); | 12 | $form = new FormValidation($json, 'email_params'); |
13 | 13 | ||
14 | if((filter_var($email, FILTER_VALIDATE_EMAIL) // nouvel e-mail | 14 | $error = ''; |
15 | || ($json['email'] === '' && !empty(Config::$email_dest))) // e-mail par défaut | 15 | if($form->validate()){ |
16 | && isset($json['hidden']) && empty($json['hidden'])) | ||
17 | { | ||
18 | $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); | 16 | $form_data = $entityManager->find('App\Entity\NodeData', $json['id']); |
19 | $form_data->updateData('email', $email); | 17 | $form_data->updateData($json['what_param'], trim($json['value'])); |
20 | $entityManager->persist($form_data); | 18 | $entityManager->persist($form_data); |
21 | $entityManager->flush(); | 19 | $entityManager->flush(); |
20 | } | ||
21 | else{ | ||
22 | $error = $form->getErrors()[0]; // la 1ère erreur sera affichée | ||
23 | } | ||
22 | 24 | ||
25 | if(empty($error)){ | ||
23 | echo json_encode(['success' => true]); | 26 | echo json_encode(['success' => true]); |
24 | } | 27 | } |
25 | else{ | 28 | else{ |
26 | echo json_encode(['success' => false]); | 29 | echo json_encode(['success' => false, 'error' => $error]); |
27 | } | 30 | } |
28 | die; | 31 | die; |
29 | } | 32 | } |
33 | |||
34 | // les deux méthodes suivantes sont "factorisables", elles ne se distinguent que par la gestion ou non du formulaire rempli par le visiteur | ||
30 | static public function sendVisitorEmail(EntityManager $entityManager, array $json): void | 35 | static public function sendVisitorEmail(EntityManager $entityManager, array $json): void |
31 | { | 36 | { |
32 | $form = new FormValidation($json, 'email'); | 37 | $form = new FormValidation($json, 'email_send'); |
33 | 38 | ||
34 | $error = ''; | 39 | $error = ''; |
35 | if($form->validate()){ | 40 | if($form->validate()){ |
@@ -40,9 +45,8 @@ class ContactFormController | |||
40 | echo json_encode(['success' => false, 'error' => 'server_error']); | 45 | echo json_encode(['success' => false, 'error' => 'server_error']); |
41 | die; | 46 | die; |
42 | } | 47 | } |
43 | $recipient = $form_data->getData()['email'] ?? Config::$email_dest; | 48 | |
44 | 49 | if(!EmailService::send($entityManager, $form_data, false, $form->getField('name'), $form->getField('email'), $form->getField('message'))){ | |
45 | if(!EmailService::send($entityManager, $recipient, true, $form->getField('name'), $form->getField('email'), $form->getField('message'))){ | ||
46 | $error = 'email_not_sent'; | 50 | $error = 'email_not_sent'; |
47 | } | 51 | } |
48 | } | 52 | } |
@@ -67,9 +71,8 @@ class ContactFormController | |||
67 | echo json_encode(['success' => false, 'error' => 'server_error']); | 71 | echo json_encode(['success' => false, 'error' => 'server_error']); |
68 | die; | 72 | die; |
69 | } | 73 | } |
70 | $recipient = $form_data->getData()['email'] ?? Config::$email_dest; | ||
71 | 74 | ||
72 | if(EmailService::send($entityManager, $recipient, false, 'nom du visiteur', 'adresse@du_visiteur.fr', "TEST d'un envoi d'e-mail depuis le site web")){ | 75 | 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")){ |
73 | echo json_encode(['success' => true]); | 76 | echo json_encode(['success' => true]); |
74 | } | 77 | } |
75 | else{ | 78 | else{ |