feat(main): Old module
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Override CustomerAddressFormatter
|
||||
*/
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class CustomerAddressFormatter extends CustomerAddressFormatterCore
|
||||
{
|
||||
private $country;
|
||||
private $translator;
|
||||
private $availableCountries;
|
||||
private $definition;
|
||||
|
||||
public function __construct(
|
||||
Country $country,
|
||||
TranslatorInterface $translator,
|
||||
array $availableCountries
|
||||
) {
|
||||
parent::__construct($country, $translator, $availableCountries);
|
||||
|
||||
$this->country = $country;
|
||||
$this->translator = $translator;
|
||||
$this->availableCountries = $availableCountries;
|
||||
$this->definition = Address::$definition['fields'];
|
||||
}
|
||||
|
||||
public function getFormat()
|
||||
{
|
||||
$fields = AddressFormat::getOrderedAddressFields(
|
||||
$this->country->id,
|
||||
true,
|
||||
true
|
||||
);
|
||||
$required = array_flip(AddressFormat::getFieldsRequired());
|
||||
|
||||
$format = [
|
||||
'back' => (new FormField())
|
||||
->setName('back')
|
||||
->setType('hidden'),
|
||||
'token' => (new FormField())
|
||||
->setName('token')
|
||||
->setType('hidden'),
|
||||
'alias' => (new FormField())
|
||||
->setName('alias')
|
||||
->setLabel(
|
||||
$this->getFieldLabel('alias')
|
||||
),
|
||||
];
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$formField = new FormField();
|
||||
$formField->setName($field);
|
||||
|
||||
$fieldParts = explode(':', $field, 2);
|
||||
|
||||
if (count($fieldParts) === 1) {
|
||||
if ($field === 'postcode') {
|
||||
if ($this->country->need_zip_code) {
|
||||
$formField->setRequired(true);
|
||||
}
|
||||
} 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;
|
||||
|
||||
// Fields specified using the Entity:field
|
||||
// notation are actually references to other
|
||||
// entities, so they should be displayed as a select
|
||||
$formField->setType('select');
|
||||
|
||||
// Also, what we really want is the id of the linked entity
|
||||
$formField->setName('id_' . strtolower($entity));
|
||||
|
||||
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);
|
||||
foreach ($states as $state) {
|
||||
$formField->addAvailableValue(
|
||||
$state['id_state'],
|
||||
$state[$entityField]
|
||||
);
|
||||
}
|
||||
$formField->setRequired(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$formField->setLabel($this->getFieldLabel($field));
|
||||
if (!$formField->isRequired()) {
|
||||
// Only trust the $required array for fields
|
||||
// that are not marked as required.
|
||||
// $required doesn't have all the info, and fields
|
||||
// may be required for other reasons than what
|
||||
// AddressFormat::getFieldsRequired() says.
|
||||
$formField->setRequired(
|
||||
array_key_exists($field, $required)
|
||||
);
|
||||
}
|
||||
|
||||
$format[$formField->getName()] = $formField;
|
||||
}
|
||||
|
||||
//To add the extra fields in address form
|
||||
//$additionalAddressFormFields = Hook::exec('additionalCustomerAddressFields', array(), null, true);
|
||||
$additionalAddressFormFields = Hook::exec('additionalCustomerAddressFields', ['fields' => &$format], null, true);
|
||||
if (is_array($additionalAddressFormFields)) {
|
||||
foreach ($additionalAddressFormFields as $moduleName => $additionnalFormFields) {
|
||||
if (!is_array($additionnalFormFields)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($additionnalFormFields as $formField) {
|
||||
$formField->moduleName = $moduleName;
|
||||
$format[$moduleName . '_' . $formField->getName()] = $formField;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->addConstraints(
|
||||
$this->addMaxLength(
|
||||
$format
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function addConstraints(array $format)
|
||||
{
|
||||
foreach ($format as $field) {
|
||||
if (!empty($this->definition[$field->getName()]['validate'])) {
|
||||
$field->addConstraint(
|
||||
$this->definition[$field->getName()]['validate']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $format;
|
||||
}
|
||||
|
||||
private function addMaxLength(array $format)
|
||||
{
|
||||
foreach ($format as $field) {
|
||||
if (!empty($this->definition[$field->getName()]['size'])) {
|
||||
$field->setMaxLength(
|
||||
$this->definition[$field->getName()]['size']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $format;
|
||||
}
|
||||
|
||||
private function getFieldLabel($field)
|
||||
{
|
||||
// Country:name => Country, Country:iso_code => Country,
|
||||
// same label regardless of which field is used for mapping.
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
/**
|
||||
* @Override CustomerFormatter
|
||||
*/
|
||||
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class CustomerFormatter extends CustomerFormatterCore
|
||||
{
|
||||
private $translator;
|
||||
private $language;
|
||||
|
||||
private $ask_for_birthdate = true;
|
||||
private $ask_for_partner_optin = true;
|
||||
private $partner_optin_is_required = true;
|
||||
private $ask_for_password = true;
|
||||
private $password_is_required = true;
|
||||
private $ask_for_new_password = false;
|
||||
|
||||
public function __construct(
|
||||
TranslatorInterface $translator,
|
||||
Language $language
|
||||
) {
|
||||
parent::__construct($translator, $language);
|
||||
$this->translator = $translator;
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
public function setAskForBirthdate($ask_for_birthdate)
|
||||
{
|
||||
$this->ask_for_birthdate = $ask_for_birthdate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAskForPartnerOptin($ask_for_partner_optin)
|
||||
{
|
||||
$this->ask_for_partner_optin = $ask_for_partner_optin;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPartnerOptinRequired($partner_optin_is_required)
|
||||
{
|
||||
$this->partner_optin_is_required = $partner_optin_is_required;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAskForPassword($ask_for_password)
|
||||
{
|
||||
$this->ask_for_password = $ask_for_password;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAskForNewPassword($ask_for_new_password)
|
||||
{
|
||||
$this->ask_for_new_password = $ask_for_new_password;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPasswordRequired($password_is_required)
|
||||
{
|
||||
$this->password_is_required = $password_is_required;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFormat()
|
||||
{
|
||||
$format = [];
|
||||
|
||||
$genders = Gender::getGenders($this->language->id);
|
||||
if ($genders->count() > 0) {
|
||||
$genderField = (new FormField())
|
||||
->setName('id_gender')
|
||||
->setType('radio-buttons')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'Social title',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
);
|
||||
foreach ($genders as $gender) {
|
||||
$genderField->addAvailableValue($gender->id, $gender->name);
|
||||
}
|
||||
$format[$genderField->getName()] = $genderField;
|
||||
}
|
||||
|
||||
$format['firstname'] = (new FormField())
|
||||
->setName('firstname')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'First name',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
)
|
||||
->setRequired(true);
|
||||
|
||||
$format['lastname'] = (new FormField())
|
||||
->setName('lastname')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'Last name',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
)
|
||||
->setRequired(true);
|
||||
|
||||
if (Configuration::get('PS_B2B_ENABLE')) {
|
||||
$format['company'] = (new FormField())
|
||||
->setName('company')
|
||||
->setType('text')
|
||||
->setLabel($this->translator->trans(
|
||||
'Company',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
));
|
||||
$format['siret'] = (new FormField())
|
||||
->setName('siret')
|
||||
->setType('text')
|
||||
->setLabel($this->translator->trans(
|
||||
// Please localize this string with the applicable registration number type in your country. For example : "SIRET" in France and "Código fiscal" in Spain.
|
||||
'Identification number',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
));
|
||||
}
|
||||
|
||||
$format['email'] = (new FormField())
|
||||
->setName('email')
|
||||
->setType('email')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'Email',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
)
|
||||
->setRequired(true);
|
||||
|
||||
if ($this->ask_for_password) {
|
||||
$format['password'] = (new FormField())
|
||||
->setName('password')
|
||||
->setType('password')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'Password',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
)
|
||||
->setRequired($this->password_is_required);
|
||||
}
|
||||
|
||||
if ($this->ask_for_new_password) {
|
||||
$format['new_password'] = (new FormField())
|
||||
->setName('new_password')
|
||||
->setType('password')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'New password',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->ask_for_birthdate) {
|
||||
$format['birthday'] = (new FormField())
|
||||
->setName('birthday')
|
||||
->setType('text')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'Birthdate',
|
||||
[],
|
||||
'Shop.Forms.Labels'
|
||||
)
|
||||
)
|
||||
->addAvailableValue('placeholder', Tools::getDateFormat())
|
||||
->addAvailableValue(
|
||||
'comment',
|
||||
$this->translator->trans('(E.g.: %date_format%)', array('%date_format%' => Tools::formatDateStr('31 May 1970')), 'Shop.Forms.Help')
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->ask_for_partner_optin) {
|
||||
$format['optin'] = (new FormField())
|
||||
->setName('optin')
|
||||
->setType('checkbox')
|
||||
->setLabel(
|
||||
$this->translator->trans(
|
||||
'Receive offers from our partners',
|
||||
[],
|
||||
'Shop.Theme.Customeraccount'
|
||||
)
|
||||
)
|
||||
->setRequired($this->partner_optin_is_required);
|
||||
}
|
||||
|
||||
// ToDo, replace the hook exec with HookFinder when the associated PR will be merged
|
||||
//$additionalCustomerFormFields = Hook::exec('additionalCustomerFormFields', array(), null, true);
|
||||
$additionalCustomerFormFields = Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true);
|
||||
|
||||
if (is_array($additionalCustomerFormFields)) {
|
||||
foreach ($additionalCustomerFormFields as $moduleName => $additionnalFormFields) {
|
||||
if (!is_array($additionnalFormFields)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($additionnalFormFields as $formField) {
|
||||
$formField->moduleName = $moduleName;
|
||||
$format[$moduleName . '_' . $formField->getName()] = $formField;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: TVA etc.?
|
||||
|
||||
return $this->addConstraints($format);
|
||||
}
|
||||
|
||||
private function addConstraints(array $format)
|
||||
{
|
||||
$constraints = Customer::$definition['fields'];
|
||||
|
||||
foreach ($format as $field) {
|
||||
if (!empty($constraints[$field->getName()]['validate'])) {
|
||||
$field->addConstraint(
|
||||
$constraints[$field->getName()]['validate']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $format;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2022 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License (AFL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/afl-3.0.php
|
||||
* 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 http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2022 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
Reference in New Issue
Block a user