/**
  * No validation errors occured, so we register the customer and send
  * mails with further instructions for the double opt-in procedure.
  *
  * @param SS_HTTPRequest $data       SS session data
  * @param Form           $form       the form object
  * @param array          $formData   CustomHTMLForms session data
  * @param bool           $doRedirect Set to true to redirect after submitSuccess
  *
  * @return void
  * 
  * @author Sebastian Diel <*****@*****.**>,
  *         Roland Lehmann <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 28.01.2015
  */
 protected function submitSuccess($data, $form, $formData, $doRedirect = true)
 {
     $anonymousCustomer = false;
     /*
      * Logout anonymous users and save their shoppingcart temporarily.
      */
     if (SilvercartCustomer::currentUser()) {
         $anonymousCustomer = SilvercartCustomer::currentUser();
         SilvercartCustomer::currentUser()->logOut();
     }
     // Aggregate Data and set defaults
     $formData['MemberID'] = Member::currentUserID();
     $formData['Locale'] = Translatable::get_current_locale();
     if ($this->demandBirthdayDate()) {
         $formData['Birthday'] = $formData['BirthdayYear'] . '-' . $formData['BirthdayMonth'] . '-' . $formData['BirthdayDay'];
         if ($this->UseMinimumAgeToOrder()) {
             if (!SilvercartConfig::CheckMinimumAgeToOrder($formData['Birthday'])) {
                 $this->errorMessages['BirthdayDay'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY'), 'BirthdayDay' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
                 $this->errorMessages['BirthdayMonth'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY'), 'BirthdayMonth' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
                 $this->errorMessages['BirthdayYear'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY'), 'BirthdayYear' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
                 $this->setSubmitSuccess(false);
                 return $this->submitFailure($data, $form);
             }
         }
     }
     // Create new regular customer and perform a log in
     $customer = new Member();
     // Pass shoppingcart to registered customer and delete the anonymous
     // customer.
     if ($anonymousCustomer) {
         $newShoppingCart = $anonymousCustomer->getCart()->duplicate(true);
         foreach ($anonymousCustomer->getCart()->SilvercartShoppingCartPositions() as $shoppingCartPosition) {
             $newShoppingCartPosition = $shoppingCartPosition->duplicate(false);
             $newShoppingCartPosition->SilvercartShoppingCartID = $newShoppingCart->ID;
             $newShoppingCartPosition->write();
             $shoppingCartPosition->transferToNewPosition($newShoppingCartPosition);
         }
         $customer->SilvercartShoppingCartID = $newShoppingCart->ID;
         $anonymousCustomer->delete();
     }
     $customer->castedUpdate($formData);
     $customer->write();
     $customer->logIn();
     $customer->changePassword($formData['Password']);
     $customerGroup = $this->getTargetCustomerGroup($formData);
     if ($customerGroup) {
         $customer->Groups()->add($customerGroup);
     }
     // Create ShippingAddress for customer and populate it with registration data
     $address = new SilvercartAddress();
     $address->castedUpdate($formData);
     $country = DataObject::get_by_id('SilvercartCountry', (int) $formData['Country']);
     if ($country) {
         $address->SilvercartCountryID = $country->ID;
     }
     $address->write();
     $this->extend('updateRegisteredAddress', $address, $data, $form, $formData);
     //connect the ShippingAddress and the InvoiceAddress to the customer
     $customer->SilvercartShippingAddressID = $address->ID;
     $customer->SilvercartInvoiceAddressID = $address->ID;
     $customer->SilvercartAddresses()->add($address);
     $customer->write();
     // Remove from the anonymous newsletter recipients list
     if (SilvercartAnonymousNewsletterRecipient::doesExist($customer->Email)) {
         $recipient = SilvercartAnonymousNewsletterRecipient::getByEmailAddress($customer->Email);
         if ($recipient->NewsletterOptInStatus) {
             $customer->NewsletterOptInStatus = 1;
             $customer->NewsletterConfirmationHash = $recipient->NewsletterOptInConfirmationHash;
             $customer->write();
         }
         SilvercartAnonymousNewsletterRecipient::removeByEmailAddress($customer->Email);
     }
     if ($customer->SubscribedToNewsletter && !$customer->NewsletterOptInStatus) {
         SilvercartNewsletter::subscribeRegisteredCustomer($customer);
     }
     $this->extend('updateRegisteredCustomer', $customer, $data, $form, $formData);
     if ($doRedirect) {
         // Redirect to welcome page
         if (array_key_exists('backlink', $formData) && !empty($formData['backlink'])) {
             $this->controller->redirect($formData['backlink']);
         } else {
             $this->controller->redirect($this->controller->PageByIdentifierCode('SilvercartRegisterConfirmationPage')->Link());
         }
     }
 }
 /**
  * We intercept the submit handler since we have to alter some field
  * checks depending on the status of the field "InvoiceAddressAsShippingAddress".
  *
  * @param SS_HTTPRequest $data submit data
  * @param Form           $form form object
  *
  * @return ViewableData
  *
  * @author Sascha Koehler <*****@*****.**>,
  *         Sebastian Diel <*****@*****.**>
  * @since 20.01.2014
  */
 public function submit($data, $form)
 {
     // Disable the check instructions if the shipping address shall be
     // the same as the invoice address.
     if ($data['InvoiceAddressAsShippingAddress'] == '1') {
         $this->deactivateValidationFor('Shipping_Salutation');
         $this->deactivateValidationFor('Shipping_AcademicTitle');
         $this->deactivateValidationFor('Shipping_FirstName');
         $this->deactivateValidationFor('Shipping_Surname');
         $this->deactivateValidationFor('Shipping_Addition');
         $this->deactivateValidationFor('Shipping_Street');
         $this->deactivateValidationFor('Shipping_StreetNumber');
         $this->deactivateValidationFor('Shipping_Postcode');
         $this->deactivateValidationFor('Shipping_City');
         $this->deactivateValidationFor('Shipping_PhoneAreaCode');
         $this->deactivateValidationFor('Shipping_Phone');
         $this->deactivateValidationFor('Shipping_Country');
     }
     $formData = $data->postVars();
     if ($this->UseMinimumAgeToOrder()) {
         $formData['Birthday'] = $formData['BirthdayYear'] . '-' . $formData['BirthdayMonth'] . '-' . $formData['BirthdayDay'];
         if (!SilvercartConfig::CheckMinimumAgeToOrder($formData['Birthday'])) {
             $this->errorMessages['BirthdayDay'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY') . ' - ' . _t('SilvercartPage.DAY'), 'BirthdayDay' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
             $this->errorMessages['BirthdayMonth'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY') . ' - ' . _t('SilvercartPage.MONTH'), 'BirthdayMonth' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
             $this->errorMessages['BirthdayYear'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY') . ' - ' . _t('SilvercartPage.YEAR'), 'BirthdayYear' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
             $this->setSubmitSuccess(false);
             return $this->submitFailure($data, $form);
         }
     }
     parent::submit($data, $form);
 }
 /**
  * executed if there are no valdation errors on submit
  * Form data is saved in session
  *
  * @param SS_HTTPRequest $data             contains the frameworks form data
  * @param Form           $form             not used
  * @param array          $registrationData contains the modules form data
  *
  * @return void
  * 
  * @author Sebastian Diel <*****@*****.**>,
  *         Roland Lehmann <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 15.11.2014
  */
 protected function submitSuccess($data, $form, $registrationData)
 {
     $member = SilvercartCustomer::currentUser();
     // -------------------------------------------------------------------
     // process data
     // -------------------------------------------------------------------
     // Password
     unset($registrationData['PasswordCheck']);
     if (empty($registrationData['Password'])) {
         unset($registrationData['Password']);
     }
     // birthday
     if ($this->demandBirthdayDate()) {
         if (!empty($registrationData['BirthdayDay']) && !empty($registrationData['BirthdayMonth']) && !empty($registrationData['BirthdayYear'])) {
             $registrationData['Birthday'] = $registrationData['BirthdayYear'] . '-' . $registrationData['BirthdayMonth'] . '-' . $registrationData['BirthdayDay'];
         }
         if ($this->UseMinimumAgeToOrder()) {
             if (!SilvercartConfig::CheckMinimumAgeToOrder($registrationData['Birthday'])) {
                 $this->errorMessages['BirthdayDay'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY'), 'BirthdayDay' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
                 $this->errorMessages['BirthdayMonth'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY'), 'BirthdayMonth' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
                 $this->errorMessages['BirthdayYear'] = array('message' => SilvercartConfig::MinimumAgeToOrderError(), 'fieldname' => _t('SilvercartPage.BIRTHDAY'), 'BirthdayYear' => array('message' => SilvercartConfig::MinimumAgeToOrderError()));
                 $this->setSubmitSuccess(false);
                 return $this->submitFailure($data, $form);
             }
         }
     }
     $member->castedUpdate($registrationData);
     if (!$member->SubscribedToNewsletter) {
         $member->NewsletterOptInStatus = false;
         $member->NewsletterConfirmationHash = '';
     }
     $member->write();
     if ($member->SubscribedToNewsletter && !$member->NewsletterOptInStatus) {
         $confirmationHash = SilvercartNewsletter::createConfirmationHash($member->Salutation, $member->FirstName, $member->Surname, $member->Email);
         $member->setField('NewsletterConfirmationHash', $confirmationHash);
         $member->write();
         SilvercartNewsletter::sendOptInEmailTo($member->Salutation, $member->FirstName, $member->Surname, $member->Email, $confirmationHash);
     }
     $this->controller->redirect($this->controller->Link());
 }