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

How to fetch functions from other controllers?

Marcus

Well-known member
#1
I wrote a small addon to display Watched Threads on forum_list_nodes. It works, but I copied and pasted the code and most of the functions from Xenforo/ControllerPublic/Watched::actionIndex in my addon/ControllerPublic/Index::actionIndex. I would like to replace the code of my Index.php with a simple $response = parent::actionIndex from the Watched::actionIndex.

My x/Listener.php
PHP:
<?php
class x_Listener
{
	public static function extend ($class, array &$extend)
	{
		switch ($class)
		{
			case 'XenForo_ControllerPublic_Index':
				$extend[] = 'x_ControllerPublic_Index';
				break;
		}
	}
	
	public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
	{
		switch ($hookName)
		{
			case 'forum_list_nodes':	
				$params = array_merge($template->getParams(), $hookParams);
				$contents =  $template->create('x_watch_threads', $params) . $contents;
				break;
		}
	}
}
My x/ControllerPublic/Index.php
PHP:
<?php 

class x_ControllerPublic_Index extends XFCP_x_ControllerPublic_Index
{
	public function actionIndex()
	{
		$response = parent::actionIndex();
		
		$threadWatchModel = $this->_getThreadWatchModel();
		$visitor = XenForo_Visitor::getInstance();
		
		$newThreads = $threadWatchModel->getThreadsWatchedByUser($visitor['user_id'], true, array(
				'join' => XenForo_Model_Thread::FETCH_FORUM | XenForo_Model_Thread::FETCH_USER,
				'readUserId' => $visitor['user_id'],
				'postCountUserId' => $visitor['user_id'],
				'permissionCombinationId' => $visitor['permission_combination_id'],
				'limit' => XenForo_Application::get('options')->discussionsPerPage
		));
		$newThreads = $threadWatchModel->unserializePermissionsInList($newThreads, 'node_permission_cache');
		$newThreads = $threadWatchModel->getViewableThreadsFromList($newThreads);
		
		$newThreads = $this->_prepareWatchedThreads($newThreads);
		
		$response->params['newThreads'] = $newThreads;
		
		return $response;
	}
	
	protected function _prepareWatchedThreads(array $threads)
	{
		$visitor = XenForo_Visitor::getInstance();
	
		$threadModel = $this->_getThreadModel();
		foreach ($threads AS &$thread)
		{
			if (!$visitor->hasNodePermissionsCached($thread['node_id']))
			{
				$visitor->setNodePermissions($thread['node_id'], $thread['permissions']);
			}
	
			$thread = $threadModel->prepareThread($thread, $thread);
	
			// prevent these things from interfering
			$thread['canInlineMod'] = false;
			$thread['canEditThread'] = false;
			$thread['isIgnored'] = false;
		}
	
		return $threads;
	}
	
	protected function _getThreadModel()
	{
		return $this->getModelFromCache('XenForo_Model_Thread');
	}
	
	protected function _getThreadWatchModel()
	{
		return $this->getModelFromCache('XenForo_Model_ThreadWatch');
	}
}
 

Marcus

Well-known member
#2
1. I think I should extend Xenforo_ControllerPublic_Watched instead of Xenforo_ControllerPublic_Index. But I have to execute Xenforo_ControllerPublic_Watched::actionIndex on the forum index. How could I do that?

2. If I use the hook location ad_sidebar_visitor_panel, the template is rendered, but the variable $newThreads is not submitted to the template