Welcome Guest, Not a member yet? Register   Sign In
How to Integrate Vbulletin object into codeigniter?
#1

[eluser]CroNiX[/eluser]
Ive written some custom software for a company for their needs of using vbulletin outside of vbulletin. This was pretty easy to do.

Basically you just:
Code:
require_once(VBULLETIN_PATH . 'global.php');
and then you have access to the vbulletin object and can use it like: $vbulletin->userinfo();

I would like to port that app to CI as they now want to add a bunch of additional functionality and CI is the bomb. How do I use this method in CI to load vbulletin? I want it to be a global object like the rest of CI so I can have it autoloaded and access it from any controller using $this->vbulletin->xxx. This is a bit beyond my CI knowledge as Ive only used CI to work with CI Smile

Thanks!
#2

[eluser]Shadab[/eluser]
I was trying to achieve the same thing.
But found out that vBulletin can't be initialized from inside a function.

[ Reference: http://www.vbulletin.org/forum/showthread.php?t=205765 ]

Now trying another approach:
ยป Initialize vBulletin and pass the $vbulletin object to a (CI) library somehow.

Will report tomorrow.
#3

[eluser]CroNiX[/eluser]
Thank you for the reply. I also found that reference on vb.org and have since given up the approach of trying to use CI for this project. No matter what you do, CI is an object and no matter where or how you try to bring vB into CI, CI is an object, so it won't work....unless Im missing something and that is totally possible. Ive only used CI for a few months on 2 projects and don't profess to be an expert.

This really sucks as I really wanted to use CI for this project to save a few days of coding, but the way vB is written, it won't let you.

It has to be done in a non-oop approach.

If you discover something I missed, please do tell. Even after 2 days of building a new "framework" to code for vB, I would throw it all away to be able to use CI.
#4

[eluser]Shadab[/eluser]
>> Initialize vBulletin and pass the $vbulletin object to a (CI) library somehow.

I tried this approach and it worked. Though it required modifying the main (index.php) file.
Solution:

index.php
Put this right after the opening PHP tag:
Code:
$dir = getcwd();

chdir('path/to/your/vb/installation/');
require './global.php';

chdir($dir);


config.php
Enable the hooks:
Code:
$config['enable_hooks'] = TRUE;


hooks.php
Create a 'post_controller_constructor' hook (a pre_controller hook won't work as the $CI object hasn't been created at that point)
Code:
$hook['post_controller_constructor'] = array(
                                        'class' => 'vBHook',
                                        'function' => 'init',
                                        'filename' => 'vbhook.php',
                                        'filepath' => 'hooks'
                                        );


vbhook.php in /hooks/ folder
Code:
<?php

class vBHook {
    
    function init() {
        
        $CI =& get_instance();
        $CI->vbulletin = $GLOBALS['vbulletin'];
        unset($GLOBALS['vbulletin']);
    }
}

DONE! Wink

I tested this hook (or hack?) by this controller; and it working fine:
Code:
<?php

class Test extends Controller {

    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        $query = $this->vbulletin->db->query_read("SELECT * FROM post WHERE postid = 1");
        $post = $this->vbulletin->db->fetch_array($query);  
        print_r($post);
    }    
}



Now if somebody could find another (non-invasive?) way of achieving it
(ie, without needing to edit the index.php file); it would be great!

Good night. Smile
#5

[eluser]CroNiX[/eluser]
Wow, I will go test this out. I don't care if it has to have a little mod to index.php for this project, its an easy thing to update if needed and I doubt they really make any changes to index.php when updating CI, or rarely. I hope it works as you say! Thank you for your work!
#6

[eluser]CroNiX[/eluser]
And it does work great. Thank you again for this! [big ass smile]
#7

[eluser]CroNiX[/eluser]
I notice that you can't access the vb object from the constructor of a controller. You can from any method, but not the constructor. Do you have any ideas? I would like to check the usergroup of the currently logged in user from the constructor so I don't have to do it for each method. Other than that its working very well so far!
#8

[eluser]Shadab[/eluser]
Yes, the $this->vbulletin object is not available in controller constructors as the $vbulletin is copied into $CI 'after' the controller constructor has finished executing. Another approach would be to copy $vbulletin into $CI via a library class instead of the hook.


index.php
Same as in previous post


vb.php in /application/libraries/
Code:
<?php

class vB {

    function __construct() {
        
        $CI =& get_instance();
        $CI->vbulletin = $GLOBALS['vbulletin'];

        unset($GLOBALS['vbulletin']);
    }
}

[No hooks required]


And then use like this in a controller :

Code:
class Test extends Controller {

    function __construct()
    {
        parent::Controller();
        
        $this->load->library('vB');
        
        $query = $this->vbulletin->db->query_read("SELECT * FROM post WHERE postid = 1");
        $post = $this->vbulletin->db->fetch_array($query);
        
        print_r($post);
    }

    function index()
    {
        // Some other $this->vbulletin work.
    }

}
#9

[eluser]CroNiX[/eluser]
This works great. Thanks again! This is almost identical to what I came up with earlier. The only different is I didn't think to load the vb object from index.php.
#10

[eluser]Shadab[/eluser]
Not a problem. Glad to hear that it works. Smile




Theme © iAndrew 2016 - Forum software by © MyBB