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

Creating the add-on Smilie Count v1.1

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

  1. AndyB

    AndyB Well-Known Member

    Description:

    The Smilie Count add-on will limit the number of smilies allowed in a post. By default the add-on will limit five smilies per post.

    To change the maximum number of smilies allowed in a post:

    Admin CP -> Options -> Messages -> Maximum Smilies Per Post

    Requirements:

    This add-on only works on XenForo v1.2 and above.
     
    Hugilore111 and Amaury like this.
  2. AndyB

    AndyB Well-Known Member

    This tutorial will show the steps required to create this add-on:
    1. Create the directory structure
    2. Create the New Add-on
    3. Create the Listener.php file
    4. Create the Code Event Listener
    5. Create the DataWriter.php file
    6. Create the New Phrase
    7. Add Option
     
    Last edited: Oct 6, 2013
    Hugilore111 and Amaury like this.
  3. AndyB

    AndyB Well-Known Member

    Create the directory structure:

    library
    --Andy
    ----SmilieCount
    ----DataWriter.php
    ----Listener.php
     
    Amaury likes this.
  4. AndyB

    AndyB Well-Known Member

    Create the New Add-on:

    pic001.jpg
     
    Last edited: Oct 6, 2013
    Hugilore111 and Amaury like this.
  5. AndyB

    AndyB Well-Known Member

    Create the Listener.php file:

    PHP:
    <?php

    class Andy_SmilieCount_Listener
    {    
        public static function 
    loadClassDatawriter($class, array &$extend)
        {
            
    $extend[] = 'Andy_SmilieCount_DataWriter';
        } 
    }

    ?>
     
    Amaury likes this.
  6. AndyB

    AndyB Well-Known Member

    Create the Code Event Listener:

    pic002.jpg
     
    Last edited: Oct 6, 2013
    Hugilore111 and Amaury like this.
  7. AndyB

    AndyB Well-Known Member

    Create the DataWriter.php file:

    PHP:
    <?php

    class Andy_SmilieCount_DataWriter extends XFCP_Andy_SmilieCount_DataWriter
    {
        public function 
    preSave()
        {
            
    // make sure preSave() is only run once
            
    if ($this->_preSaveCalled)
            {
                return;
            }
                                            
            
    // 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
            {
                
    $this->_preSaveCalled true;
                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();
            }        
        }   
    }

    ?>
     
    Amaury likes this.
  8. AndyB

    AndyB Well-Known Member

    Create the New Phrase:

    pic003.jpg
     
    Last edited: Oct 5, 2013
    Amaury likes this.
  9. AndyB

    AndyB Well-Known Member

    Add Option:

    This will add the Admin CP option to allow changing the Maximum Smilies Per Message.

    Admin CP -> Options -> Messages -> + Add Option

    pic001.jpg pic002.jpg
     
    Hugilore111 likes this.
  10. AndyB

    AndyB Well-Known Member

    Hugilore111 and tajhay like this.

Share This Page