Advertisement
Gaskabur

Untitled

Oct 10th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.60 KB | None | 0 0
  1. public function getFormat()
  2.     {
  3.         $fields = AddressFormat::getOrderedAddressFields(
  4.             $this->country->id,
  5.             true,
  6.             true
  7.         );
  8.         $required = array_flip(AddressFormat::getFieldsRequired());
  9.  
  10.         $format = [
  11.             'id_address' => (new FormField())
  12.                 ->setName('id_address')
  13.                 ->setType('hidden'),
  14.             'id_customer' => (new FormField())
  15.                 ->setName('id_customer')
  16.                 ->setType('hidden'),
  17.             'back' => (new FormField())
  18.                 ->setName('back')
  19.                 ->setType('hidden'),
  20.             'token' => (new FormField())
  21.                 ->setName('token')
  22.                 ->setType('hidden'),
  23.         ];
  24.  
  25.         foreach ($fields as $field) {
  26.             $formField = new FormField();
  27.             $formField->setName($field);
  28.  
  29.             $fieldParts = explode(':', $field, 2);
  30.  
  31.             if (count($fieldParts) === 1) {
  32.                 if ($field === 'postcode') {
  33.                     if ($this->country->need_zip_code) {
  34.                         $formField->setRequired(true);
  35.                     }
  36.                 } elseif ($field === 'phone') {
  37.                     $formField->setType('tel');
  38.                 } elseif ($field === 'dni' && null !== $this->country) {
  39.                     if ($this->country->need_identification_number) {
  40.                         $formField->setRequired(true);
  41.                     }
  42.                 }
  43.             } elseif (count($fieldParts) === 2) {
  44.                 list($entity, $entityField) = $fieldParts;
  45.  
  46.                 // Fields specified using the Entity:field
  47.                 // notation are actually references to other
  48.                 // entities, so they should be displayed as a select
  49.                 $formField->setType('select');
  50.  
  51.                 // Also, what we really want is the id of the linked entity
  52.                 $formField->setName('id_' . strtolower($entity));
  53.  
  54.                 if ($entity === 'Country') {
  55.                     $formField->setType('countrySelect');
  56.                     $formField->setValue($this->country->id);
  57.                     foreach ($this->availableCountries as $country) {
  58.                         $formField->addAvailableValue(
  59.                             $country['id_country'],
  60.                             $country[$entityField]
  61.                         );
  62.                     }
  63.                 } elseif ($entity === 'State') {
  64.                     if ($this->country->contains_states) {
  65.                         $states = State::getStatesByIdCountry($this->country->id, true);
  66.                         foreach ($states as $state) {
  67.                             $formField->addAvailableValue(
  68.                                 $state['id_state'],
  69.                                 $state[$entityField]
  70.                             );
  71.                         }
  72.                         $formField->setRequired(true);
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             $formField->setLabel($this->getFieldLabel($field));
  78.             if (!$formField->isRequired()) {
  79.                 // Only trust the $required array for fields
  80.                 // that are not marked as required.
  81.                 // $required doesn't have all the info, and fields
  82.                 // may be required for other reasons than what
  83.                 // AddressFormat::getFieldsRequired() says.
  84.                 $formField->setRequired(
  85.                     array_key_exists($field, $required)
  86.                 );
  87.             }
  88.  
  89.             $format[$formField->getName()] = $formField;
  90.         }
  91.  
  92.  
  93.         $ff = new FormField();
  94.         $ff->setName('alias');
  95.         $ff->setLabel($this->getFieldLabel('alias'));
  96.         $ff->setValue('Mi dirección');
  97.         $format[$ff->getName()] = $ff;
  98.  
  99.         //To add the extra fields in address form
  100.         $additionalAddressFormFields = Hook::exec('additionalCustomerAddressFields', array(), null, true);
  101.         if (is_array($additionalAddressFormFields)) {
  102.             foreach ($additionalAddressFormFields as $moduleName => $additionnalFormFields) {
  103.                 if (!is_array($additionnalFormFields)) {
  104.                     continue;
  105.                 }
  106.  
  107.                 foreach ($additionnalFormFields as $formField) {
  108.                     $formField->moduleName = $moduleName;
  109.                     $format[$moduleName . '_' . $formField->getName()] = $formField;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         return $this->addConstraints(
  115.                 $this->addMaxLength(
  116.                     $format
  117.                 )
  118.         );
  119.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement