I'd like to run some php code after a member registers

Hi Nasr,

I commented out my code altogether, so I'm only using the example Chris provided. Unfortunately it breaks the ability for the DataWriter to save Custom User Field data.
 
This is the code Chris provided.

Code:
<?php

class sbr_register_index
{
    public static function registerEmail($class, array &$extend)
    {
        if ($class == 'XenForo_DataWriter_User')
        {
            $extend[] = 'sbr_register_DataWriter_User';
        }
    }
}

?>

Code:
<?php

class sbr_register_DataWriter_User extends XFCP_sbr_register_DataWriter_User
{
    protected function _postSave()
    {
        $db = $this->_db;

        if ($this->isInsert() && $this->isChanged('user_id'))
        {
            // This code should execute if new record is inserted and user_id is being set (so should be only new users)
        }
    }
}

?>
 
Steps to reproduce:

1) Create Add-on
2) Create Code Event Listener
3) Upload the two files provided by Chris
4) Register as new user
5) Fill out registration questions including a Custom User Field
6) Submit New Registration
7) Verify with phpmyadmin that xf_user_field_view has not been updated
 
if ($this->isesert() && $this->isChanged('user_id'))


needs to be
if ($this->isInsert()){
}
2. what do you want to achieve with your addon?
 
if ($this->isesert() && $this->isChanged('user_id'))


needs to be
if ($this->isInsert()){
}
2. what do you want to achieve with your addon?
I disagree.

The User DataWriter handles many User related tables, so your code probably won't do any different. But, mine is wrong too because user_id will always be changed on an insert to any of the xf_user_ tables... therefore... to correctly catch only new users being created, something like this is probably more appropriate:

Code:
		if ($this->isInsert() && $this->isChanged('user_id') && $this->isChanged('username') && $this->isChanged('email'))
		{
			// This code should execute if new record is inserted and user_id is being set (so should be only new users)	
		}

Those three fields are ONLY available in xf_user so, technically, anything within the if statement will only execute on an insert to the xf_user table - .e.g. registration.
 
is xenforo changing the code automatic?


isInsert gets always isesert
I'm not sure what you're saying?

isInsert returns true whenever data is being entered into the tables defined in XenForo_DataWriter_User::_getFields() function.

So isInsert is true whenever:

xf_user, xf_user_profile, xf_user_option, xf_user_privacy, xf_user_option

Has data inserted. During registration, pretty much all of those fields has data inserted so isInsert by itself is a little too broad.
 
Actually... I've just realised a massive omission on my part... I'm sorry.

What I've said is accurate, but the code example I provided is OVERWRITING the postSave function instead of extending it.

It needs to look like this. Sorry by bad:

Code:
<?php

class sbr_register_DataWriter_User extends XFCP_sbr_register_DataWriter_User
{
	protected function _postSave()
	{
		$parent = parent::_postSave();
		
		$db = $this->_db;

		if ($this->isInsert() && $this->isChanged('user_id') && $this->isChanged('username') && $this->isChanged('email'))
		{	
			// Additional code here
		}
		
		return $parent;
	}
}
 
It's still overwriting it if it's not a insert;) Your code won't call the parent method if it's not a insert
the parent call needs to be outside of the if condition.
 
Hi Chris and havanclub,

Thank you both for your time. Looking forward to a solution to this problem.
 
2. what do you want to achieve with your addon?

I would like to be able to run a php script that will run after a new user has registered. The script emails me notifying that a new user has registered (I use moderation) and will include important information that helps me identify if the new registration is legit or a spammer.
 
Hi Chris,

One option I have is to run a cron job every 15 minutes that calls my php script. That will email me reminding me that there are new registrations to moderate. It's not as elegant as using an add-on that runs after a new registration, but getting emails every 15 minutes would be fine too.
 
Oh my... what a complicated solution to such an easy requirement.

Andy, firstly welcome to Xenforo... :)

As a general advice, if you want to do something after a user does something with default Xenforo, the first option to look for would be try and find the Xenforo controller which does the action which the user did, extend the controller, call the parent, do your stuff and return. Only when what you want to extend is finer action done from within a controller should you go to extend the Model and if it is further deep, what you want to do, then you go to the DataWriter.

In most simple cases, extending the controller action would serve your purpose.

In your case, what you should do is extend the actionRegister() method within XenForo_ControllerPublic_Register. I have the addon attached for you, tested and it works and took all of 5 mins to make.

Replace the "Hello World" with whatever it is you want to do.

Regards
 

Attachments

By the way if you want all the details of the user which just got registered the $user variable is present in $response->params['user']
 
Thank you, sadikb.

I appreciate the warm welcome and time you took to educate us. I'll try your code now and report back.
 
Top Bottom