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

How can I properly extend Xenforo_Model_Post::canEditPost

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

  1. AndyB

    AndyB Well-Known Member

    My default edit time limit for regular members is 24 hours. What I would like to do is create an add-on which will allow me to override this default value on a per post basis.

    If I hack the following code like this it works perfect:

    PHP:
        public function canEditPost(array $post, array $thread, array $forum, &$errorPhraseKey '', array $nodePermissions null, array $viewingUser null)
        {
            
    $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser$nodePermissions);
          
            
    // start hack
          
            
    if ($viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
            {
                return 
    true;
            }      
              
            
    // end hack

    <snip>
    However when I try to extend Xenforo_Model_Post::canEditPost the Edit link appears properly below the post, but clicking the link produces a "You do not have permission to view this page or perform this action." error message.

    Here is my add-on code which only partially works:

    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 (
    $viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
            {
                return 
    true;
            }                 
          
            
    // return parent
            
    return $parent;
          
        }
    }

    ?>
    Thank you.
     
  2. Syndol

    Syndol Guest

    You must ensure that the viewingUser array is populated.

    PHP:
    $canEditPost parent::canEditPost($post$thread$forum$errorPhraseKey$nodePermissions$viewingUser);

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

    return 
    $canEditPost;
     
    AndyB likes this.
  3. AndyB

    AndyB Well-Known Member

    Hi Syndol,

    That worked perfect.

    Thank you kindly for your time!
     
  4. AndyB

    AndyB Well-Known Member

    I know it doesn't make any difference, but I prefer to use the variable $parent instead.

    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;
        }
    }

    ?>
     

Share This Page