1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
4
|
|
|
* |
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LoginCidadao\DynamicFormBundle\Form; |
12
|
|
|
|
13
|
|
|
use libphonenumber\PhoneNumberFormat; |
14
|
|
|
use LoginCidadao\CoreBundle\Entity\PersonAddress; |
15
|
|
|
use LoginCidadao\CoreBundle\Form\Type\CitySelectorComboType; |
16
|
|
|
use LoginCidadao\CoreBundle\Form\Type\IdCardType; |
17
|
|
|
use LoginCidadao\CoreBundle\Form\Type\PersonAddressFormType; |
18
|
|
|
use LoginCidadao\CoreBundle\Model\IdCardInterface; |
19
|
|
|
use LoginCidadao\CoreBundle\Model\LocationSelectData; |
20
|
|
|
use LoginCidadao\DynamicFormBundle\Model\DynamicFormData; |
21
|
|
|
use LoginCidadao\ValidationControlBundle\Handler\ValidationHandler; |
22
|
|
|
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType; |
23
|
|
|
use Symfony\Component\Form\Extension\Core\Type\BirthdayType; |
24
|
|
|
use Symfony\Component\Form\FormInterface; |
25
|
|
|
use Symfony\Component\Validator\Constraints; |
26
|
|
|
|
27
|
|
|
class DynamicFormBuilder |
28
|
|
|
{ |
29
|
|
|
/** @var ValidationHandler */ |
30
|
|
|
private $validationHandler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* DynamicFormBuilder constructor. |
34
|
|
|
* @param ValidationHandler $validationHandler |
35
|
|
|
*/ |
36
|
3 |
|
public function __construct(ValidationHandler $validationHandler) |
37
|
|
|
{ |
38
|
3 |
|
$this->validationHandler = $validationHandler; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
3 |
|
public function addFieldFromScope(FormInterface $form, $scope, DynamicFormData $data) |
42
|
|
|
{ |
43
|
|
|
switch ($scope) { |
44
|
3 |
|
case 'name': |
45
|
3 |
|
case 'surname': |
46
|
3 |
|
case 'full_name': |
47
|
1 |
|
$this->addRequiredPersonField($form, 'firstname'); |
48
|
1 |
|
$this->addRequiredPersonField($form, 'surname'); |
49
|
1 |
|
break; |
50
|
3 |
|
case 'cpf': |
51
|
1 |
|
$this->addRequiredPersonField($form, 'cpf', 'form-control cpf'); |
52
|
1 |
|
break; |
53
|
3 |
|
case 'email': |
54
|
1 |
|
$this->addRequiredPersonField($form, 'email'); |
55
|
1 |
|
break; |
56
|
3 |
|
case 'id_cards': |
57
|
3 |
|
$this->addIdCard($form, $data); |
58
|
3 |
|
break; |
59
|
1 |
|
case 'phone_number': |
60
|
1 |
|
case 'mobile': |
61
|
1 |
|
$this->addPhoneField($form); |
62
|
1 |
|
break; |
63
|
1 |
|
case 'birthdate': |
64
|
1 |
|
$this->addBirthdayField($form); |
65
|
1 |
|
break; |
66
|
1 |
|
case 'city': |
67
|
1 |
|
case 'state': |
68
|
1 |
|
case 'country': |
69
|
1 |
|
$this->addPlaceOfBirth($form, $scope); |
70
|
1 |
|
break; |
71
|
1 |
|
case 'addresses': |
72
|
1 |
|
$this->addAddresses($form, $data); |
73
|
1 |
|
break; |
74
|
|
|
default: |
75
|
1 |
|
break; |
76
|
|
|
} |
77
|
3 |
|
} |
78
|
|
|
|
79
|
1 |
|
private function getPersonForm(FormInterface $form) |
80
|
|
|
{ |
81
|
1 |
|
if ($form->has('person') === false) { |
82
|
1 |
|
$form->add('person', DynamicPersonType::class, ['label' => false]); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $form->get('person'); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
private function addRequiredPersonField(FormInterface $form, $field, $cssClass = null) |
89
|
|
|
{ |
90
|
1 |
|
$options = ['required' => true]; |
91
|
|
|
|
92
|
1 |
|
if ($cssClass) { |
93
|
1 |
|
$options['attr'] = ['class' => $cssClass]; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$this->getPersonForm($form)->add($field, null, $options); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
private function addPhoneField(FormInterface $form) |
100
|
|
|
{ |
101
|
1 |
|
$this->getPersonForm($form)->add('mobile', PhoneNumberType::class, [ |
102
|
1 |
|
'required' => true, |
103
|
|
|
'label' => 'person.form.mobile.label', |
104
|
|
|
'attr' => ['class' => 'form-control intl-tel', 'placeholder' => 'person.form.mobile.placeholder'], |
105
|
|
|
'label_attr' => ['class' => 'intl-tel-label'], |
106
|
|
|
'format' => PhoneNumberFormat::E164, |
107
|
|
|
]); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
1 |
|
private function addBirthdayField(FormInterface $form) |
111
|
|
|
{ |
112
|
1 |
|
$this->getPersonForm($form)->add('birthdate', BirthdayType::class, [ |
113
|
1 |
|
'required' => true, |
114
|
|
|
'format' => 'dd/MM/yyyy', |
115
|
|
|
'widget' => 'single_text', |
116
|
|
|
'label' => 'form.birthdate', |
117
|
|
|
'translation_domain' => 'FOSUserBundle', |
118
|
|
|
'attr' => ['pattern' => '[0-9/]*', 'class' => 'form-control birthdate'], |
119
|
|
|
]); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
private function addPlaceOfBirth(FormInterface $form, $level) |
123
|
|
|
{ |
124
|
1 |
|
$form->add('placeOfBirth', CitySelectorComboType::class, [ |
125
|
1 |
|
'level' => $level, |
126
|
1 |
|
'city_label' => 'Place of birth - City', |
127
|
1 |
|
'state_label' => 'Place of birth - State', |
128
|
1 |
|
'country_label' => 'Place of birth - Country', |
129
|
1 |
|
'constraints' => new Constraints\Valid(), |
130
|
|
|
]); |
131
|
|
|
|
132
|
1 |
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
private function addAddresses(FormInterface $form, DynamicFormData $data) |
136
|
|
|
{ |
137
|
1 |
|
$address = new PersonAddress(); |
138
|
1 |
|
$address->setLocation(new LocationSelectData()); |
139
|
1 |
|
$data->setAddress($address); |
140
|
1 |
|
$form->add('address', PersonAddressFormType::class, [ |
141
|
1 |
|
'label' => false, |
142
|
1 |
|
'constraints' => new Constraints\Valid(), |
143
|
|
|
]); |
144
|
1 |
|
} |
145
|
|
|
|
146
|
3 |
|
private function addIdCard(FormInterface $form, DynamicFormData $data) |
147
|
|
|
{ |
148
|
3 |
|
$person = $data->getPerson(); |
149
|
3 |
|
$state = $data->getIdCardState(); |
150
|
3 |
|
if (!$state) { |
|
|
|
|
151
|
1 |
|
return; |
152
|
|
|
} |
153
|
2 |
|
foreach ($person->getIdCards() as $idCard) { |
154
|
1 |
|
if ($idCard->getState()->getId() === $state->getId()) { |
155
|
1 |
|
$data->setIdCard($idCard); |
156
|
1 |
|
break; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
if (!($data->getIdCard() instanceof IdCardInterface)) { |
|
|
|
|
161
|
1 |
|
$idCard = $this->validationHandler->instantiateIdCard($state); |
162
|
1 |
|
$idCard->setPerson($person); |
163
|
1 |
|
$data->setIdCard($idCard); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
$form->add('idcard', IdCardType::class, ['label' => false, 'constraints' => new Constraints\Valid()]); |
167
|
2 |
|
} |
168
|
|
|
} |
169
|
|
|
|