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

[eluser]Choo[/eluser]
I want to add my experience using this variant... It doesn't work with data manager. Anybody else try to work with it from CI? I think it happens because vb classes work with $GLOBAL directly, so we can't unset it. So, for what that library when we can simple write something like $vb=&$GLOBAL['vbulletin'] everywhere. And it works with data manager. Any opinions?
#12

[eluser]DemiGod[/eluser]
You're right, there are some issues with the Data Manager it seems. I've been working on getting codeIgniter and vBulletin integration for the last few days for a project. It would be nice to have the Data Manager working correctly so I don't have to manipulate the database directly.

I put all my code into the Forums library (still needed to do the loading trick for globals.php in the index.php file). My test code below contains different ways of accessing data that I found from the codeigniter and vbulletin forums (and software packages). The idea is to have different options to get around any issues I might come across. All of these functions work, except the one noted below.

Anyway, I created a function (setCurrentUserHomePage) that would update the current logged in users homepage to test out the Data Managers. The function logic outside the codeigniter environment works perfectly without any errors (I set error reporting to E_ALL). In the codeigniter environment though, vBulletin generates a bunch of error notices about undefined indexes (ipoints, posts, membergroupids, posts, displaygroupid). The homepage is still updated though. Code follows below:


Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Forums
{
    private $CI;
    
    private $vbulletin;
    
    private $db_prefix     = 'vb_';
    private $cookie_prefix = 'bb';
    
    function __construct()
    {
        $this->CI          =& get_instance();
        $this->CI->forums  =  $this;
        $this->vbulletin   =  $GLOBALS['vbulletin'];
    }
    
    function hasSession()
    {
        return $this->vbulletin->userinfo['userid'] > 0;
    }
    
    function getSessionHash()
    {
        // using vbulletin globals
        global $sessionhash;
        return $sessionhash;
    }
    
    function getCurrentUserId()
    {
        // We can use vbuser way of doing things
        // although in this case it would be better to use
        // $this->vbulletin->userinfo['userid']
        return @$_COOKIE[$this->cookie_prefix . 'userid'];
    }
    
    function getUserInfo($user_id)
    {
        return fetch_userinfo($user_id);
    }
    
    function setCurrentUserHomePage($homepage)
    {
        // Get User data manager object
        $dmUser =& datamanager_init('User', $this->vbulletin);
        
        // Load user info
        $dmUser->set_existing($this->vbulletin->userinfo);
        
        // Set new homepage
        $dmUser->set('homepage', $homepage);
        
        // Try to save chagnes
        $dmUser->pre_save();
        if (count($dmUser->errors) > 0)
        {
            return false;
        }
        else
        {
            $dmUser->save();
            return true;
        }  
    }
}
?>



This is in my controller functions to test out the class:
Code:
$this->load->library('forums');
        //echo $this->forums->getCurrentUserId();
        
        if ($this->forums->hasSession())
        {
            $this->forums->setCurrentUserHomePage('www.google.com');
            //print_r($this->forums->getUserInfo(1));
        }
        
        //echo $this->forums->getSessionHash();
#13

[eluser]DemiGod[/eluser]
Ha... seems like vBulletin changes the error reporting level while callings its functions. The reason codeigniter sees these errors is because it has its own custom error handler in CodeIgniter.php:

set_error_handler('_exception_handler');

So this is why the codeigniter picks up these error notices. Using similar error code in my test php file produced similar results. So it seems there are no integration issues with codeigniter... but you will need to set you error level to E_ALL & ~E_NOTICE. If you want to keep the error level to E_ALL, you could do the following:

Code:
function xyz()
{
    // Use php error handler at start of function
    restore_error_handler();

    // call vbulletin data manager functions

    // Restore codeIgniter's error handler before returning
    set_error_handler('_exception_handler');
}
#14

[eluser]CroNiX[/eluser]
[quote author="DemiGod" date="1241069706"]In the codeigniter environment though, vBulletin generates a bunch of error notices about undefined indexes (ipoints, posts, membergroupids, posts, displaygroupid). The homepage is still updated though. Code follows below:[/quote]

Yes I ran into this as well, but the solution was the same that you did here:
Code:
function getSessionHash()
{
    // using vbulletin globals
    global $sessionhash;
    return $sessionhash;
}

Those variables you were having troubles with are in the global namespace within the vb application and NOT a part of the object, so you need to declare them global to get access to them from within CI. I was thinking about compiling a list of these variables and then adding them to my CI vb library, but as of now I have just been adding them as I encounter the errors. Been working so far...
#15

[eluser]CroNiX[/eluser]
Just a note, I couldn't get the vb datamanagers working from within CI, or at least the one I was trying which was the user datamanager. I kept getting "Call to a member function query_first_slave() on a non-object" errors and finally tracked it down to the fact that we were unsetting $GLOBALS['vbulletin']. The datamanagers internally call upon the global $vbulletin, so we can't unset it if we want to use datamangers, unless Im missing something. After commenting out this line I was able to use the user datamanager and change a persons usergroup with no errors. This hasn't yet shown to prove harmful to anything else that I've done, although it probably consumes more memory because you aren't destroying the global vbulletin object.

Has anybody done anything interesting with CI/VB integration? Any common issues? It would be nice if we could collaborate so everything is in one thread.

Thanks.
#16

[eluser]Uplift[/eluser]
[quote author="CroNiX" date="1271646495"]Just a note, I couldn't get the vb datamanagers working from within CI, or at least the one I was trying which was the user datamanager. I kept getting "Call to a member function query_first_slave() on a non-object" errors and finally tracked it down to the fact that we were unsetting $GLOBALS['vbulletin']. The datamanagers internally call upon the global $vbulletin, so we can't unset it if we want to use datamangers, unless Im missing something. After commenting out this line I was able to use the user datamanager and change a persons usergroup with no errors. This hasn't yet shown to prove harmful to anything else that I've done, although it probably consumes more memory because you aren't destroying the global vbulletin object.

Has anybody done anything interesting with CI/VB integration? Any common issues? It would be nice if we could collaborate so everything is in one thread.

Thanks.[/quote]

I'm trying to use the above code

$query = $this->vbulletin->db->query_read("SELECT * FROM post WHERE postid = 1");
$post = $this->vbulletin->db->fetch_array($query);

print_r($post);

but get the error you mentioned above

Fatal error: Call to a member function query_read() on a non-object ...

i'm pretty sure this worked when i first tested it before i built my site Confused

i can use stuff like getUsername(), hasSession() without issues.

any ideas?

[edit] i also get

A PHP Error was encountered
Severity: Notice

Message: Undefined property: vBulletin::$db
Filename: controllers/theme.php
Line Number: 22
#17

[eluser]Uplift[/eluser]
bump Sad anyone?
#18

[eluser]Uplift[/eluser]
bump :p
#19

[eluser]nikes[/eluser]
And it does work great. Thank you again for this!




Theme © iAndrew 2016 - Forum software by © MyBB