Unable to extend both Model_Post and ControllerPublic_Post

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
 
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.
 
Back
Top Bottom