Flag Thread v1.0

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

(Example of flagged threads list)

pic002.webp

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

pic003.webp

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

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

?>
 
library/Andy/FlagThread/Listener.php

PHP:
<?php

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

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

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

PHP:
<?php

class Andy_FlagThread_Route_Prefix_FlagThread implements XenForo_Route_Interface
{
	public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
	{
		return $router->getRouteMatch('Andy_FlagThread_ControllerPublic_FlagThread', $routePath);
	}
}

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

?>
 
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>
 
Top Bottom