/**
  * 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());
         }
     }
 }
 /**
  * Checks if the given email address is allocated by an anonymous
  * newsletter subscriber.
  *
  * @param string $email The email address to check
  * 
  * @return boolean
  * 
  * @author Sascha Koehler <*****@*****.**>
  * @since 25.08.2011
  */
 public static function isEmailAllocatedByAnonymousRecipient($email)
 {
     $emailIsAllocated = false;
     $anonymousRecipient = SilvercartAnonymousNewsletterRecipient::getByEmailAddress($email);
     if ($anonymousRecipient) {
         $emailIsAllocated = true;
     }
     return $emailIsAllocated;
 }
 /**
  * Final step for the newsletter opt-in.
  *
  * @return array message for the template
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 16.06.2014
  */
 public function doConfirmation()
 {
     $statusMessage = $this->ConfirmationFailureMessage;
     $anonymousCustomer = true;
     if (isset($_GET['h'])) {
         $hash = Convert::raw2sql(urldecode($_GET['h']));
         if ($hash) {
             $recipient = SilvercartAnonymousNewsletterRecipient::getByHash($hash);
             if ($recipient) {
                 $customer = $recipient;
             } else {
                 $customer = Member::get()->filter('NewsletterConfirmationHash', $hash)->first();
                 if ($customer) {
                     $anonymousCustomer = false;
                 }
             }
             if ($customer) {
                 if ($customer->NewsletterOptInStatus) {
                     $statusMessage = $this->AlreadyConfirmedMessage;
                 } else {
                     if ($anonymousCustomer) {
                         SilvercartAnonymousNewsletterRecipient::doConfirmationByHash($hash);
                     } else {
                         $customer->setField('NewsletterOptInStatus', true);
                         $customer->write();
                     }
                     $statusMessage = $this->ConfirmationSuccessMessage;
                     $this->sendConfirmationMail($customer->Salutation, $customer->FirstName, $customer->Surname, $customer->Email);
                 }
             }
         }
     }
     return $this->customise(array('message' => $statusMessage));
 }
 /**
  * Returns an SilvercartAnonymousNewsletterRecipient object with the given
  * email address.
  *
  * @param string $confirmationHash ...
  *
  * @return mixed SilvercartAnonymousNewsletterRecipient|boolean false
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 16.06.2014
  */
 public static function getByHash($confirmationHash)
 {
     $recipient = SilvercartAnonymousNewsletterRecipient::get()->filter('NewsletterOptInConfirmationHash', $confirmationHash)->first();
     return $recipient;
 }