This repository was archived by the owner on Oct 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathEmailFormHandler.php
More file actions
103 lines (86 loc) · 2.46 KB
/
EmailFormHandler.php
File metadata and controls
103 lines (86 loc) · 2.46 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Lexik\Bundle\MailerBundle\Form\Handler;
use Doctrine\ORM\EntityManager;
use Lexik\Bundle\MailerBundle\Entity\Email;
use Lexik\Bundle\MailerBundle\Entity\EmailTranslation;
use Lexik\Bundle\MailerBundle\Form\Model\EntityTranslationModel;
use Lexik\Bundle\MailerBundle\Form\Type\EmailType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Cédric Girard <c.girard@lexik.fr>
*/
class EmailFormHandler implements FormHandlerInterface
{
/**
* @var \Symfony\Component\Form\FormFactoryInterface
*/
private $factory;
/**
* @var EntityManager
*/
private $em;
/**
* @var string
*/
private $defaultLocale;
/**
* @var string
*/
private $locale;
/**
* @param FormFactoryInterface $factory
* @param EntityManager $em
* @param string $defaultLocale
*/
public function __construct(FormFactoryInterface $factory, EntityManager $em, $defaultLocale)
{
$this->factory = $factory;
$this->em = $em;
$this->defaultLocale = $defaultLocale;
}
/**
* {@inheritdoc}
*/
public function getLocale()
{
return $this->locale;
}
/**
* {@inheritdoc}
*/
public function createForm($email = null, $lang = null)
{
$edit = ($email !== null);
$this->locale = $lang ? : $this->defaultLocale;
if ($edit) {
$translation = $email->getTranslation($this->locale);
} else {
$email = new Email();
$translation = new EmailTranslation($this->defaultLocale);
$translation->setEmail($email);
}
$model = new EntityTranslationModel($email, $translation);
return $this->factory->create(EmailType::class, $model, array(
'data_translation' => $translation,
'edit' => $edit,
));
}
/**
* {@inheritdoc}
*/
public function processForm(FormInterface $form, Request $request)
{
$valid = false;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$model = $form->getData();
$model->getEntity()->addTranslation($model->getTranslation());
$this->em->persist($model->getEntity());
$this->em->flush();
$valid = true;
}
return $valid;
}
}