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

Unable to extend both Model_Post and ControllerPublic_Post

Discussion in 'XenForo Development Discussions' started by AndyB, Jan 31, 2014.

  1. AndyB

    AndyB Well-Known Member

    I have successfully extended the Model_Post::canEditPost and here is the code:

    PHP:
    <?php

    class Andy_CanEditPost_Model_Post extends XFCP_Andy_CanEditPost_Model_Post
    {
        public function 
    canEditPost(array $post, array $thread, array $forum, &$errorPhraseKey '', array $nodePermissions null, array $viewingUser null)
        {      
            
    // get parent
            
    $parent parent::canEditPost($post$thread$forum$errorPhraseKey$nodePermissions$viewingUser);

            if (!
    $parent)
            {
                
    $this->standardizeViewingUserReference($viewingUser);
                  
                if (
    $viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
                {
                    
    $parent true;
                }
            }
          
            
    // return parent
            
    return $parent;
        }
    }

    ?>
    However if I also try to extend the ControllerPublic_Post, I get an error. Here's the code:

    PHP:
    <?php

    class Andy_CanEditPost_ControllerPublic_Post extends XFCP_Andy_CanEditPost_ControllerPublic_Post
    {  
        public function 
    actionTest()
        {  
            
    // if not a super admin, return
            
    if (!XenForo_Visitor::getInstance()->isSuperAdmin())
            {
                return;
            }
        }
    }

    ?>
    The error I get is this:

    Fatal error: Cannot redeclare class XFCP_Andy_CanEditPost_Model_Post in /home/southbay/www/forums/library/XenForo/Application.php(513) : eval()'d code on line 1
     
  2. AndyB

    AndyB Well-Known Member

    Anyone have any ideas why I'm seeing this fatal error?
     
  3. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    Show us your Listeners, the problem might lie there. Where you are doing the extend[]
     
    AndyB likes this.
  4. Chris D

    Chris D XenForo Developer Staff Member

    You either have two files somewhere in any of your directories which have the same class, e.g. class Andy_CanEditPost_Model_Post extends XFCP_Andy_CanEditPost_Model_Post

    Or your code event listener for that add-on load_class_model doesn't have an event hint.

    If you don't have an event hint (in this case presumably it should be XenForo_Model_Post) then in your Listener you should be doing something like:

    PHP:
    if ($class == 'XenForo_Model_Post')
    {
        
    $extend[] = 'Andy_CanEditPost_Model_Post';
    }
    Event hints and/or that conditional will prevent your extended class from being applied more than once.
     
  5. AndyB

    AndyB Well-Known Member

    You nailed it Rigel Kentaurus! The problem was my Listener.php. It only had one function.

    Thank you.
     
  6. AndyB

    AndyB Well-Known Member

    Thank you, Chris. I really appreciate your time as well in helping.
     

Share This Page