Monday, 28 October 2013

Create Custom Registration with admin user role.


There are 2 steps.

Step 1. 
app\design\frontend\base\default\template\persistent\customer\form\register.phtml

<div class="field">
  <label for="userrole" class="required"><em>*</em><?php echo $this->__('User Role') ?></label>
  <div class="input-box">
    <?php   
     $prefix = Mage::getConfig()->getTablePrefix();
     $write = Mage::getSingleton('core/resource')->getConnection('core_read');    
     $sql  = $write->query("select value from ".$prefix."core_config_data  where path  = 'customer/create_account/default_group' ");
     $customerRoleid = $sql->fetchAll();
     $val = $customerRoleid[0]['value'];
   
     $sql1  = $write->query("select * from ".$prefix."customer_group where customer_group_id  = '".$val."' ");
     $customerRole = $sql1->fetchAll();
     ?>
    <input type="radio" name="userrole" id="userrole" value="General" />
    <?php echo $customerRole[0]['customer_group_code'];?></br>
    <?php
     $write = Mage::getSingleton('core/resource')->getConnection('core_read');    
     $sql  = $write->query("select * from ".$prefix."admin_role  where role_type ='G' and role_name != 'Administrators'");
     $GetRole = $sql->fetchAll();   
     foreach($GetRole as $Userrole)
     {
       ?>
    <input type="radio" name="userrole" id="userrole" value="<?php echo $Userrole['role_id'] ?>" />
    <?php echo $Userrole['role_name']; ?></br>
    <?php } ?>
  </div>
</div>


Step2

2) app\code\core\Mage\Customer\controllers\AccountController.php
Place this code in createPostAction() function
 $var = $this->getRequest()->getParam('userrole');   
$prefix = Mage::getConfig()->getTablePrefix();

 if ($this->getRequest()->isPost()) {
            if($var=="General") {
           
            $errors = array();

            if (!$customer = Mage::registry('current_customer')) {
                $customer = Mage::getModel('customer/customer')->setId(null);
            }

            /* @var $customerForm Mage_Customer_Model_Form */
            $customerForm = Mage::getModel('customer/form');
            $customerForm->setFormCode('customer_account_create')
                ->setEntity($customer);

            $customerData = $customerForm->extractData($this->getRequest());

            if ($this->getRequest()->getParam('is_subscribed', false)) {
                $customer->setIsSubscribed(1);
            }

            /**
             * Initialize customer group id
             */
            $customer->getGroupId();

            if ($this->getRequest()->getPost('create_address')) {
                /* @var $address Mage_Customer_Model_Address */
                $address = Mage::getModel('customer/address');
                /* @var $addressForm Mage_Customer_Model_Form */
                $addressForm = Mage::getModel('customer/form');
                $addressForm->setFormCode('customer_register_address')
                    ->setEntity($address);

                $addressData    = $addressForm->extractData($this->getRequest(), 'address', false);
                $addressErrors  = $addressForm->validateData($addressData);
                if ($addressErrors === true) {
                    $address->setId(null)
                        ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
                        ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
                    $addressForm->compactData($addressData);
                    $customer->addAddress($address);

                    $addressErrors = $address->validate();
                    if (is_array($addressErrors)) {
                        $errors = array_merge($errors, $addressErrors);
                    }
                } else {
                    $errors = array_merge($errors, $addressErrors);
                }
            }

            try {
                $customerErrors = $customerForm->validateData($customerData);
                if ($customerErrors !== true) {
                    $errors = array_merge($customerErrors, $errors);
                } else {
                    $customerForm->compactData($customerData);
                    $customer->setPassword($this->getRequest()->getPost('password'));
                    $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
                    $customerErrors = $customer->validate();
                    if (is_array($customerErrors)) {
                        $errors = array_merge($customerErrors, $errors);
                    }
                }

                $validationResult = count($errors) == 0;

                if (true === $validationResult) {
                    $customer->save();

                    Mage::dispatchEvent('customer_register_success',
                        array('account_controller' => $this, 'customer' => $customer)
                    );

                    if ($customer->isConfirmationRequired()) {
                        $customer->sendNewAccountEmail(
                            'confirmation',
                            $session->getBeforeAuthUrl(),
                            Mage::app()->getStore()->getId()
                        );
                        $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())));
                        $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
                        return;
                    } else {
                        $session->setCustomerAsLoggedIn($customer);
                        $url = $this->_welcomeCustomer($customer);
                        $this->_redirectSuccess($url);
                        return;
                    }
                } else {
                    $session->setCustomerFormData($this->getRequest()->getPost());
                    if (is_array($errors)) {
                        foreach ($errors as $errorMessage) {
                            $session->addError($errorMessage);
                        }
                    } else {
                        $session->addError($this->__('Invalid customer data'));
                    }
                }
            } catch (Mage_Core_Exception $e) {
                $session->setCustomerFormData($this->getRequest()->getPost());
                if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                    $url = Mage::getUrl('customer/account/forgotpassword');
                    $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);
                    $session->setEscapeMessages(false);
                } else {
                    $message = $e->getMessage();
                }
                $session->addError($message);
            } catch (Exception $e) {
                $session->setCustomerFormData($this->getRequest()->getPost())
                    ->addException($e, $this->__('Cannot save the customer.'));
            }
       
        }
            else {  
            $write = Mage::getSingleton('core/resource')->getConnection('core_read');
            $sql  = $write->query("select * from ".$prefix."admin_role  where role_type ='G' and role_name != 'Administrators'");
            $GetRole = $sql->fetchAll();
           
            $data1 = $this->getRequest()->getPost();
            if($data1['userrole'] == 'General') {
                $role = '0';
            }
            else {
                $role = $data1['userrole'];
            }   
       
            $model = Mage::getModel('admin/user')->load();
            $email_array = explode('@',$data1['email']);
            $username = $email_array[0];
            $data = array('username'=>$username,'firstname'=>$data1['firstname'],'lastname'=>$data1['lastname'],
'email'=>$data1['email'],'password'=>$data1['password'],'password_confirmation'=>$data1['confirmation'],
'is_active'=>'1','user_roles'=>'','page'=>'1','assigned_user_role'=>'','role_name'=>'');               
            $model->setData($data);
            $model->save();
            $uRoles = array('0'=>$role);
            $model->setRoleIds($uRoles)
             ->setRoleUserId($model->getUserId())
             ->saveRelations();
            $session->addSuccess($this->__('User Created Successfully.'));
            $this->_redirectSuccess('*/*/index', array('_secure'=>true));
       
            }
        }

No comments:

Post a Comment