Creating the add-on Smilie Count v1.1

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.
 
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:
Create the directory structure:

library
--Andy
----SmilieCount
----DataWriter.php
----Listener.php
 
Create the Listener.php file:

PHP:
<?php

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

?>
 
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();
		}		
	}   
}

?>
 
Add Option:

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

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

pic001.webp pic002.webp
 
Back
Top Bottom