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

Title Control v1.1

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

  1. AndyB

    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.jpg

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

    ?>
     
    Pereira likes this.
  2. AndyB

    AndyB Well-Known Member

    The directory structure:

    --Andy
    ----TitleControl
    ------DataWriter
    --------Discussion
    ----------Thread.php
    ----Listener.php
     
  3. AndyB

    AndyB Well-Known Member

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

    ?>
     
  4. AndyB

    AndyB Well-Known Member

    library/Andy/TitleControl/Listener.php

    PHP:
    <?php

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

    ?>
     
  5. AndyB

    AndyB Well-Known Member

    Code Event Listener:

    pic002.jpg
     
  6. rhodes

    rhodes Active Member

    Thank you @AndyB . That helped me a lot. Finally I was able to write my addon ;-)
     
    AndyB likes this.

Share This Page