-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddressFieldsFormatTrait.php
More file actions
220 lines (201 loc) · 7.88 KB
/
AddressFieldsFormatTrait.php
File metadata and controls
220 lines (201 loc) · 7.88 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
namespace PrestaShop\Module\PsOnePageCheckout\Form;
/**
* Module-owned shared logic for OPC address format generation.
*
* Expects the using class to define: $country, $translator, $availableCountries, $definition.
*/
trait AddressFieldsFormatTrait
{
/**
* Build address fields format with optional prefix (for delivery vs invoice).
*
* @param string $prefix Prefix for field names (e.g. 'invoice_' for invoice address)
* @param bool $aliasRequired Whether alias field is required
*
* @return array<string, \FormField>
*/
protected function getAddressFieldsFormat($prefix = '', $aliasRequired = false)
{
$fields = \AddressFormat::getOrderedAddressFields(
$this->country->id,
true,
true
);
$required = array_flip(\AddressFormat::getFieldsRequired());
$format = [];
$format[$prefix . 'alias'] = (new \FormField())
->setName($prefix . 'alias')
->setLabel($this->getFieldLabel('alias'));
if ($aliasRequired) {
$format[$prefix . 'alias']->setRequired(true);
}
foreach ($fields as $field) {
$formField = new \FormField();
$fieldName = $field;
$fieldParts = explode(':', $field, 2);
if (count($fieldParts) === 2) {
list($entity, $entityField) = $fieldParts;
$fieldName = 'id_' . strtolower($entity);
}
$formField->setName($prefix . $fieldName);
if (count($fieldParts) === 1) {
if ($field === 'postcode') {
if ($this->country->need_zip_code) {
$formField->setRequired(true);
}
$formField->setMinLength(
strlen($this->country->zip_code_format)
);
} elseif ($field === 'phone') {
$formField->setType('tel');
} elseif ($field === 'dni' && null !== $this->country) {
if ($this->country->need_identification_number) {
$formField->setRequired(true);
}
}
} elseif (count($fieldParts) === 2) {
list($entity, $entityField) = $fieldParts;
$formField->setType('select');
if ($entity === 'Country') {
$formField->setType('countrySelect');
$formField->setValue($this->country->id);
foreach ($this->availableCountries as $country) {
$formField->addAvailableValue(
$country['id_country'],
$country[$entityField]
);
}
} elseif ($entity === 'State') {
if ($this->country->contains_states) {
$states = \State::getStatesByIdCountry($this->country->id, true, 'name', 'asc');
foreach ($states as $state) {
$formField->addAvailableValue(
$state['id_state'],
$state[$entityField]
);
}
$formField->setRequired(true);
}
}
}
$formField->setLabel($this->getFieldLabel($field));
if (!$formField->isRequired()) {
$formField->setRequired(
array_key_exists($field, $required)
);
}
$format[$formField->getName()] = $formField;
}
return $format;
}
/**
* Get base field name for definition lookup (e.g. strip invoice_ prefix).
* Override in OnePageCheckoutFormatter to strip invoice_ prefix.
*
* @param string $name
*
* @return string
*/
protected function getDefinitionKey($name)
{
return $name;
}
/**
* @param array<string, \FormField> $format
*
* @return array<string, \FormField>
*/
protected function addConstraints(array $format)
{
foreach ($format as $field) {
$key = $this->getDefinitionKey($field->getName());
if (!empty($this->definition[$key]['validate'])) {
$field->addConstraint($this->definition[$key]['validate']);
}
}
return $format;
}
/**
* @param array<string, \FormField> $format
*
* @return array<string, \FormField>
*/
protected function addMaxLength(array $format)
{
foreach ($format as $field) {
$key = $this->getDefinitionKey($field->getName());
if (!empty($this->definition[$key]['size'])) {
$field->setMaxLength($this->definition[$key]['size']);
}
}
return $format;
}
/**
* @param string $field
*
* @return string
*/
protected function getFieldLabel($field)
{
$field = explode(':', $field)[0];
switch ($field) {
case 'alias':
return $this->translator->trans('Alias', [], 'Shop.Forms.Labels');
case 'firstname':
return $this->translator->trans('First name', [], 'Shop.Forms.Labels');
case 'lastname':
return $this->translator->trans('Last name', [], 'Shop.Forms.Labels');
case 'address1':
return $this->translator->trans('Address', [], 'Shop.Forms.Labels');
case 'address2':
return $this->translator->trans('Address Complement', [], 'Shop.Forms.Labels');
case 'postcode':
return $this->translator->trans('Zip/Postal Code', [], 'Shop.Forms.Labels');
case 'city':
return $this->translator->trans('City', [], 'Shop.Forms.Labels');
case 'Country':
return $this->translator->trans('Country', [], 'Shop.Forms.Labels');
case 'State':
return $this->translator->trans('State', [], 'Shop.Forms.Labels');
case 'phone':
return $this->translator->trans('Phone', [], 'Shop.Forms.Labels');
case 'phone_mobile':
return $this->translator->trans('Mobile phone', [], 'Shop.Forms.Labels');
case 'company':
return $this->translator->trans('Company', [], 'Shop.Forms.Labels');
case 'vat_number':
return $this->translator->trans('VAT number', [], 'Shop.Forms.Labels');
case 'dni':
return $this->translator->trans('Identification number', [], 'Shop.Forms.Labels');
case 'other':
return $this->translator->trans('Other', [], 'Shop.Forms.Labels');
default:
return $field;
}
}
}