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

sadikb,

Actually the two solutions proposed aren't that much different anyway. Whether you extend one function in the DataWriter or one function in the Controller it's the same difference.

In many ways, my solution will ultimately be less complicated.

With your solution you have to handle the instance where the user could encounter an error (mismatched passwords, CAPTCHA failure etc.). With my solution, it will only execute once the record is inserted and the user has definitely been created.
 
  • Like
Reactions: Bob
Thank you, sadikb.

I appreciate the warm welcome and time you took to educate us. I'll try your code now and report back.
What's wrong with the code I suggested??

Are you getting errors? My solution should be ultimately easier to work with.
 
sadikb,

Actually the two solutions proposed aren't that much different anyway. Whether you extend one function in the DataWriter or one function in the Controller it's the same difference.

In many ways, my solution will ultimately be less complicated.

With your solution you have to handle the instance where the user could encounter an error (mismatched passwords, CAPTCHA failure etc.). With my solution, it will only execute once the record is inserted and the user has definitely been created.


Chris, this is not a competition... :)

To ensure error checking, just one conditional is sufficient...

PHP:
public function actionRegister()
    {
        $response = parent::actionRegister();
 
        if(!$response->params['errors'])
        {
            //Do Stuff
            echo 'Hello World!';
            Zend_Debug::dump($response);
        }
 
        return $response;
 
    }

The problem with extending postSave is that postSave is called from all sorts of places. For instance if you update the value of custom user fields post save is called. Yes, there you could simply check for isInsert and it would yield same results. However the logic here is that Andy wants to do something after a new user registers, not after a user record is saved (which could happen from AdminCP or user himself editing his settings etc.) So extending the actionRegister method means that Addon code (even if it's just a conditional) is only executed once someone registers.

Also,in my experience it's always easier, specially for someone new to MVC, to first approach extending the Controller as it helps in understanding the way the software works.

Regards
 
I am aware it's not a competition. But I already provided a valid solution and now I fear we'll be confusing the poor guy.

If I provided a solution that would work, and this isn't a competition, why are you calling my solution "beyond complicated" when it takes the same number of steps?

Well, anyway, thanks for your input. Your method is valid assuming you work around its pit falls and aren't bothered about dealing with other ways a user could be inserted (Facebook registration, add-ons which may insert users, etc.).
 
Chris, my sincere apologies. The latest code you wrote I thought was not working. Your last post indicating you were going to bed and the comment "fixed" was interpreted incorrectly by me.

Your help is greatly appreciated.
 
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']

Hi sadikb,

Your code works perfect so far. Thank you very much.

Would you be so kind as to expand a bit on this (text quoted). I would like to get the following two items and I need the proper code to put into my php script.

username

and

Custom User Field, where field_id = 2

If the Custom User Field information is not exposed, I could query the database from my script, I'll need the userid to do that.
 
Hi sadikb,

Your code works perfect so far. Thank you very much.

Would you be so kind as to expand a bit on this (text quoted). I would like to get the following two items and I need the proper code to put into my php script.

username

and

Custom User Field, where field_id = 2

If the Custom User Field information is not exposed, I could query the database from my script, I'll need the userid to do that.

Replace actionRegister in the addon with this code:
PHP:
public function actionRegister()
    {
        $response = parent::actionRegister();
 
        if(!isset($response->params['errors']))
        {
            //Do Stuff
            $visitor = XenForo_Visitor::getInstance();
         
            //Below Line will dump the full $visitor
            //Zend_Debug::dump($visitor);
         
            //Username
            echo $visitor['username'];
         
            //Custom User Fields
         
            $userCustomFields  = $visitor['customFields'];
         
            echo $userCustomFields['2'];
        }
 
        return $response;
 
    }

By the way, as Chris rightly pointed out, this addon is only running when a user is registering from the front end with the registration form.

P.S I am logging off, so no more replies from me for the night... :)
 
Everything is working perfect now (I ended up using sadikb example). Thank you sadikb and Chris for all your time. I learned a lot from both of you and I'm thankful for your time. I hope to be able to pay it forward when I get to know Xenforo better.
 
Well it looks like I spoke too soon. As Chris pointed out if there's a password mismatch then sadikb's code would throw an error, which it did. So I put in the code "if(!isset($response->params['errors'])) " as sadikb described, unfortunately adding that code causes an error.

I'm going to try Chris' code now and will report back in a bit.
 
Hi Chris,

Your latest code works perfect. I can't thank you enough for seeing this through.
Sorry to "bump" a very old thread, but I'm curious how you got access to the user data through Chris' solution?

Using:
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
            $visitor = XenForo_Visitor::getInstance();
         
            //Below Line will dump the full $visitor
            Zend_Debug::dump($visitor);
       }
       
       return $parent;
   }
}

I tried using the code someone else provided to get the visitor info but it all just returned blank data

Code:
//Do Stuff
            $visitor = XenForo_Visitor::getInstance();
         
            //Below Line will dump the full $visitor
            Zend_Debug::dump($visitor);

The entire debug is empty variables (no user_id, username, email, etc etc). Wondering if that stuff is accessible in Chris' solution or if it only works for Sadik's
 
Ah, yupp go figure that worked like a charm. Thank you!

For anyone who comes across this, FWIW it looks like some of the Visitor data is available using Chris' solution but its not "named" yet..? Not sure just saw this in the XF error log in the admin console while swapping out the add-ons.

Code:
array(3) {
  ["url"] => string(##) "https://-.com/register/register"
  ["_GET"] => array(0) {
  }
  ["_POST"] => array(16) {
   ["username"] => string(0) ""
   ["d8b8047f1662ab18f3dffb4c812b7b1f"] => string(7) "as48sda"
   ["avatar_upload"] => string(1) "0"
   ["e06edb1f578029a312c47596ad90a630"] => string(14) "as48sd@sfd.com"
   ["04e06b4a1a7cb29f509835f76954f091"] => string(0) ""
   ["dob_month"] => string(1) "2"
   ["dob_day"] => string(2) "11"
   ["dob_year"] => string(4) "1980"
   ["location"] => string(9) "as48sdasd"
   ["-"] => array(1) {
     ["-"] => string(0) ""
   }
   ["custom_fields_shown"] => array(1) {
     [0] => string(18) "---"
   }
   ["99de8945ed293d392f70f2e267c39f13"] => string(16) "America/New_York"
   ["recaptcha_challenge_field"] => string(206) "03AJz9lvRlhcnyfhbmVrjpuVwqOLrMaO4zC9g8IMM-71d5i4hSj7Hr_gCEoaU-s7FBKsrsPrKp--p8NH_LbPLfVTiU0AhheeAHXpR4F442HgchmGiD7k395MSvOv_zxrfQ6nj9mqoQ_Lw_RLO8SVmt8IQnllYwpiOMiBBlODSW53zYCaXKlijLrWdCETJL5vJ5UqYoSs5B1tOx"
   ["recaptcha_response_field"] => string(12) "5650 toppina"
   ["_xfToken"] => string(8) "********"
   ["reg_key"] => string(32) "---"
  }
}

In any case swapping them out works, thanks again and sorry to necro this!
 
Top Bottom