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

Flag Thread v1.0

Discussion in 'XenForo Development Discussions' started by AndyB, Dec 6, 2013.

  1. AndyB

    AndyB Well-Known Member

    Purpose:

    The purpose of this thread is to provide additional information on the Flag Thread v1.0 add-on.

    To download and install this add-on please visit the following XenForo Resource:

    http://xenforo.com/community/resources/flag-thread.2617/

    Description:

    This add-on will allow the admin to do the following:
    1. Flag thread
    2. Unflag thread
    3. View flagged threads list
    Flagging a thread is useful in situations where the admin will want to get back to a thread in the future. Only the admin will see the thread has been flagged.

    (Example of Thread Tools)

    pic001.jpg

    (Example of flagged threads list)

    pic002.jpg

    (Example of flagged thread while viewing a Forum or New Posts)

    pic003.jpg

    How to view flagged threads list:

    Create a bookmark in your browser that points to the following URL example:

    http://www.mydomain.com/forums/flagthread/

    Requirements:
    • This add-on requires XenForo v1.2x and above
    • This add-on requires default templates
    • Must be installed manually, do not use any third party installers
     
    Last edited: Dec 6, 2013
  2. AndyB

    AndyB Well-Known Member

    The directory structure with the core files highlighted.

    library
    --Andy
    ----FlagThread
    ------ControllerPublic
    --------FlagThread.php
    ----Install.php
    ----Listener.php
    ----Model
    ------Thread.php
    ----Route
    ------Prefix
    --------FlagThread.php
    ----Uninstall.php
     
  3. AndyB

    AndyB Well-Known Member

    Create Add-on

    pic001.jpg
     
  4. AndyB

    AndyB Well-Known Member

    library/Andy/FlagThread/ControllerPublic/FlagThread.php

    PHP:
    <?php

    class Andy_FlagThread_ControllerPublic_FlagThread extends XenForo_ControllerPublic_Abstract
    {    
        public function 
    actionIndex()
        {    
            
    // throw error if not SuperAdmin
            
    if (!XenForo_Visitor::getInstance()->isSuperAdmin())
            {
                throw 
    $this->getNoPermissionResponseException();
            }
            
            
    //########################################
            // flag thread
            //########################################
            
            // filter URL
            
    $threadId $this->_input->filterSingle('flag'XenForo_Input::STRING);            
            
            
    // flag thread if $threadId is numeric
            
    if (is_numeric($threadId))
            {
                
    // get database
                
    $db XenForo_Application::get('db');            
                
                
    // run query
                
    $db->query('
                    UPDATE xf_thread SET
                        flag = "1"
                    WHERE thread_id = ?
                '
    $threadId);
                
                
    // confirm action
                
    return $this->responseMessage('Thread successfully flagged');    
            }    
            
            
    //########################################
            // unflag thread
            //########################################

            // filter URL
            
    $threadId $this->_input->filterSingle('unflag'XenForo_Input::STRING);            
            
            
    // flag thread if $threadId is numeric
            
    if (is_numeric($threadId))
            {
                
    // get database
                
    $db XenForo_Application::get('db');            
                
                
    // run query
                
    $db->query('
                    UPDATE xf_thread SET
                        flag = "0"
                    WHERE thread_id = ?
                '
    $threadId);
                
                
    // confirm action
                
    return $this->responseMessage('Thread successfully unflagged');            
            }
            
            
    //########################################
            // show flag list
            //########################################

            //########################################
            // This is fetching $threads from XenForo_Model_Thread.
            // Initially the function getFlaggedThreads() is not there, 
            // but we extended XenForo_Model_Thread with Andy_FlagThread_Model_Thread.
            // Now Andy_FlagThread_Model_Thread functions are accessible from XenForo_Model_Thread.
            //########################################

            // get flagged thread_id numbers and title
            
    $threads $this->getModelFromCache('XenForo_Model_Thread')->getFlaggedThreads();
                    
            
    //########################################
            // display data        
            
            // prepare $viewParams for template
            
    $viewParams = array(
                
    'threads' => $threads,
            ); 
            
            
    // send to template for display
            
    return $this->responseView('Andy_FlagThread_ViewPublic_FlagThread''andy_flagthread'$viewParams);
        }
    }

    ?>
     
    Lil.Tee likes this.
  5. AndyB

    AndyB Well-Known Member

    library/Andy/FlagThread/Install.php

    PHP:
    <?php

    class Andy_FlagThread_Install
    {
        public static function 
    install()
        {
            
    $db XenForo_Application::get('db');        
            
    $db->query("
                ALTER TABLE xf_thread
                    ADD flag TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT 0
            "
    );
        }
    }

    ?>
     
  6. AndyB

    AndyB Well-Known Member

    library/Andy/FlagThread/Listener.php

    PHP:
    <?php

    class Andy_FlagThread_Listener
    {
        public static function 
    loadClassModel($class, array &$extend)
        {
            
    $extend[] = 'Andy_FlagThread_Model_Thread';
        }    
    }

    ?>
     
  7. AndyB

    AndyB Well-Known Member

    library/Andy/FlagThread/Model/Thread.php

    PHP:
    <?php

    class Andy_FlagThread_Model_Thread extends XFCP_Andy_FlagThread_Model_Thread
    {
        public function 
    getFlaggedThreads()
        {
            return 
    $this->_getDb()->fetchAll('
            SELECT thread_id, title, last_post_date
            FROM xf_thread
            WHERE flag = "1"
            ORDER BY thread_id ASC
            LIMIT 100'
    );
        }        
    }

    ?>
     
  8. AndyB

    AndyB Well-Known Member

    library/Andy/FlagThread/Route/Prefix/FlagThread.php

    PHP:
    <?php

    class Andy_FlagThread_Route_Prefix_FlagThread implements XenForo_Route_Interface
    {
        public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
        {
            return 
    $router->getRouteMatch('Andy_FlagThread_ControllerPublic_FlagThread'$routePath);
        }
    }

    ?>
     
  9. AndyB

    AndyB Well-Known Member

    library/Andy/FlagThread/Uninstall.php

    PHP:
    <?php

    class Andy_FlagThread_Uninstall
    {
        public static function 
    uninstall()
        {
            
    $db XenForo_Application::get('db');
            
    $db->query("
                ALTER TABLE xf_thread
                    DROP flag
            "
    );
        }
    }

    ?>
     
  10. AndyB

    AndyB Well-Known Member

    Code Event Listener

    pic001.jpg
     
  11. AndyB

    AndyB Well-Known Member

    Route Prefix

    pic001.jpg
     
  12. AndyB

    AndyB Well-Known Member

    andy_flagthread template

    Code:
    {xen:phrase flagged_threads_maximum_100_shown}
    <br /><br />
    
    <table class="dataTable">
        <tr class="dataRow">
            <th>{xen:phrase thread}</th>
            <th>{xen:phrase title}</th>
            <th>{xen:phrase last_reply_date}</th>
        </tr>
        
        <xen:foreach loop="$threads" value="$thread">
        <tr class="dataRow">
            <td>{$thread.thread_id}
            <td><a href="{xen:link 'threads', $thread}" />{$thread.title}</a></td>
            <td>{xen:datetime $thread.last_post_date}</td>
        </tr>
        </xen:foreach>
    </table>
    
     
  13. AndyB

    AndyB Well-Known Member

    Template Modifications - thread_list_item

    pic001.jpg
     
  14. AndyB

    AndyB Well-Known Member

    Template Modifications - thread_view

    pic001.jpg
     

Share This Page