PDA

View Full Version : Cannot access lastposterid from within plugin


nambisolo
03-27-2012, 07:36 AM
Hello

I'm attempting to create a plugin that retrieves a user profile field to display on the forum home page. The code is as follows.

$lastpostuserid = $lastpostinfo['lastposterid'];

/*write custom query here to get display name*/

$lastposter = $db->query_first("SELECT field6 FROM " . TABLE_PREFIX . "userfield WHERE userid = '$lastpostuserid'");

$lastposterhome_var = $lastposter['field6'];*/

/* register variable in the 'memberaction_dropdown' template */

vB_Template::preRegister('memberaction_dropdown',a rray('lastposterhome_var'=> $lastposterhome_var));

The hook I'm using is forumbit_display. The problem is that the value of last $lastpostinfo['lastposterid'] is not available and subsequently I'm not getting a result. The value is available though in the template containing memberaction_dropdown (forumhome_lastpostby). The plugin does work if I assign arbitrary text to my variable just to test.

I do have a very similar plugin working on the forum display page which is using the threadbit_display hook and $thread['lastposterid']

I'm quite new to this so any advice would be most welcome.

Thanks.

Mike

kh99
03-27-2012, 10:53 AM
It looks like you should be able to use $lastpostinfo['lastposterid'] . Are you sure that's the problem?

nambisolo
03-27-2012, 12:19 PM
Hi

I've just run a check by trying to return the $lastpostinfo['lastposterid'] value directly to the memberaction_dropdown e.g.

$lastposterhome_var = $lastpostuserid;

I have discovered a value is being returned, as you said it should, but there's an anomalie. The lastposterid is in fact the id of the poster of the last thread in the forum above (this is the forum home page where each forum is listed).

The database query was failing because the first forum on the page had no post.

Any ideas why the ids are out of sync like this?

Thanks.

Mike

kh99
03-27-2012, 12:57 PM
Yeah, the problem might be that $lastpostinfo is an empty array if there is no last thread (a forum is private or doesn't allow posts). So maybe if you do something like:

if (!empty($lastpostinfo))
{
// your code here
}

nambisolo
03-28-2012, 07:48 AM
Yes, the first forum in the list had no threads. I added your suggested test which did not catch an empty array. I added a thread to the forum which had no affect either. I have also removed the first forum from the list (display order set to zero) but the issue is then transferred to the forum that moves to the top of the list. It appears the first forum in the list has no access to the $lastpostinfo array. Any ideas?

kh99
03-28-2012, 11:34 AM
The problem is that the memberaction_dropdown is created before the forumbit_display hook is called (sorry, I should have realized that yesterday). I think you can use the memberaction_dropdown hook, I'm trying to work that out now.

Edit: OK, this seems to work (using hook memberaction_dropdown):

$lastpostuserid = $memberinfo['userid'];

/*write custom query here to get display name*/

global $db;
$lastposter = $db->query_first("SELECT field6 FROM " . TABLE_PREFIX . "userfield WHERE userid = '$lastpostuserid'");

$lastposterhome_var = $lastposter['field6'];

/* register variable in the 'memberaction_dropdown' template */
vB_Template::preRegister('memberaction_dropdown',a rray('lastposterhome_var'=> $lastposterhome_var));

nambisolo
03-28-2012, 02:22 PM
Thanks but I don't see the hook memberaction_dropdown in the dropdown list of hook locations. Isn't this the name of the template?

kh99
03-28-2012, 02:30 PM
It's also a hook location. I had trouble finding it at first too, because it comes before all the "member_" hooks so it kind of looks like it's out of order. It's just after the "mail_" hooks.

nambisolo
03-28-2012, 03:51 PM
I've double checked and it's definately not in the list.

kh99
03-28-2012, 03:53 PM
What version are you running? Maybe it was added recently (although I thought I checked a previous version to make sure it was there).

Edit: I checked again and it looks like it's in vb4.1.3 (which is the earliest vb4 version I have the source for).

nambisolo
03-28-2012, 04:33 PM
It looks like it's 4.0.1.

kh99
03-28-2012, 05:05 PM
Yeah, that hook didn't exist in that version. I guess now we know why it was added.

You could add it to your version. I wouldn't normally suggest adding a hook, but in this case you'll probably upgrade someday and then you won't have to change your plugin. (Also, I don't see any other way to do what you want to do using hooks)

Anyway, you can edit the file includes/xml/hooks_vbulletin.xml and in the section <hooktype type="general"> add

<hook>memberaction_dropdown</hook>


Then in the file includes/functions.php around line 3180 add the line in red:


fetch_online_status($memberinfo);
}

($hook = vBulletinHook::fetch_hook('memberaction_dropdown') ) ? eval($hook) : false;

$templater = vB_Template::create('memberaction_dropdown');
$templater->register('template_hook', $template_hook);
$templater->register('memberinfo', $memberinfo);




Then you should see it appear in the list and you can proceed from there. (I tried adding to the xml file and saw the hook appear in the list, but I haven't tested it completely to see if it works).

nambisolo
03-29-2012, 07:52 AM
Fantastic! Does exactly what was required. Thanks so much for your help. I will look at upgrading to the latest version.

Cheers.

Mike.