PDA

View Full Version : Plugin variable not showing in postbit data


Rogue_SSEi
03-18-2010, 07:57 PM
Background:
I've created a userfield which contains the number of times a forum member has won an award (in this case an oven mitt). I'm trying to display a graphic in postbits for each time the winner has won. So if a member has won 3 oven mitts then the field
contains 3 and it should display the graphic 3 times. I've written some plugin code that creates a variable with an oven mitt image. I did this because I was unable to use a for loop in the postbit template.

Here is the plugin code that creates the oven mitt image data:


global $vbulletin;

if ($vbulletin->userinfo['userid'] == 1)
{
$ovenmittcount = $vbulletin->userinfo['field12'];

$ovenmitts = "";

for ($loopcount=1; $loopcount<=$ovenmittcount; $loopcount+=1)
{
$ovenmitts = $ovenmitts . '<img src="http://www.badbeatspoker.net/images/isopmitt.jpg">';
}

}

This code works and has the desired effect of creating 3 oven mitts (the userfield is set to 3). I've set the plugin to only work for me so I didn't interfere with members on the board until I have it working. I've tested the plugin by placing the $ovenmitts variable in the forumdisplay template and it posted the image fine.

In forumdisplay this code shows the image correctly:


<center>
$random_banner[$random_number]
$ovenmitts
</center>


The issue is when I put the $ovenmitts variable in the postbit template to place the image amongst the other postbit data. It never displays like it can't find the variable.

I've tried setting the plugin to work in global_start and a variety of other showthread and postbit hooks with no joy.


This is the code I'm using in postbit:


<if condition="$post['field12']">
$ovenmitts
</if>


Any clues as to what I'm doing wrong?

kh99
03-18-2010, 09:58 PM
I think the problem is that unless the variable is declared global (where it's set and where the template is eval'ed), it will be undefined and so will appear to be 0. That's a problem when doing what you're doing - you pretty much need to go in to the code to see what's going on, and find out where the template is eval'ed and where your hook is executed. In the case of postbit it looks like the template is used in includes/class_postbit.php in the function construct_postbit around line 304. I see that just above that is the "postbit_display_complete" hook, so probably the easiest thing would be to try using that hook.

Rogue_SSEi
03-19-2010, 12:29 PM
I tried that hook per you suggestion and it still doesn't display. Is there a way to declare my ovemitts variable global? I've tried putting global in front of it as the first line of the plugin like so:


global $ovenmitts;


That didn't seem to work either.

Do I need to declare this as global in the class_postbit.php file?

kh99
03-19-2010, 01:24 PM
Hmm...sorry, I guess I was wrong about something, just don't know what it is. There's no reason to edit class_postbit.php if putting it in the pluging didn't work.

So let's see - maybe that's not really where the template is used. You could try just putting an "$ovenmitts = something or other" in a plugin in using postbit_display_complete just to see if that's the right place. Are you sure "$post['field12']" is the right condition? If not, try taking the condition out temporarily.

Rogue_SSEi
03-19-2010, 01:32 PM
the condition is right because I can put some random text in the the if statement and that prints as long as there is data in the field. The plugin code is correct because I can use the variable in forumdisplay and it works fine.

I thought it was a scope issue but I would have thought putting it in global_start would solve that. As a side note I noticed the random_banner code which posts an ad banner in forumdisplay correctly also does not display in the postbit template.

I've removed the conditional in the postbit template just to make sure and still nothing. It is like the variable is blank.

kh99
03-19-2010, 01:36 PM
Yeah, I know you'd have to have a "global" where the template is eval'ed (if it's in a function call), but it could be that the place I mentioned is not the right place for that template.

Sorry. Maybe someone else knows?

--------------- Added 1269010062 at 1269010062 ---------------

...oh, I did think of one thing - your plugin uses $vbulletin->userinfo['field12'] but I think you'd want $post['field12'], right?

Rogue_SSEi
03-19-2010, 02:48 PM
The plugin generates the proper image into the $ovenmitts variable. I confirmed this by putting the variable into the forumdisplay template and it shows properly. I also did a couple diagnostic print statements in the plugin to make sure the $ovenmittcount varialbe was getting the correct number from the field12 variable.

Either the $ovenmitts varialbe is getting wiped, isn't scoped correctly or I'm not calling it correctly to display in the postbit template. I'm just not sure where to go from here.

I appreciate your help though :)

Rogue_SSEi
03-23-2010, 08:12 PM
Anyone else have some ideas that I can try?

kh99
03-24-2010, 11:08 PM
OK, well, I have another one - use $bbuserinfo['ovenmitts'] instead of $ovenmitts to get rid of the scoping issue. I think $bbuserinfo is the same as $vbulletin->userinfo, and I don't know where $bbuserinfo gets set, so it's possible you might have to set $vbulletin->userinfo['ovenmitts'] in the plugin and use $bbuserinfo['ovenmitts'] in the template. (If someone wants to explain more about the use of those, I'd like to know).

Anyway, it may not be the "correct" thing to do but I think it will work (assuming scoping is the issue).

Rogue_SSEi
04-01-2010, 12:38 PM
I'll give it a shot. Thanks :)

tommythejoat
12-20-2010, 07:05 PM
I have a similar problem and need to make changes to the display if a userfield value is set.

I am pretty sure $bbuserinfo refers to the logged in user, while $postinfo refers to the person who made the post. So you would want to use a value in $postinfo in your case. I did not think that the names of the fields were available at this level, so I am using $bbuserinfo[field11] to refer to my field in the template. That seems to work just fine and I can control the display with that value with <if condition="$bbuserinfo[field11]> stuff </if> at the appropriate place in the template.

My problem is that I cannot figure out how to set the value in the database at my hook location. I could generate the sql, but I thought I should use the user datamanager to do that and I cannot find either examples or documentation on how to set a userfield programmatically. Setting it directly with a query seems like a coding standard violation.

btw, I would have thought you could just use ratings for the ovenmitts and change the rating symbol. Here is what we did for a similar donor display with the donor having the option to not display the symbol.

<! -- Donor mod add conditional to rank -->
<if condition="$post[field11]">
<if condition="$post['rank']"><div class="smallfont">$post[rank]</div></if></if>
<! -- end donor mod -->