0

I am looking for a solution to pre-select the group a user will be added to depending on the menu item he is accessing the registration form.

Something like 2 menu items with registration form. One is just the basic user registration users are directed to during the checkout process to they can continue with their orders. The second would be a registration form for users wanting to signup to be a distributor in a Page 'Become a Distributor'. And all the registrations from there would go to a distributors group, which would require approval.

Does anyone know of any component plugin that would do that? I looked at the Extensions directory and couldn't really find anything.

  • Are you coding the registration forms yourself or using the Joomla registration form? – Terry Carter Mar 16 '16 at 18:07
  • Hi Terry, I am using joomla registration form. – user3750757 Mar 17 '16 at 2:24
  • You would definitely need a custom plug in for that then. The alternative would be creating your own forms and including a hidden field with which form is being submitted. A plug in would also require editing the save method for users to assign them to a specific group. The easiest approach would be creating your own forms. – Terry Carter Mar 17 '16 at 3:18
  • When you say create my own form you mean creating a module right? – user3750757 Mar 17 '16 at 3:51
  • Yes, easiest method would be a form module loaded on a page. The form could submit back to the current page for processing. You could then pass the form variables to a function to create the user account. I'll post a simple example when I get home in a bit or in the morning if you can't figure it out from here. – Terry Carter Mar 17 '16 at 3:53
1

I put together the following script for you and tested it on my site to be sure it worked. This should do exactly what you want to do. I will outline the steps below to implement this

  1. My code makes the assumption you are using reCaptcha on your site, if you are not you should be to reduce some of your spam registrations.
  2. Download Flexi Custom Code Module from the JED and install if in extension manager
  3. Go to Extensions > Modules
  4. You should see an unpublished module there for Flexi Custom Code, click the title to edit it.
  5. Set the title to Registration or anything else you want, then hide the title.
  6. Choose a module position to display this module in
  7. There are 4 options beneath the Custom Code box, leave PHP toggled on and toggle Clean JScript, Clean HTML, and Clean CSS off
  8. Under the Menu Assignments tab choose the 2 menu items you want for your User Registration and Partner Registration
  9. Save and Close this Module for now
  10. Go to Users > Groups
  11. Get the User Group IDs for the 2 groups you setup for your users and partners
  12. Go to Menu > Your Main Menu
  13. Get the Menu Item IDs for the 2 menu items you have assigned the module to
  14. Go back to Extensions > Modules and edit the registration module you created.
  15. Change the $Itemid variable to reflect the first menu Item that you have assigned the module to, the second will be automatic.
  16. Change the $userGroupAssignment variables to match the Group IDs you created for your users and partners. (An ID of 2 is the default registered user group ID)
  17. If you want you can change the messages that are displayed and the redirect page as well. You may want to style your form so that it is a bit prettier as well. Change the $formName variables to whatever you want the form name to display as well.
  18. Save the module and close it.
  19. Go to your site and test registration for your registered users and partners

    <?php 
    //GET APPLICATION
    $app = JFactory::getApplication();
    
    //GET POSTED DATA
    $JInput = JFactory::getApplication()->input;
    
    //GET ACTIVE MENU ITEM ID
    $Itemid = JSite::getMenu()->getActive()->id;
    
    //SET THE GROUP ASSIGNMENTS BELOW TO YOUR CUSTOM GROUP IDS FROM USERS > GROUPS
    //GET THE ITEMID OF THE FIRST MENU ITEM FROM YOUR MENU MANAGER, THE SECOND ID ISN'T NEEDED
    if($Itemid == '101'){
        $userGroupAssignment = '2';
        $formName = 'User Registration';
    }else{
        $userGroupAssignment = '10';
        $formName = 'Partner Registration';
    }
    
    //THIS CODE EXPECTS THAT YOU ARE USING RECAPTCHA FOR SECURITY VERIFICATION
    JPluginHelper::importPlugin('captcha');
    $dispatcher = JDispatcher::getInstance();
    $dispatcher->trigger('onInit','dynamic_recaptcha_1');
    $captchaCode = $JInput->get('recaptcha_response_field', '', 'string');
    
    //GET POSTED REGISTRATION FORM DATA
    $form = $JInput->get('jform','','array');
    
    //ADD JOOMLA USER FUNCTION
    function addJoomlaUser($name, $username, $email, $password, $password2, $userGroupAssignment)
    {
        jimport('joomla.user.helper');
        $data = array(
            "name"=>$name,
            "username"=>$username,
            "password"=>$password,
            "password2"=>$password2,
            "email"=>$email,
            "block"=>0,
            "sendEmail"=>0,
            "requireReset"=>0,
            "groups"=>array($userGroupAssignment)
        );
    
        $user = new JUser;
        if(!$user->bind($data)){
            throw new Exception("Could not bind data. Error: " . $user->getError());
        }
        if(!$user->save()){
            throw new Exception("Could not register user. Error: " . $user->getError());
        }
    }
    
    
    //CHECK IF THE FORM HAS BEEN SUBMITTED, IF NOT LOAD THE FORM
    if(isset($form['registerUser']) && $form['registerUser'] == 'registerNewUser'){
    
        $res = $dispatcher->trigger('onCheckAnswer',$captchaCode);
        if(!$res[0]){
            //QUEUE FAILED MESSAGE
            JFactory::getApplication()->enqueueMessage('An invalid Captcha code was submitted and registration has failed. Please try again.', 'error');
        }else{
            //REGISTER THE USER
            addJoomlaUser($form['name'],$form['username'],$form['email'],$form['password1'],$form['password2'],$form['userGroup']);
    
            //QUEUE SUCCESS MESSAGE
            JFactory::getApplication()->enqueueMessage('Thank you '.$form['name'].', registration was successful. You may now login to your account.', 'message');
    
            //REDIRECT TO THE HOME PAGE
            $app->redirect(JUri::root());
        }
    }else{
        JHtml::_('behavior.formvalidation');
        ?>
        <h1><?php echo $formName;?></h1>
        <form action="<?php echo JUri::current();?>" method="post" name="adminForm" id="adminForm" class="form-validate">
            <label for="name">Name</label>
            <input id="name" type="text" name="jform[name]" required value="<?php echo $form['name'];?>" />
            <label for="username">Username</label>
            <input id="username" type="text" name="jform[username]" required value="<?php echo $form['username'];?>" />
            <label for="email">Email Address</label>
            <input id="email" type="text" name="jform[email]" value="<?php echo $form['email'];?>" required />
            <label for="password1">Password</label>
            <input id="password1" required type="password" name="jform[password1]" value="" />
            <label for="password2">Verify Password</label>
            <input id="password2" required type="password" name="jform[password2]" value="" />
            <div id="dynamic_recaptcha_1" style="width:350px; margin-bottom:15px;"></div>
            <input type="hidden" name="jform[registerUser]" value="registerNewUser" />
            <input type="hidden" name="jform[userGroup]" value="<?php echo $userGroupAssignment;?>" />
            <input type="submit" value="Register Now" />
        </form>
    <?php }?>
    

As I said, I tested this script already and it works. You should be able to tweak anything else you need to do. If you need clarification on how something works in this script or with Flexi Custom Code Module just ask.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.