Tom
Tom

Reputation: 2634

Joomla plugin event with failed registration

I have a number of menus that point to the users registration component. Depending on where the user registers I redirect them accordingly with a custom plugin.

public function onUserAfterSave($user, $isnew, $success, $msg)
{

    //get the active menu item id so we can compare what plan we are on
    $menu   = $app->getMenu();
    $active   = $menu->getActive();
    $activeId = $active->id;

    //compare active page (i.e. what menu item are we on) with the parameter set in the plugin
    //if it matches redirect accordingly - fallback is to always redirect to free plan
    if ($activeId == $this->params->get('free-reg-menu'))
    {
        //redirect required to go to main page
        JFactory::getApplication()->redirect(JRoute::_('page1'));
    }
    else if ($activeId == $this->params->get('bv-reg-menu')) 
    {
        //redirect to step 2 of main plan payment processing
        JFactory::getApplication()->redirect(JRoute::_('page2'));
    }
    else if ($activeId == $this->params->get('prem-reg-menu'))
    {
        //redirect to step 2 of value plan payment processing
        JFactory::getApplication()->redirect(JRoute::_('page3'));
    }
    else
    {
        //other stuff
    }

}

Depending on what menus were assigned in this plugin I redirect the user to a specific page. This all works quite well.

My problem is when registration fails (ie. duplicate username, wrong matching passwords etc.) the page does a refresh due to the server side validation. This refresh brings the page away from the original link. It seem to redirect to the first registration menu page I created.

For example: I'm on a registration page and I enter wrong credentials then the page refreshes to a completely different page. Any ideas? I guess an extension for AJAX registration might address this but I'd like to avoid that for now.

Upvotes: 2

Views: 369

Answers (2)

Tom
Tom

Reputation: 2634

Credit definitely goes to @WooDzu who stuck with it. The key was definitely session variables. But what I did was create a component that does one simple thing. Create session variables based on a url with a query string I pull out. So my component would be accessed with:

www.mysite.com/index.php?option=com_mycomp&task=registration&plan=<value>

My component then has a simple controller that looks like:

function registration()
{
    $jinput = JFactory::getApplication()->input;
    $plan = $jinput->get('plan', null, 'STRING');

    $session = JFactory::getSession();
    $session->set('plan', $plan);

    if ($plan == "page1")
    {
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=registration&plan=page1'));
    }
    else if ($plan == "page2")
    {
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=registration&plan=page2'));

    }
    else 
    {
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=registration&plan=page3'));
    }

}

This redirects to the com_users registration component with a session variable set. Then I have my plugin in place with OnUserAfterSave but it redirects based on the session variables and not the menu ids or alias.

Don't know if this is the best approach but it does seem fairly clean.

Upvotes: 0

WooDzu
WooDzu

Reputation: 4866

You could do the following:

  • perform the check within onAfterRoute instead
  • keep the result in user session
  • do the redirection from onUserAfterSave when the result is successful

I cannot think of a simpler method of doing it or any complications it makes.


System plugin

<?php
public function onAfterRoute()
{
    $app = JFactory::getApplication();

    if ($app->isSite() == false)
        return;

    //get the active menu item id so we can compare what plan we are on
    $menu   = $app->getMenu();
    $active   = $menu->getActive();
    $activeId = $active->id;

    $finalRedirect = null;

    //compare active page (i.e. what menu item are we on) with the parameter set in the plugin
    //if it matches redirect accordingly - fallback is to always redirect to free plan
    if ($activeId == $this->params->get('free-reg-menu'))
    {
        //redirect required to go to main page
        $finalRedirect = JRoute::_('page1');
    }
    else if ($activeId == $this->params->get('bv-reg-menu')) 
    {
        //redirect to step 2 of main plan payment processing
        $finalRedirect = JRoute::_('page2');
    }
    else if ($activeId == $this->params->get('prem-reg-menu'))
    {
        //redirect to step 2 of value plan payment processing
        $finalRedirect = JRoute::_('page3');
    }
    else
    {
        //other stuff
    }

    if($finalRedirect)
    {
        // Store selection in session
        JFactory::getSession()->set('my.redirection', $finalRedirect);
    }
}

User plugin

<?php
public function onUserAfterSave($user, $isnew, $success, $msg)
{
    $session = JFactory::getSession();

    if ($finalRedirect = $session->get('my.redirection')
        && $success == true
        && $isnew == true)
    {
        // Clear the session entry
        $session->set('my.redirection', null);

        JFactory::getApplication()->redirect($finalRedirect);
    }
}

Upvotes: 1

Related Questions