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

XenForo_Phrase is being called twice in public function preSave()

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 5, 2013.

  1. AndyB

    AndyB Well-Known Member

    I'm updating the code in my add-on called Smilie Count. When a new post is being created and there are too many smilies, I return this error. The problem is the error message is displayed twice.

    PHP:

    return $this->error(new XenForo_Phrase('please_reduce_the_number_of_smilies_maximum_allowed_is') . ' ' $maxSmilieCount);

    Here's the error message:

    pic001.jpg
     
  2. AndyB

    AndyB Well-Known Member

    Here's the full code:

    PHP:
    <?php

    class Andy_SmilieCount_DataWriter extends XFCP_Andy_SmilieCount_DataWriter
    {
        public function 
    preSave()
        {     
            
    // get smilies from xf_smilie table
            
    $smilies $this->getModelFromCache('XenForo_Model_Smilie')->getAllSmilies();
                 
            
    // get $maxSmilieCount from Admin CP -> Options -> Messages -> Maximum Smilies Per Post 
            
    $maxSmilieCount XenForo_Application::get('options')->maxSmilieCount;     
         
            
    //########################################
            // count number of smilies in message
         
            
    $smilieCount 0;
            foreach (
    $smilies as $smilie)
            {
                
    $smilieCount += substr_count($this->get('message'), $smilie['smilie_text']);
            }          
         
            
    // show error message if $smilie_count exceeds limit
            
    if ($smilieCount $maxSmilieCount)
            {
                return 
    $this->error(new XenForo_Phrase('please_reduce_the_number_of_smilies_maximum_allowed_is') . ' ' $maxSmilieCount);
            } else {
                
    // run the original save() function
                
    return parent::preSave();
            }     
        }
    }

    ?>
     
    Last edited: Oct 5, 2013
  3. AndyB

    AndyB Well-Known Member

    If I edit a post or create a new thread with too many smilies, the error message is displayed once as it should.
     
  4. Amaury

    Amaury Well-Known Member

    Could be a conflict with other add-ons.

    Have you tried disabling them?
     
  5. AndyB

    AndyB Well-Known Member

    Thank you for the excellent suggestion, Amaury. I just tried disabling all the other add-ons and unfortunately the problem still occurs.
     
  6. Nobita.Kun

    Nobita.Kun Well-Known Member

    I think you should extends
    `protected function _preSave()
    {

    }`
     
  7. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    When I try that I get the following error:

    b>Fatal error</b>: Cannot override final method XenForo_DataWriter_DiscussionMessage::_preSave()
     
  8. Nobita.Kun

    Nobita.Kun Well-Known Member

    What DataWriter you are extending?
    If you're extends XenForo_DataWriter_Discussion_Thread or XenForo_DataWriter_DiscussionMessage_Post
    So should be:
    `_discussionPreSave` Or `_messagePreSave`
     
  9. AndyB

    AndyB Well-Known Member

  10. Amaury

    Amaury Well-Known Member

    (y)

    Sorry I couldn't help more. I'm not really server-savvy, so about all I could ask was that add-on question.
     

Share This Page