SHARE
TWEET

Untitled

a guest May 14th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. use Symfony\Component\Translation\TranslatorInterface;
  3.  
  4. class CustomerFormatter extends CustomerFormatterCore
  5. {
  6.    private $translator;
  7.    private $language;
  8.  
  9.    private $ask_for_birthdate = true;
  10.    private $ask_for_partner_optin = true;
  11.    private $partner_optin_is_required = true;
  12.    private $ask_for_password = true;
  13.    private $password_is_required = true;
  14.    private $ask_for_new_password = false;
  15.  
  16.    public function __construct(
  17.        TranslatorInterface $translator,
  18.        Language $language
  19.    ) {
  20.        $this->translator = $translator;
  21.         $this->language = $language;
  22.     }
  23.  
  24.     public function setAskForBirthdate($ask_for_birthdate)
  25.     {
  26.         $this->ask_for_birthdate = $ask_for_birthdate;
  27.  
  28.         return $this;
  29.     }
  30.  
  31.     public function setAskForPartnerOptin($ask_for_partner_optin)
  32.     {
  33.         $this->ask_for_partner_optin = $ask_for_partner_optin;
  34.  
  35.         return $this;
  36.     }
  37.  
  38.     public function setPartnerOptinRequired($partner_optin_is_required)
  39.     {
  40.         $this->partner_optin_is_required = $partner_optin_is_required;
  41.  
  42.         return $this;
  43.     }
  44.  
  45.     public function setAskForPassword($ask_for_password)
  46.     {
  47.         $this->ask_for_password = $ask_for_password;
  48.  
  49.         return $this;
  50.     }
  51.  
  52.     public function setAskForNewPassword($ask_for_new_password)
  53.     {
  54.         $this->ask_for_new_password = $ask_for_new_password;
  55.  
  56.         return $this;
  57.     }
  58.  
  59.     public function setPasswordRequired($password_is_required)
  60.     {
  61.         $this->password_is_required = $password_is_required;
  62.  
  63.         return $this;
  64.     }
  65.  
  66.     public function getFormat()
  67.     {
  68.         $format = [];
  69.  
  70.         $format['id_customer'] = (new FormField())
  71.             ->setName('id_customer')
  72.             ->setType('hidden')
  73.         ;
  74.  
  75.         $genders = Gender::getGenders($this->language->id);
  76.         if ($genders->count() > 0) {
  77.             $genderField = (new FormField())
  78.                 ->setName('id_gender')
  79.                 ->setType('radio-buttons')
  80.                 ->setLabel(
  81.                     $this->translator->trans(
  82.                         'Social title', [], 'Shop.Forms.Labels'
  83.                     )
  84.                 )
  85.             ;
  86.             foreach ($genders as $gender) {
  87.                 $genderField->addAvailableValue($gender->id, $gender->name);
  88.             }
  89.             $format[$genderField->getName()] = $genderField;
  90.         }
  91.  
  92.         $format['firstname'] = (new FormField())
  93.             ->setName('firstname')
  94.             ->setLabel(
  95.                 $this->translator->trans(
  96.                     'First name', [], 'Shop.Forms.Labels'
  97.                 )
  98.             )
  99.             ->setRequired(true)
  100.         ;
  101.  
  102.         $format['lastname'] = (new FormField())
  103.             ->setName('lastname')
  104.             ->setLabel(
  105.                 $this->translator->trans(
  106.                     'Last name', [], 'Shop.Forms.Labels'
  107.                 )
  108.             )
  109.             ->setRequired(true)
  110.         ;
  111.  
  112.         if (Configuration::get('PS_B2B_ENABLE')) {
  113.             $format['company'] = (new FormField())
  114.                 ->setName('company')
  115.                 ->setType('text')
  116.                 ->setLabel($this->translator->trans(
  117.                     'Company', [], 'Shop.Forms.Labels'
  118.                 ));
  119.             $format['siret'] = (new FormField())
  120.                 ->setName('siret')
  121.                 ->setType('text')
  122.                 ->setLabel($this->translator->trans(
  123.                     // Please localize this string with the applicable registration number type in your country. For example : "SIRET" in France and "C√≥digo fiscal" in Spain.
  124.                     'Identification number', [], 'Shop.Forms.Labels'
  125.                 ));
  126.         }
  127.  
  128.         $format['phone'] = (new FormField)
  129.         ->setName('phone')
  130.         ->setLabel(
  131.             $this->translator->trans(
  132.                 'Phone', [], 'Shop.Forms.Labels'
  133.             )
  134.         )
  135.         ->setRequired(true)
  136.     ;
  137.     $format['address1'] = (new FormField)
  138.         ->setName('address1')
  139.         ->setLabel(
  140.             $this->translator->trans(
  141.                 'Address', [], 'Shop.Forms.Labels'
  142.             )
  143.         )
  144.         ->setRequired(true)
  145.     ;
  146.     $format['postcode'] = (new FormField)
  147.         ->setName('postcode')
  148.         ->setLabel(
  149.             $this->translator->trans(
  150.                 'Zip/Postal Code', [], 'Shop.Forms.Labels'
  151.             )
  152.         )
  153.         ->setRequired(true)
  154.     ;
  155.     $format['city'] = (new FormField)
  156.         ->setName('city')
  157.         ->setLabel(
  158.             $this->translator->trans(
  159.                 'City', [], 'Shop.Forms.Labels'
  160.             )
  161.         )
  162.         ->setRequired(true)
  163.     ;
  164.  
  165.         $format['email'] = (new FormField())
  166.             ->setName('email')
  167.             ->setType('email')
  168.             ->setLabel(
  169.                 $this->translator->trans(
  170.                     'Email', [], 'Shop.Forms.Labels'
  171.                 )
  172.             )
  173.             ->setRequired(true)
  174.         ;
  175.  
  176.         if ($this->ask_for_password) {
  177.             $format['password'] = (new FormField())
  178.                 ->setName('password')
  179.                 ->setType('password')
  180.                 ->setLabel(
  181.                     $this->translator->trans(
  182.                         'Password', [], 'Shop.Forms.Labels'
  183.                     )
  184.                 )
  185.                 ->setRequired($this->password_is_required)
  186.             ;
  187.         }
  188.  
  189.         if ($this->ask_for_new_password) {
  190.             $format['new_password'] = (new FormField())
  191.                 ->setName('new_password')
  192.                 ->setType('password')
  193.                 ->setLabel(
  194.                     $this->translator->trans(
  195.                         'New password', [], 'Shop.Forms.Labels'
  196.                     )
  197.                 )
  198.             ;
  199.         }
  200.  
  201.         if ($this->ask_for_birthdate) {
  202.             $format['birthday'] = (new FormField())
  203.                 ->setName('birthday')
  204.                 ->setType('text')
  205.                 ->setLabel(
  206.                     $this->translator->trans(
  207.                         'Birthdate', [], 'Shop.Forms.Labels'
  208.                     )
  209.                 )
  210.                 ->addAvailableValue('placeholder', Tools::getDateFormat())
  211.                 ->addAvailableValue(
  212.                     'comment',
  213.                     $this->translator->trans('(E.g.: %date_format%)', array('%date_format%' => Tools::formatDateStr('31 May 1970')), 'Shop.Forms.Help')
  214.                 )
  215.             ;
  216.         }
  217.  
  218.         if ($this->ask_for_partner_optin) {
  219.             $format['optin'] = (new FormField())
  220.                 ->setName('optin')
  221.                 ->setType('checkbox')
  222.                 ->setLabel(
  223.                     $this->translator->trans(
  224.                         'Receive offers from our partners', [], 'Shop.Theme.Customeraccount'
  225.                     )
  226.                 )
  227.                 ->setRequired($this->partner_optin_is_required)
  228.             ;
  229.         }
  230.  
  231.         // ToDo, replace the hook exec with HookFinder when the associated PR will be merged
  232.         $additionalCustomerFormFields = Hook::exec('additionalCustomerFormFields', array(), null, true);
  233.  
  234.         if (is_array($additionalCustomerFormFields)) {
  235.             foreach ($additionalCustomerFormFields as $moduleName => $additionnalFormFields) {
  236.                 if (!is_array($additionnalFormFields)) {
  237.                     continue;
  238.                 }
  239.  
  240.                 foreach ($additionnalFormFields as $formField) {
  241.                     $formField->moduleName = $moduleName;
  242.                     $format[$moduleName . '_' . $formField->getName()] = $formField;
  243.                 }
  244.             }
  245.         }
  246.  
  247.         // TODO: TVA etc.?
  248.  
  249.         return $this->addConstraints($format);
  250.     }
  251.  
  252.     private function addConstraints(array $format)
  253.     {
  254.         $constraints = Customer::$definition['fields'];
  255.  
  256.         foreach ($format as $field) {
  257.             if (!empty($constraints[$field->getName()]['validate'])) {
  258.                 $field->addConstraint(
  259.                     $constraints[$field->getName()]['validate']
  260.                 );
  261.             }
  262.         }
  263.  
  264.         return $format;
  265.     }
  266. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top