1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Overriding a Function?

Discussion in 'XenForo Development Discussions' started by James, May 17, 2011.

  1. James

    James Well-Known Member

    I'm trying to override the actionPersonalDetailsSave() function to prevent saving if the gender had changed (basically preventing users from changing their gender).

    I've tried something simple like:
    PHP:
    Class James_ControllerPublic_Account extends XFCP_James_ControllerPublic_Account
    {
    public function 
    actionPersonalDetailsSave()
      {
    $userId XenForo_Visitor::getUserId();
    $writer XenForo_DataWriter::create('XenForo_DataWriter_User');
    $writer->setExistingData($userId);
    $writer->save();
          if (
    $writer->isChanged('gender')){
    return 
    $this->responseError("You cannot change your gender once it has been set!");
    }
      }
    }
    But this doesn't work and throws this error:
    Has anyone got any ideas?
     
  2. Kier

    Kier XenForo Developer Staff Member

    It looks like it's failing because gender is not changed, and you have no controller response in that case.
     
  3. James

    James Well-Known Member

    Is it possible to use the same function name but only change part of the function?

    I'll look into it some more and see if I can come up with anything.
     
  4. James

    James Well-Known Member

    I've been looking in XenForo_ControllerResponse_Error because I'm making use of responseError(), would I need to extend the ControllerResponse_Error for my own add-on or could I use XenForo's? I'm a little baffled at this bit.
     
  5. Kier

    Kier XenForo Developer Staff Member

    What do you actually want to do?
     
  6. James

    James Well-Known Member

    I'm basically trying to allow gender selection at registration but prevent it from being edited on the personal details page.
     
  7. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    Use this instead

     
    James likes this.
  8. Kier

    Kier XenForo Developer Staff Member

    That should work nicely :)
     
  9. James

    James Well-Known Member

    This didn't work after all!
     
  10. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    why?
     
  11. James

    James Well-Known Member

    Didn't throw an error on gender change, it just allowed it. It's probably my method rather than yours.
     
  12. ragtek

    ragtek Guest

    Your code isn't logical

    $userId = XenForo_Visitor::getUserId();
    $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
    $writer->setExistingData($userId);
    $writer->save();
    if ($writer->isChanged('gender')){
    return $this->responseError("You cannot change your gender once it has been set!");
    }

    You should check before the save if the gender is changed and not after the save, because here it will be too late
     
  13. James

    James Well-Known Member

    Hmm... maybe I'm not looking at this the right way. None of the below methods work:
    Code:
    public function actionPersonalDetailsSave()
    	{
    		$userId = XenForo_Visitor::getUserId();
    		$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
    		$writer->setExistingData($userId);
    		$writer->preSave();
    		if ($writer->isChanged('gender'))
    		{
    			return $this->responseError("You cannot change your gender once it has been set!");
    		}	else	{
    			$writer->save();
    		}
    		return parent::actionPersonalDetailsSave();
    	
    	}
    
    Code:
    public function actionPersonalDetailsSave()
    	{
    		$userId = XenForo_Visitor::getUserId();
    		$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
    		$writer->setExistingData($userId);
    		if ($writer->isChanged('gender'))
    		{
    			return $this->responseError("You cannot change your gender once it has been set!");
    		}	else	{
    			$writer->save();
    		}
    		return parent::actionPersonalDetailsSave();
    	
    	}
     
  14. Martin Aronsen

    Martin Aronsen Active Member

    What about this?
    Code:
        protected function _saveVisitorSettings($settings, &$errors, $extras = array())
        {
            $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
            $writer->setExistingData(XenForo_Visitor::getUserId());
            $writer->bulkSet($settings);
            
            if ($writer->isChanged('gender'))
            {
                return $this->responseError("You cannot change your gender once it has been set!");
            }
            
            return parent::_saveVisitorSettings( $settings, $errors, $extras );
        }
     
  15. James

    James Well-Known Member

    I'll try it in the morning, I'm In bed now!
     
  16. James

    James Well-Known Member

    That throws an error:
    Code:
    PHP <br /> <b>Fatal error</b>: Call to undefined method XenForo_ControllerResponse_Error::getMergedData() in <b>/home/whyhq/data/www/whyhq.net/secret/library/XenForo/ControllerPublic/Account.php</b> on line <b>304</b><br />
    
     
  17. Tilkißey

    Tilkißey Well-Known Member

    you ve to overwrite _saveVisitorSettings function like that

    PHP:
    protected function _saveVisitorSettings($settings, &$errors$extras = array())
    {
    $writer XenForo_DataWriter::create('XenForo_DataWriter_User');
    $writer->setExistingData(XenForo_Visitor::getUserId());
    $writer->bulkSet($settings);

       if(
    $writer->isChanged('gender'))
    {
       throw new 
    XenForo_Exception('You cannot change your gender once it has been set!'true);
    }

    if (
    $writer->isChanged('email')
    && 
    XenForo_Application::get('options')->get('registrationSetup''emailConfirmation')
    && !
    $writer->get('is_moderator')
    && !
    $writer->get('is_admin')
    )
    {
    switch (
    $writer->get('user_state'))
    {
    case 
    'moderated':
    case 
    'email_confirm':
    $writer->set('user_state''email_confirm');
    break;

    default:
    $writer->set('user_state''email_confirm_edit');
    }
    }

    foreach (
    $extras AS $methodName => $data)
    {
    if (
    method_exists($writer$methodName))
    {
    call_user_func(array($writer$methodName), $data);
    }
    }

    $writer->preSave();

    if (
    $dwErrors $writer->getErrors())
    {
    $errors = (is_array($errors) ? $dwErrors $errors $dwErrors);
    return 
    false;
    }

    $writer->save();
    return 
    $writer;
    }
     
    James likes this.
  18. SheepCow

    SheepCow Well-Known Member

    It would probably make more sense to override the DataWriter preSave (or equivalent if it's private), then you can check if it's an update or an insert -- allow gender changes on insert but throw an error on update.

    If you do it in the DataWriter you should then cover everywhere that attempts to change the user automagically.
     
    Marc and Kier like this.
  19. James

    James Well-Known Member

    The function above seems to work fine, if the gender is changed at all then it throws an error.

    All I'm doing now is working on differentiating between a male/female gender and a male/female -> unspecified change.
     
  20. Marc

    Marc Well-Known Member

    OK I really have to ask .... Your members change sex regularly? LOL
     
    Fuhrmann and ragtek like this.

Share This Page