Return results in sidebar

MattW

Well-known member
I'm trying to extend this: http://xenforo.com/community/resources/attachment-statistics-admin-home.1654/ so that it displays the number of attachments in the forum statistics section on the forum home.

I can get it to render the text, but I can't seem to get it to display the number

attach_stats1.webp

In my listener, I'm extending

PHP:
if ($class == 'XenForo_ControllerPublic_Misc')
        {
            $extend[] = 'AttachStats_ControllerPublic_AttachStats';
        }
but I'm not sure if XenForo_ControllerPublic_Misc is the correct class to extend?

My ControllerPublic then looks the same as my ControllerAdmin file (which works):

PHP:
<?php
 
class AttachStats_ControllerPublic_AttachStats extends XFCP_AttachStats_ControllerPublic_AttachStats
{
    public function actionIndex()
    {
        $parent = parent::actionIndex();
       
        $AttachStatsModel = $this->_getAttachStatsModel();
 
        $attachFiles = $AttachStatsModel->getNumRecords();
        $parent->params['attach_files'] = $attachFiles;
   
        return $parent;
    }
 
    protected function _getAttachStatsModel()
    {
        return $this->getModelFromCache('AttachStats_Model_AttachStats');
    }

I'm still getting my head around all this, so I'm probably missing something really simple?
 
I think I've figured which class I needed to extend:

PHP:
        if ($class =='XenForo_ControllerPublic_Index')
        {
            $extend[] = 'AttachStats_ControllerPublic_AttachStats';
        }

I can now see the files being run and the queries in the debug, but it's still not passing the variable back into the template.
 
I assume you are using the forum_list_sidebar template hook?

Within your template_hook listener you can use this to reference the value of any param from the content template (as passed from the controller) for use in your custom content:

Code:
$template->getParam('boardTotals')
 
I assume you are using the forum_list_sidebar template hook?

Within your template_hook listener you can use this to reference the value of any param from the content template (as passed from the controller) for use in your custom content:

Code:
$template->getParam('boardTotals')
No, I've used some code from RagTek's Most Users Online to place it inside the original sidebar


PHP:
public static function templateHooks($name, &$contents, array $params, XenForo_Template_Abstract $template)
    {
        if ($name == 'page_container_sidebar')
        {
 
            $search = '<!-- slot: forum_stats_extra -->';
 
            $params = $template->getParams();
            $attach = $template->create('attach_stats_home', $params)->render();
            $replace = $search . $attach;
            $contents = preg_replace('#' . $search . '#', $replace, $contents, 1);
        }
    }

It's calling the controller and I can see the query is being executed


SELECT COUNT(*)
FROM xf_attachment_data

Run Time: 0.000277
Select TypeTableTypePossible KeysKeyKey LenRefRowsExtra
SIMPLE xf_attachment_data index attach_count 4 2 Using index


Here is the template I'm creating:
Code:
<template title="attach_stats_home" version_id="1" version_string="1.0"><![CDATA[<dl>
    <dt>{xen:phrase attachment_num_files}:</dt>
    <dd>{xen:number $attach_files}</dd></dl>
]]></template>
 
You must define the param in the template call:

Rich (BB code):
public static function templateHooks($name, &$contents, array $params, XenForo_Template_Abstract $template)
    {
        if ($name == 'page_container_sidebar')
        {
 
            $search = '<!-- slot: forum_stats_extra -->';
 
            $params = $template->getParams();
            $attach = $template->create('attach_stats_home', array('attach_files' => $template->getParam('boardTotals')))->render();
            $replace = $search . $attach;
            $contents = preg_replace('#' . $search . '#', $replace, $contents, 1);
        }
    }

If the number is defined as a viewParam in the controller then you need to specify the name of that param in place of 'boardTotals'. Or you could run the query directly in your template_hook listener.
 
Thanks Jake, but it's still not working.

I've created another template hook, exactly the same as the one I'm using for the ACP home, and it's still not working

all.webp

The controller is exactly the same as the one I'm using in the ACP, which works fine.
phped.webp
 
OK, so I'm still stuck with this :(

I've tidied up the Listener, and I'm using the same way to generate the template in both the ACP and on the forum home

PHP:
<?php
 
class AttachStatsHome_Listener
{
        public static function extendControllers($class, &$extend)
        {
                switch ($class)
                {
                        case 'XenForo_ControllerAdmin_Home':
 
                                $extend[] = 'AttachStatsHome_ControllerAdmin_AttachStatsHome';
                                break;
 
                        case 'XenForo_ControllerPublic_Index':
 
                                $extend[] = 'AttachStatsHome_ControllerPublic_AttachStatsHome';
                                break;
                }
        }
 
 
        public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
        {
 
                switch ($hookName)
                {
 
                        case 'page_container_breadcrumb_top':
 
                                $params = $template->getParams();
                                $params += $hookParams;
 
                                $contents = $template->create('attach_stats_block', $params) . $contents;
                                break;
 
                        case 'admin_sidebar_home':
 
                                $visitor = XenForo_Visitor::getInstance();
 
                                $params = $template->getParams();
                                $params += $hookParams;
 
                                $contents = $template->create('attach_stats', $params) . $contents;
                                break;
                }
        }
}

The Controller is also the same, just in ControllerAdmin and ControllerPublic.

ControllerAdmin is working perfectly. The params are being passed into the template, and putting the values in

stats1.webp

Now, no matter which hook I use for the forum home, it's rendering the template, but not passing the params into it.

stats2.webp


The controller looks like this:
PHP:
<?php
 
class AttachStatsHome_ControllerPublic_AttachStatsHome extends XFCP_AttachStatsHome_ControllerPublic_AttachStatsHome
{
        public function actionIndex()
        {
                $parent = parent::actionIndex();
 
                $AttachStatsHomeModel = $this->_getAttachStatsHomeModel();
 
                $attachSize = $AttachStatsHomeModel->getAttachUsage();
                $parent->params['attach_size'] = $attachSize / (1024 * 1024); // size in MB
 
                $attachFiles = $AttachStatsHomeModel->getNumAttachments();
                $parent->params['attach_files'] = $attachFiles;
 
                return $parent;
        }
 
        protected function _getAttachStatsHomeModel()
        {
                return $this->getModelFromCache('AttachStatsHome_Model_AttachStatsHome');
        }
}

Again, this works fine in the ACP

If I change the Controller to render the tempate as such
PHP:
<?php
 
class AttachStatsHome_ControllerPublic_AttachStatsHome extends XFCP_AttachStatsHome_ControllerPublic_AttachStatsHome
{
        public function actionIndex()
        {
 
                $AttachStatsHomeModel = $this->_getAttachStatsHomeModel();
 
                $attachSize = $AttachStatsHomeModel->getAttachUsage();
                $viewParams['attach_size'] = $attachSize / (1024 * 1024); // size in MB
 
                $attachFiles = $AttachStatsHomeModel->getNumAttachments();
                $viewParams['attach_files'] = $attachFiles;
 
                return $this->responseView('XenForo_ViewPublic_Forum', 'attach_stats_block', $viewParams);
                
        }
 
        protected function _getAttachStatsHomeModel()
        {
                return $this->getModelFromCache('AttachStatsHome_Model_AttachStatsHome');
        }
}

It's renders it over the forum home page, with the variables
stats3.webp

Is there a difference with how the params are passed into the parent between the ACP Home and Forum Index?
 
Grrrrr.....this is not working now on 1.2 Beta 1.

Getting a NULL value only for the forum home....still working in the ACP. Is there anything different that needs to be done in 1.2?

1.2.webp
 
After a quick bit of digging post tea, XenForo_ControllerPublic_Index is totally different in 1.2 :confused:
 
you could also include xen:callback via TMS into the template, without the need to use a proxyclass and extend the controller:)

then it would be shown on ALL PAGES where the forum statistics are shown in the sidebar
 
Last edited:
Top Bottom