Title Control v1.1

AndyB

Well-known member
Purpose:

The purpose of this thread is to provide additional information on the Title Control v1.0 add-on. To download and install this add-on please visit the following XenForo Resource:

http://xenforo.com/community/resources/title-control.2485/

Description:

The Title Control add-on will do the following:
  • Control maximum length of thread titles (Option)
  • Force the first character of the thread title to uppercase (Option)
  • Allows additional title control using external PHP file (Option)

Admin Options:

pic001.webp

External File:

The External File option allows the use of a PHP file that can control any aspect of the thread title. Here's an example that will make the first character of each word uppercase, but allow the admin to override the rule:

PHP:
<?php

// get userId
$visitor = XenForo_Visitor::getInstance();
$userId = $visitor['user_id'];

if ($userId != 1)
{
    // get title
    $title = $this->get('title');
      
    // uppercase the first character of each word in a string
    $title = ucwords($title);
  
    // set title
    $this->set('title', $title);
}

?>
 
The directory structure:

--Andy
----TitleControl
------DataWriter
--------Discussion
----------Thread.php
----Listener.php
 
library/Andy/TitleControl/DataWriter/Discussion/Thread.php

PHP:
<?php

class Andy_TitleControl_DataWriter_Discussion_Thread extends XFCP_Andy_TitleControl_DataWriter_Discussion_Thread
{	
	protected function _discussionPreSave()
	{
		// call parent function
		parent::_discussionPreSave();
				
		// get options from Admin CP -> Options -> Title Control -> Maximum Length   
		$maxLength = XenForo_Application::get('options')->maxLength;
		
		// get options from Admin CP -> Options -> Title Control -> Uppercase First  
		$uppercaseFirst = XenForo_Application::get('options')->uppercaseFirst;
		
		// get options from Admin CP -> Options -> Title Control -> External File  
		$externalFile = XenForo_Application::get('options')->externalFile;		
		
		// maximum length
		if ($maxLength != '')
		{
			$title = $this->get('title'); 
			
			$currentTitleLength = strlen($title);
			
			if ($currentTitleLength > $maxLength)
			{
				$title = substr($title, 0, $maxLength);
				$this->set('title', $title);			
			}			
		}
		
		// uppercase first
		if ($uppercaseFirst != '')
		{		
			$title = $this->get('title');	
			$title = ucfirst($title);
			$this->set('title', $title);
		}
		
		// include optional external file
		if ($externalFile != '')
		{		
			if (file_exists($externalFile))
			{
				include($externalFile);
			}
		}
	}
}

?>
 
library/Andy/TitleControl/Listener.php

PHP:
<?php

class Andy_TitleControl_Listener
{
	public static function Thread($class, array &$extend)
	{		
		$extend[] = 'Andy_TitleControl_DataWriter_Discussion_Thread';
	}
}

?>
 
Back
Top Bottom