public function execute()
 {
     $f = waRequest::param('f');
     $fid = waRequest::param('fid');
     $parent = waRequest::param('parent');
     $css_class = waRequest::param('css_class');
     $new_field = false;
     if (!$f instanceof waContactField) {
         $new_field = true;
         $f = new waContactStringField($fid, '', array('app_id' => 'shop'));
     }
     $prefix = 'options';
     if ($parent) {
         $prefix .= '[' . $parent . '][fields]';
     }
     static $ftypes = null;
     if ($ftypes === null) {
         $ftypes = array('NameSubfield' => _w('Text (input)'), 'Email' => _w('Text (input)'), 'Address' => _w('Address'), 'Text' => _w('Text (textarea)'), 'String' => _w('Text (input)'), 'Select' => _w('Select'), 'Phone' => _w('Text (input)'), 'IM' => _w('Text (input)'), 'Url' => _w('Text (input)'), 'Date' => _w('Date'), 'Composite' => _w('Composite field group'), 'Checkbox' => _w('Checkbox'));
     }
     $form = waContactForm::loadConfig(array('_default_value' => $f), array('namespace' => "{$prefix}[{$fid}]"));
     // Get default value
     $default_value = null;
     if (!$new_field && $f->getParameter('_disabled')) {
         $settings = wa('shop')->getConfig()->getCheckoutSettings();
         if (!isset($settings['contactinfo'])) {
             $settings = wa('shop')->getConfig()->getCheckoutSettings(true);
         }
         $fields_config = ifset($settings['contactinfo']['fields'], array());
         if ($parent) {
             if (!empty($fields_config[$parent]['fields'][$fid]['hidden'])) {
                 $default_value = ifset($fields_config[$parent]['fields'][$fid]['value']);
             }
         } else {
             if (!empty($fields_config[$fid]['hidden'])) {
                 $default_value = ifset($fields_config[$fid]['value']);
             }
         }
         if ($default_value !== null) {
             $form->setValue('_default_value', $default_value);
         }
     }
     $this->view->assign('f', $f);
     $this->view->assign('fid', $fid);
     $this->view->assign('form', $form);
     $this->view->assign('parent', $parent);
     $this->view->assign('prefix', $prefix);
     $this->view->assign('uniqid', 'f' . uniqid());
     $this->view->assign('new_field', $new_field);
     $this->view->assign('tr_classes', $css_class);
     $this->view->assign('default_value', $default_value);
     $this->view->assign('ftypes', $ftypes);
 }
 /**
  * @return waContactForm
  */
 protected function getForm()
 {
     // Read all contact fields and find all enabled for "my profile"
     $fields = array('photo' => new waContactHiddenField('photo', _ws('Photo'))) + waContactFields::getAll('person') + array('password' => new waContactPasswordField('password', _ws('Password'))) + array('password_confirm' => new waContactPasswordField('password_confirm', _ws('Confirm password')));
     $domain = wa()->getRouting()->getDomain();
     $domain_config_path = wa()->getConfig()->getConfigPath('domains/' . $domain . '.php', true, 'site');
     if (file_exists($domain_config_path)) {
         $domain_config = (include $domain_config_path);
     } else {
         $domain_config = array();
     }
     $enabled = array();
     foreach ($fields as $fld_id => $f) {
         if (!empty($domain_config['personal_fields'][$fld_id])) {
             $enabled[$fld_id] = $f;
             if ($fld_id === 'password') {
                 $enabled[$fld_id . '_confirm'] = $fields[$fld_id . '_confirm'];
             }
         }
     }
     // If nothing found, fall back to the default field list
     if (!$enabled) {
         foreach (array('firstname', 'middlename', 'lastname', 'email', 'phone', 'password') as $fld_id) {
             if (!empty($fields[$fld_id])) {
                 $enabled[$fld_id] = $fields[$fld_id];
             }
         }
     }
     return waContactForm::loadConfig($enabled, array('namespace' => 'profile'));
 }
 /**
  * Execute step
  *
  * @return bool
  */
 public function execute()
 {
     $contact = $this->getContact();
     if (!$contact) {
         $contact = new waContact();
     }
     $data = waRequest::post('customer');
     if ($data && is_array($data)) {
         foreach ($data as $field => $value) {
             $contact->set($field, $value);
         }
     }
     $this->form = shopHelper::getCustomerForm();
     if ($shipping = $this->getSessionData('shipping') && !waRequest::post('ignore_shipping_error')) {
         $shipping_step = new shopOnestepCheckoutShipping();
         $rate = $shipping_step->getRate($shipping['id'], isset($shipping['rate_id']) ? $shipping['rate_id'] : null, $contact);
         if (!$rate || is_string($rate)) {
             // remove selected shipping method
             $this->setSessionData('shipping', null);
             /*
              $errors = array();
              $errors['all'] = sprintf(_w('We cannot ship to the specified address via %s.'), $shipping['name']);
              if ($rate) {
              $errors['all'] .= '<br> <strong>'.$rate.'</strong><br>';
              }
              $errors['all'] .= '<br> '._w('Please double-check the address above, or return to the shipping step and select another shipping option.');
              $errors['all'] .= '<input type="hidden" name="ignore_shipping_error" value="1">';
              wa()->getView()->assign('errors', $errors);
              return false;
             */
         }
     }
     if (wa()->getUser()->isAuth()) {
         $contact->save();
     } else {
         $errors = array();
         if (waRequest::post('create_user')) {
             $login = waRequest::post('login');
             if (!$login) {
                 $errors['email'][] = _ws('Required');
             }
             if (!waRequest::post('password')) {
                 $errors['password'] = _ws('Required');
             }
             $email_validator = new waEmailValidator();
             if (!$email_validator->isValid($login)) {
                 $errors['email'] = $email_validator->getErrors();
             }
             if (!$errors) {
                 $contact_model = new waContactModel();
                 if ($contact_model->getByEmail($login, true)) {
                     $errors['email'][] = _w('Email already registered');
                 }
             }
             if (!$errors) {
                 $contact->set('email', $login);
                 $contact->set('password', waRequest::post('password'));
             } else {
                 if (isset($errors['email'])) {
                     $errors['email'] = implode(', ', $errors['email']);
                 }
                 wa()->getView()->assign('errors', $errors);
                 return false;
             }
         }
         $this->setSessionData('contact', $contact);
     }
     if ($comment = waRequest::post('comment')) {
         $this->setSessionData('comment', $comment);
     }
     if (!$this->form->isValid($contact)) {
         return false;
     }
     return true;
 }
Example #4
0
 /**
  * Returns instance of class waContactForm.
  *
  * @param int|waContact|null $id Optional id of contact or contact object whose data must be pre-filled in contact form.
  * @param bool $ensure_address Whether address fields must be included regardless of store's contact fields settings.
  * @return waContactForm
  */
 public static function getCustomerForm($id = null, $ensure_address = false)
 {
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     if (!isset($settings['contactinfo'])) {
         $settings = wa('shop')->getConfig()->getCheckoutSettings(true);
     }
     $fields_config = ifset($settings['contactinfo']['fields'], array());
     $address_config = ifset($fields_config['address'], array());
     unset($fields_config['address']);
     if (wa()->getEnv() == 'backend') {
         // new order
         if (!isset($fields_config['address.shipping']) || !$id) {
             $fields_config['address.shipping'] = array();
         } elseif (!empty($fields_config['address.shipping']) && $id && $id instanceof waContact) {
             $address = $id->getFirst('address.shipping');
             if ($address && !empty($address['data'])) {
                 foreach ($address['data'] as $subfield => $v) {
                     if (!isset($fields_config['address.shipping']['fields'][$subfield])) {
                         $fields_config['address.shipping']['fields'][$subfield] = array();
                     }
                 }
             }
         }
     }
     if ($ensure_address && !isset($fields_config['address.billing']) && !isset($fields_config['address.shipping'])) {
         $fields_config['address'] = $address_config;
     }
     $form = waContactForm::loadConfig($fields_config, array('namespace' => 'customer'));
     if ($id) {
         if (is_numeric($id)) {
             $contact = new waContact($id);
             $contact->getName();
             // make sure contact exists; throws exception otherwise
         } elseif ($id instanceof waContact) {
             $contact = $id;
         }
         if (isset($contact)) {
             $form->setValue($contact);
         }
     }
     return $form;
 }
 public function getAddressForm($method_id, waShipping $plugin, $config, $contact_address, $address_form)
 {
     $config_address = isset($config['contactinfo']['fields']['address.shipping']) ? $config['contactinfo']['fields']['address.shipping'] : (isset($config['contactinfo']['fields']['address']) ? $config['contactinfo']['fields']['address'] : array());
     $address_fields = $plugin->requestedAddressFields();
     $disabled_only = $address_fields === array() ? false : true;
     if ($address_fields === false || $address_fields === null) {
         return false;
     }
     foreach ($address_fields as $f) {
         if ($f !== false) {
             $disabled_only = false;
             break;
         }
     }
     $address = array();
     if ($disabled_only) {
         $allowed = $plugin->allowedAddress();
         if (count($allowed) == 1) {
             $one = true;
             if (!isset($config_address['fields'])) {
                 $address_field = waContactFields::get('address');
                 foreach ($address_field->getFields() as $f) {
                     $fields[$f->getId()] = array();
                 }
             } else {
                 $fields = $config_address['fields'];
             }
             foreach ($allowed[0] as $k => $v) {
                 if (is_array($v)) {
                     $one = false;
                     break;
                 } else {
                     $fields[$k]['hidden'] = 1;
                     $fields[$k]['value'] = $v;
                 }
             }
             foreach ($address_fields as $k => $v) {
                 if ($v === false && isset($fields[$k])) {
                     unset($fields[$k]);
                 }
             }
             if ($one) {
                 $address = $config_address;
                 $address['fields'] = $fields;
             }
         }
     } else {
         if (isset($config_address['fields'])) {
             $fields = $config_address['fields'];
             if ($address_fields) {
                 foreach ($fields as $f_id => $f) {
                     if (isset($address_fields[$f_id])) {
                         foreach ($address_fields[$f_id] as $k => $v) {
                             $fields[$f_id][$k] = $v;
                         }
                     } else {
                         unset($fields[$f_id]);
                     }
                 }
                 foreach ($address_fields as $f_id => $f) {
                     if (!isset($fields[$f_id])) {
                         $fields[$f_id] = $f;
                     }
                 }
             }
             $address_fields = $fields;
         }
         if ($address_fields) {
             $address = array('fields' => $address_fields);
         }
     }
     if (!$address_form && !empty($address['fields'])) {
         foreach ($address['fields'] as $k => $v) {
             if (empty($contact_address[$k])) {
                 $address_form = true;
             }
         }
     }
     if ($address_form) {
         if (!empty($config['shipping']['prompt_type'])) {
             if (!empty($address['fields'])) {
                 foreach ($address['fields'] as $k => $v) {
                     if (empty($v['cost'])) {
                         unset($address['fields'][$k]);
                     }
                 }
                 if (!$address['fields']) {
                     return null;
                 }
             } else {
                 $empty = true;
                 foreach ($address_fields as $f) {
                     if (!empty($f['cost'])) {
                         $empty = false;
                         break;
                     }
                 }
                 if ($empty) {
                     return null;
                 }
             }
         }
         return waContactForm::loadConfig(array('address.shipping' => $address), array('namespace' => 'customer_' . $method_id));
     } else {
         return null;
     }
 }