Guest User

AccountController.php

a guest
Feb 7th, 2012
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'Mage/Customer/controllers/AccountController.php';
  4.  
  5. class Elbert_Sample_Customer_AccountController extends Mage_Customer_AccountController
  6. {
  7.     public function createPostAction()
  8.     {
  9.         $session = $this->_getSession();
  10.         if ($session->isLoggedIn()) {
  11.             $this->_redirect('*/*/');
  12.             return;
  13.         }
  14.         $session->setEscapeMessages(true); // prevent XSS injection in user input
  15.         if ($this->getRequest()->isPost()) {
  16.  
  17.             $errors = array();
  18.  
  19.             if (!$customer = Mage::registry('current_customer')) {
  20.                 $customer = Mage::getModel('customer/customer')->setId(null);
  21.             }
  22.  
  23.             /* @var $customerForm Mage_Customer_Model_Form */
  24.             $customerForm = Mage::getModel('customer/form');
  25.             $customerForm->setFormCode('customer_account_create')
  26.                 ->setEntity($customer);
  27.  
  28.             $customerData = $customerForm->extractData($this->getRequest());
  29.  
  30.             if ($this->getRequest()->getParam('is_subscribed', false)) {
  31.                 $customer->setIsSubscribed(1);
  32.             }
  33.  
  34.             /**
  35.              * Initialize customer group id
  36.              */
  37.             $customer->getGroupId();
  38.  
  39.             if ($this->getRequest()->getPost('create_address')) {
  40.                 /* @var $address Mage_Customer_Model_Address */
  41.                 $address = Mage::getModel('customer/address');
  42.                 /* @var $addressForm Mage_Customer_Model_Form */
  43.                 $addressForm = Mage::getModel('customer/form');
  44.                 $addressForm->setFormCode('customer_register_address')
  45.                     ->setEntity($address);
  46.  
  47.                 $addressData    = $addressForm->extractData($this->getRequest(), 'address', false);
  48.                 $addressErrors  = $addressForm->validateData($addressData);
  49.  
  50.                 if ($addressErrors === true) {
  51.                     $address->setId(null)
  52.                         ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
  53.                         ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
  54.                     $addressForm->compactData($addressData);
  55.                     $customer->addAddress($address);
  56.  
  57.                     $addressErrors = $address->validate();
  58.                     if (is_array($addressErrors)) {
  59.                         $errors = array_merge($errors, $addressErrors);
  60.                     }
  61.                 } else {
  62.                     $errors = array_merge($errors, $addressErrors);
  63.                 }
  64.  
  65.                 // Tundra/Elbert -->
  66.                 if ($this->getRequest()->getPost('create_shipping_address')) {
  67.                     $shippingAddress = Mage::getModel('customer/address');
  68.  
  69.                     $shippingAddressForm = Mage::getModel('customer/form');
  70.                     $shippingAddressForm->setFormCode('customer_register_address')
  71.                         ->setEntity($shippingAddress);
  72.  
  73.                     $shippingAddressData = array(
  74.                         'firstname'  => $addressData['firstname'],
  75.                         'lastname'   => $addressData['lastname'],
  76.                         'company'    => $this->getRequest()->getPost('shipping_company'),
  77.                         'street'     => $this->getRequest()->getPost('shipping_street'),
  78.                         'city'       => $this->getRequest()->getPost('shipping_city'),
  79.                         'country_id' => $this->getRequest()->getPost('shipping_country_id'),
  80.                         'region'     => $this->getRequest()->getPost('shipping_region'),
  81.                         'region_id'  => $this->getRequest()->getPost('shipping_region_id'),
  82.                         'postcode'   => $this->getRequest()->getPost('shipping_postcode'),
  83.                         'telephone'  => $this->getRequest()->getPost('shipping_telephone'),
  84.                         'fax'        => $this->getRequest()->getPost('shipping_fax')
  85.                         );
  86.  
  87.                     $shippingAddressErrors = $addressForm->validateData($shippingAddressData);
  88.  
  89.                     if ($shippingAddressErrors === true) {
  90.                         $shippingAddress->setId(null)
  91.                             ->setIsDefaultBilling($this->getRequest()->getParam('shipping_default_billing', false))
  92.                             ->setIsDefaultShipping($this->getRequest()->getParam('shipping_default_shipping', false));
  93.  
  94.                         $shippingAddressForm->compactData($shippingAddressData);
  95.  
  96.                         $customer->addAddress($shippingAddress);
  97.  
  98.                         $shippingAddressErrors = $shippingAddress->validate();
  99.  
  100.                         if (is_array($shippingAddressErrors)) {
  101.                             $errors = array_merge($errors, $shippingAddressErrors);
  102.                         }
  103.                     } else {
  104.                         $errors = array_merge($errors, $shippingAddressErrors);
  105.                     }
  106.                 }
  107.                 // <-- Tundra/Elbert
  108.             }
  109.  
  110.             try {
  111.                 $customerErrors = $customerForm->validateData($customerData);
  112.                 if ($customerErrors !== true) {
  113.                     $errors = array_merge($customerErrors, $errors);
  114.                 } else {
  115.                     $customerForm->compactData($customerData);
  116.                     $customer->setPassword($this->getRequest()->getPost('password'));
  117.                     $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
  118.                     $customerErrors = $customer->validate();
  119.                     if (is_array($customerErrors)) {
  120.                         $errors = array_merge($customerErrors, $errors);
  121.                     }
  122.                 }
  123.  
  124.                 $validationResult = count($errors) == 0;
  125.  
  126.                 if (true === $validationResult) {
  127.                     $customer->save();
  128.  
  129.                     if ($customer->isConfirmationRequired()) {
  130.                         $customer->sendNewAccountEmail('confirmation', $session->getBeforeAuthUrl());
  131.                         $session->addSuccess($this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())));
  132.                         $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
  133.                         return;
  134.                     } else {
  135.                         $session->setCustomerAsLoggedIn($customer);
  136.                         $url = $this->_welcomeCustomer($customer);
  137.                         $this->_redirectSuccess($url);
  138.                         return;
  139.                     }
  140.                 } else {
  141.                     $session->setCustomerFormData($this->getRequest()->getPost());
  142.                     if (is_array($errors)) {
  143.                         foreach ($errors as $errorMessage) {
  144.                             $session->addError($errorMessage);
  145.                         }
  146.                     } else {
  147.                         $session->addError($this->__('Invalid customer data'));
  148.                     }
  149.                 }
  150.             } catch (Mage_Core_Exception $e) {
  151.                 $session->setCustomerFormData($this->getRequest()->getPost());
  152.                 if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
  153.                     $url = Mage::getUrl('customer/account/forgotpassword');
  154.                     $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
  155.                     $session->setEscapeMessages(false);
  156.                 } else {
  157.                     $message = $e->getMessage();
  158.                 }
  159.                 $session->addError($message);
  160.             } catch (Exception $e) {
  161.                 $session->setCustomerFormData($this->getRequest()->getPost())
  162.                     ->addException($e, $this->__('Cannot save the customer.'));
  163.             }
  164.         }
  165.  
  166.         $this->_redirectError(Mage::getUrl('*/*/create', array('_secure' => true)));
  167.     }
Advertisement
Add Comment
Please, Sign In to add comment