aboutsummaryrefslogtreecommitdiff
path: root/src/controller/ContactFormController.php
blob: 181e93c7c9af8be65a4c90f9d2d7d99661239da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
// src/controller/ContactFormController.php

declare(strict_types=1);

use Doctrine\ORM\EntityManager;

class ContactFormController
{
	static public function setEmailParam(EntityManager $entityManager, array $json): void
	{
		$form = new FormValidation($json, 'email_params');
		
		$error = '';
		if($form->validate()){
			$form_data = $entityManager->find('App\Entity\NodeData', $json['id']);
			$form_data->updateData($json['what_param'], trim($json['value']));
			$entityManager->persist($form_data);
			$entityManager->flush();
		}
		else{
			$error = $form->getErrors()[0]; // la 1ère erreur sera affichée
		}

		if(empty($error)){
			echo json_encode(['success' => true]);
		}
		else{
			echo json_encode(['success' => false, 'error' => $error]);
		}
		die;
	}

	// les deux méthodes suivantes sont "factorisables", elles ne se distinguent que par la gestion ou non du formulaire rempli par le visiteur
	static public function sendVisitorEmail(EntityManager $entityManager, array $json): void
	{
		$form = new FormValidation($json, 'email_send');

		$error = '';
		if($form->validate()){
			// destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur
			$form_data = $entityManager->find('App\Entity\NodeData', $json['id']);
			if($form_data === null){
				http_response_code(500);
				echo json_encode(['success' => false, 'error' => 'server_error']);
				die;
			}
			
			if(!EmailService::send($entityManager, $form_data, false, $form->getField('name'), $form->getField('email'), $form->getField('message'))){
				$error = 'email_not_sent';
			}
		}
		else{
			$error = $form->getErrors()[0]; // la 1ère erreur sera affichée
		}

		if(empty($error)){
			echo json_encode(['success' => true]);
		}
		else{
			echo json_encode(['success' => false, 'error' => $error]);
		}
		die;
	}
	static public function sendTestEmail(EntityManager $entityManager, array $json): void
	{
		// destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur
		$form_data = $entityManager->find('App\Entity\NodeData', $json['id']);
		if($form_data === null){
			http_response_code(500);
			echo json_encode(['success' => false, 'error' => 'server_error']);
			die;
		}

		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")){
			echo json_encode(['success' => true]);
		}
		else{
			echo json_encode(['success' => false, 'error' => 'email_not_sent']);
		}
		die;
	}
}