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

Title Control v1.0

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 25, 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

    Path to PHP:

    This feature allows admins to create an optional PHP file that can control any aspect of the thread title. Here's an example that will remove all quotes from the thread title:

    PHP:
    <?php

    // get title
    $title $this->get('title');

    // remove quotes
    if (substr_count($title'&quot;') > 0){
        
    $patterns[0] = '/\&quot;/';
        
    $replacements[0] = '';
        
    $title preg_replace($patterns$replacements$title);
    }

    // set title
    $this->set('title'$title);
    ?>
     
    Last edited: Oct 25, 2013
    Amaury likes this.
  2. AndyB

    AndyB Well-Known Member

    The directory structure:

    --Andy
    ----TitleControl
    ------DataWriter
    --------Discussion
    ----------Thread.php
    ----Listener.php
     
    Amaury likes this.
  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()
        {
            
    //########################################
            // default code start
            //########################################
            
            
    if ($this->get('prefix_id') && $this->isChanged('node_id'))
            {
                if (!
    $this->_getPrefixModel()->getPrefixIfInForum($this->get('prefix_id'), $this->get('node_id')))
                {
                    
    $this->set('prefix_id'0); // invalid prefix
                
    }
            }
            
            
    //########################################
            // default code end
            //########################################        
            
            // 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 -> Path to PHP    
            
    $pathToPHP XenForo_Application::get('options')->pathToPHP;            
            
            
    // 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);
            }
            
            
    // path to php
            
    if ($pathToPHP != '')
            {        
                include(
    $pathToPHP);
            }
        }
    }

    ?>
     
    Last edited: Oct 25, 2013
    Amaury likes this.
  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';
        }
    }

    ?>
     
    Amaury likes this.
  5. AndyB

    AndyB Well-Known Member

    Code Event Listener:

    pic001.jpg
     
    Amaury likes this.
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    You didn't call the parent function. This is bad practice and can cause issues with other addons.
     
    AndyB and xf_phantom like this.
  7. AndyB

    AndyB Well-Known Member

    Thank you for bringing this to my attention, Jeremy.

    Is this the correct code?

    PHP:
    class Andy_TitleControl_DataWriter_Discussion_Thread extends XFCP_Andy_TitleControl_DataWriter_Discussion_Thread
    {    
        protected function 
    _discussionPreSave()
        {
            
    // call parent function
            
    $parent parent::_discussionPreSave();
    ...
     
  8. Jeremy

    Jeremy XenForo Moderator Staff Member

    You don't need to store the return.
     
    AndyB likes this.
  9. AndyB

    AndyB Well-Known Member

    Thank you, Jeremy.

    PHP:
    class Andy_TitleControl_DataWriter_Discussion_Thread extends XFCP_Andy_TitleControl_DataWriter_Discussion_Thread
    {    
        protected function 
    _discussionPreSave()
        {
            
    // call parent function
            
    parent::_discussionPreSave();
     

Share This Page