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