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

How to Extend/Override User Ignore Methods?

Discussion in 'XenForo Development Discussions' started by Alteran Ancient, Nov 16, 2013.

  1. Alteran Ancient

    Alteran Ancient Well-Known Member

    Heya!

    So basically, I wanted to modify my Ignore facility so that users are unable to ignore people considered as extended members of staff, because one or two of my members see fit to abuse the facility to ignore those that are "in power", purely for their rank within the community.

    So, I set about making this impossible. What I wanted to do was modify the XenForo_Model_UserIgnore::canIgnoreUser method, but haven't found a way to override this feature with the add-on way to get it to do what I want it to do. I was able to directly edit the method to add my checks, but I'd rather do it via an add-on if I can.

    Long story short, this is the re-written method:
    PHP:
        public function canIgnoreUser($userId, array $user, &$error '')
        {
            
    $setError = (func_num_args() >= 3);

            if (!
    $userId)
            {
                return 
    false;
            }

            if (
    $user['is_moderator'] || $user['is_admin'])
            {
                if (
    $setError)
                {
                    
    $error = new XenForo_Phrase('staff_members_may_not_be_ignored');
                }
                return 
    false;
            }
           
            
    //Check against all staff...
            
    $groups = array($user['user_group_id']);
            
    $sGroups explode(","$user['secondary_group_ids']);
            
    $groups array_merge($groups$sGroups);
           
            if(
    in_array("6"$groups) || in_array("11"$groups) || in_array("13"$groups) || in_array("15"$groups)){
                if (
    $setError)
                {
                    
    $error = new XenForo_Phrase('staff_members_may_not_be_ignored');
                }
                return 
    false;
            }
            
    //END Check against all staff...

            
    if ($userId == $user['user_id'])
            {
                if (
    $setError)
                {
                    
    $error = new XenForo_Phrase('you_may_not_ignore_yourself');
                }
                return 
    false;
            }

            return 
    true;
        }
    How might I extend or override the default method so I can avoid directly tinkering with the source in future?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    You can use the load_class_model event to extend this function.

    As you can see there, the function only ever returns true or false in every scenario. When extending the function you need to ascertain what the parent is returning, do some stuff and either modify that response based on some custom code and then return that modified response.

    PHP:
        public function canIgnoreUser($userId, array $user, &$error '')
        {
            
    $parent parent::canIgnoreUser($userId$user$error);

            
    // Do something here.

            
    return $parent;
        }
    All you need to do is add your code.
     
    Bob likes this.
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    To extend on what @Chris Deeming has said, you can do a simple conditional check to see if you need to perform any work:
    PHP:
    public function canIgnoreUser($userId, array $user, &$error '')
        {
            
    $parent parent::canIgnoreUser($userId$user$error);
            
    $setError = (func_num_args() >= 3);

            
    // If parent is already false, we know they cannot ignore, performing more checks is a waste of power
            
    if ($parent) {
              
    // Do all your checks here, so...
              
    $groups = array($user['user_group_id']);
              
    $sGroups explode(","$user['secondary_group_ids']);
              
    $groups array_merge($groups$sGroups);
             
              if(
    in_array("6"$groups) || in_array("11"$groups) || in_array("13"$groups) || in_array("15"$groups)){
                  if (
    $setError)
                  {
                      
    $error = new XenForo_Phrase('staff_members_may_not_be_ignored');
                  }
                  return 
    false;
              }
            }

            return 
    $parent;
        }
     
    maitandat1507 and Bob like this.
  4. Alteran Ancient

    Alteran Ancient Well-Known Member

    I couldn't get the parent:: callback to work properly, but I was able to override the function as a whole by using the load_class_model event.

    Thanks for the help!
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    Feel free to let us know how it wasn't working or what errors were happening. Getting it working as a proper add-on is preferable just in case any further functionality is added in the future or you have an add-on that extends this in future. Overriding the function prevents that.
     
    tyteen4a03 and Jeremy like this.
  6. maitandat1507

    maitandat1507 New Member

    And what's about overriding the method return responseView('Abc_ViewPublic_', 'abc_view', $viewParams)?
    Code:
    <?php
    class ABC_XenForo_ControllerPublic_Tag_View extends XFCP_...
    public function actionIndex()
    {
        //code something
        $viewParams = array(
            'type' => $type,
            'abc' => $abc,
        );
        return $this->responseView('ABC_ViewPublic_ABC_View', 'abc_view', $viewParams);
    }
    I want to pass the variable to view, I think that we should use $viewParams to do, but How can i edit/add something to the $viewParams in parent's method?
    Code:
    <?php
    class XYZ_XenForo_ControllerPublic_Tag_View extends XFCP_...
    {
        public function actionIndex()
        {
            $actionIndex = parent::actionIndex();
            //How can i add more in $viewParams?
            return $actionIndex;
        }
    }
    Ex: if i want to add $viewParams['xyz'] = $newVariable into the method that override method actionIndex()?
     
    Last edited: Jan 22, 2016

Share This Page