/**
  *
  * @param Controller $controller
  * @param String $name, Name of the form
  */
 function __construct($controller, $name)
 {
     $member = Member::currentUser();
     $requiredFields = null;
     if ($member && $member->exists()) {
         $fields = $member->getEcommerceFields(true);
         $clearCartAndLogoutLink = ShoppingCart_Controller::clear_cart_and_logout_link();
         $loginMessage = "<span class=\"customerName\">" . Convert::raw2xml($member->FirstName) . ' ' . Convert::raw2xml($member->Surname) . '</span>, ' . '<a href="' . $clearCartAndLogoutLink . '">' . _t('Account.LOGOUT', 'Log out now?') . '</a>';
         if ($loginMessage) {
             $loginField = new ReadonlyField('LoggedInAsNote', _t("Account.LOGGEDIN", "You are currently logged in as "), $loginMessage);
             $loginField->dontEscape = true;
             $fields->push($loginField);
         }
         $actions = new FieldList(new FormAction('submit', _t('Account.SAVE', 'Save Changes')));
         if ($order = ShoppingCart::current_order()) {
             if ($order->getTotalItems()) {
                 $actions->push(new FormAction('proceed', _t('Account.SAVE_AND_PROCEED', 'Save changes and proceed to checkout')));
             }
         }
     } else {
         if (!$member) {
             $member = new Member();
         }
         $fields = new FieldList();
         $urlParams = $controller->getURLParams();
         $backURLLink = Director::baseURL();
         if ($urlParams) {
             foreach ($urlParams as $urlParam) {
                 if ($urlParam) {
                     $backURLLink = Controller::join_links($backURLLink, $urlParam);
                 }
             }
         }
         $backURLLink = urlencode($backURLLink);
         $fields->push(new LiteralField('MemberInfo', '<p class="message good">' . _t('OrderForm.MEMBERINFO', 'If you already have an account then please') . " <a href=\"Security/login?BackURL=" . $backURLLink . "\">" . _t('OrderForm.LOGIN', 'log in') . '</a>.</p>'));
         $memberFields = $member->getEcommerceFields();
         if ($memberFields) {
             foreach ($memberFields as $memberField) {
                 $fields->push($memberField);
             }
         }
         $passwordField = new PasswordField('PasswordCheck1', _t('Account.PASSWORD', 'Password'));
         $passwordFieldCheck = new PasswordField('PasswordCheck2', _t('Account.PASSWORDCHECK', 'Password (repeat)'));
         $fields->push($passwordField);
         $fields->push($passwordFieldCheck);
         $actions = new FieldList(new FormAction('creatememberandaddtoorder', _t('Account.SAVE', 'Create Account')));
     }
     $requiredFields = ShopAccountForm_Validator::create($member->getEcommerceRequiredFields());
     parent::__construct($controller, $name, $fields, $actions, $requiredFields);
     $this->setAttribute("autocomplete", "off");
     //extensions need to be set after __construct
     //extension point
     $this->extend('updateFields', $fields);
     $this->setFields($fields);
     $this->extend('updateActions', $actions);
     $this->setActions($actions);
     $this->extend('updateValidator', $requiredFields);
     $this->setValidator($requiredFields);
     if ($member) {
         $this->loadDataFrom($member);
     }
     $oldData = Session::get("FormInfo.{$this->FormName()}.data");
     if ($oldData && (is_array($oldData) || is_object($oldData))) {
         $this->loadDataFrom($oldData);
     }
     $this->extend('updateShopAccountForm', $this);
 }
 /**
  * Ensures member unique id stays unique and other basic stuff...
  * @param array $data = Form Data
  * @return Boolean
  */
 function php($data)
 {
     $this->form->saveDataToSession();
     if (Member::currentUserID()) {
         $allowExistingEmail = false;
     } else {
         $allowExistingEmail = true;
     }
     $valid = parent::php($data, $allowExistingEmail);
     if ($this->form->uniqueMemberFieldCanBeUsed($data)) {
         //do nothing
     } else {
         $uniqueFieldName = Member::get_unique_identifier_field();
         $this->validationError($uniqueFieldName, _t("OrderForm.EMAILFROMOTHERUSER", 'Sorry, an account with that email is already in use by another customer. If this is your email address then please log in first before placing your order.'), "required");
         $valid = false;
     }
     if (!$valid) {
         $this->form->sessionMessage(_t("OrderForm.ERRORINFORM", "We could not proceed with your order, please check your errors below."), "bad");
         $this->form->messageForForm("OrderForm", _t("OrderForm.ERRORINFORM", "We could not proceed with your order, please check your errors below."), "bad");
     }
     return $valid;
 }
 /**
  * Ensures member unique id stays unique and other basic stuff...
  * @param array $data = Form Data
  * @return Boolean
  */
 function php($data)
 {
     $valid = parent::php($data);
     //Note the exclamation Mark - only applies if it return FALSE.
     if (!$this->form->uniqueMemberFieldCanBeUsed($data)) {
         $uniqueField = Member::get_unique_identifier_field();
         $this->validationError($uniqueField, _t("OrderForm.EMAILFROMOTHERUSER", 'Sorry, an account with that email is already in use by another customer. If this is your email address then please log in first before placing your order.'), "required");
         $valid = false;
     }
     if (!$valid) {
         $this->form->sessionMessage(_t("OrderForm.ERRORINFORM", "We could not proceed with your order, please check your errors below."), "bad");
         $this->form->messageForForm("OrderForm", _t("OrderForm.ERRORINFORM", "We could not proceed with your order, please check your errors below."), "bad");
     }
     return $valid;
 }