Xenforo classes in a complex php application

janslu

Active member
I have a serious problem calling Xenforo classes and functions in typo3 CMS I'm using for the main site. I mention typo3 but I think the problem I'm facing is not specific to typo3, but rather to my lack of knowledge of OOP and autoloading classes. I have my own addons for typo3 using namespaces and classes and I need to use Xenforo bridge for two functions at the moments.

Here's my bridge function:

Code:
<?php

namespace JS\Babyboom\Xenforo;

use XenForo_Model;
use XenForo_DataWriter;
use XenForo_Autoloader;
use XenForo_Application;
use XenForo_Session;
use XenForo_Link;
use XenForo_Visitor;
use XenForo_Dependencies_Public;

class XenForoHelperClass {
    /*
    * Initialise the XenForom Controllers:
    */

    public static function init() {
        define('XF_ROOT', '/var/www/forum'); // set this always to xenforo root folder!
        define('TIMENOW', time());
        define('SESSION_BYPASS', false);
        require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
        XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
        XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
        XenForo_Application::set('page_start_time', TIMENOW);
        XenForo_Application::disablePhpErrorHandler();
        XenForo_Application::setDebugMode(true);       
        XenForo_Session::startPublicSession();

        XenForo_Link::useFriendlyUrls(true);
        XenForo_Link::romanizeTitles(true); 
        error_reporting(E_ALL & ~E_NOTICE);
        $dependencies = new XenForo_Dependencies_Public();
        $dependencies->preLoadData();       

    }

    public static function online(){
        $visitor = XenForo_Visitor::getInstance();
        $sessionModel = XenForo_Model::create('XenForo_Model_Session');
        $onlineUsers = $sessionModel->getSessionActivityQuickList(
            $visitor->toArray(),
            array('cutOff' => array('>', $sessionModel->getOnlineStatusTimeout())),
            ($visitor['user_id'] ? $visitor->toArray() : null)
        );
       
        return array(
            'guests' => $onlineUsers['guests'],
            'robots' => $onlineUsers['robots'],
            'members' => $onlineUsers['members'],
            'total' => $onlineUsers['total']
        );
    }

    /*
    * Get a filtered string URL for a node / forum/.
    */

    public function getNodeURL($nodeId) {
        error_log("getNodeURL called id:".$nodeId);
        $forumModel = XenForo_Model::create('XenForo_Model_Forum');
        $forum = $forumModel->getForumById($nodeId);
        $title = $forum['title'];
        $url = 'forum/'.XenForo_Link::buildPublicLink('forums',array('node_id' => $nodeId, 'title' => $title));
        $result = '<a href="/' . $url . '">' . $title . '</a>';  
        return($result);
    }
}

First function returns a number of users and guests online. I only display this information once. I call it from my function like this:

Code:
            $appXenForo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("Js\\Babyboom\\Xenforo\\XenForoHelperClass");
            $appXenForo::init();
            $usersOnline = $appXenForo::online();

            $gosci = $usersOnline['guests']+$usersOnline['robots'];
            $userow = $usersOnline['members'];
            $wsumie = $usersOnline['total'];

This one works fine.

But the other function, needs to be called multiple times (it iterates over an array of forums, and should return me titles and links to those forums one by one) from another class (and in another namespace). And this is where I am stuck. If I don't call $appXenForo::init(); it complains that there's no object to call function from. If I do call init - I get an exception that registry is already initialized.

For my tests, inside a function, that will be called multiple times, I have the following:
Code:
        $appXenForo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("Js\\Babyboom\\Xenforo\\XenForoHelperClass");
        $appXenForo::init();       
        $html2 = '<p>'.$appXenForo->getNodeURL("64").'</p>';

My questions are:
1. What is the proper way to interact with Xenforo application in such a scenario? Is there a way to create a persistent or global object, that will be available for my other functions?
2. Do I have to use those "use XenForo_Model;" in my helper class, or can I use namespace somehow?
 
OK, I got this thing working, by using typo3 singleton and manual override of error logging configuration. Unfortunately I have no idea if this is the best solution, but I'll be happy to share if someone needs more info.
 
2. Do I have to use those "use XenForo_Model;" in my helper class, or can I use namespace somehow?
I think that part is actually redundant. Remove the use statements and see if it changes anything. Don't think it will.
 
Back
Top Bottom