PDA

View Full Version : Template Variable Hook


KGodel
04-08-2015, 01:26 PM
Hey all. I read this article: https://vborg.vbsupport.ru/showthread.php?t=228078 (You know, searching before posting) and I was still a bit confused. I want to do the following:

For each post, I want to set a variable to use in the postbit based on the user's usergroup.

I imagine I can use the last part of the article to pass the variable to the postbit_legacy template(?) but I don't know where I should place my hook or how to get the poster's info from that hook. Help appreciated! :)

kh99
04-08-2015, 03:14 PM
If you want to add something to the postbit template, postbit_display_complete is probably a good hook to use. The template name you can use to preRegister is $this->template_prefix . $this->templatename.

I think the post author's usergroup info might already be in the $post array, so you might not need a plugin if you can do what you want to do using is_member_of() in a template conditional.

KGodel
04-08-2015, 06:36 PM
I'd prefer to use a hook because I will be doing multiple checks and there are multiple outcomes. I don't want to lengthen the template, so I'd rather just do all that in php and pass off the results.

KGodel
04-15-2015, 01:59 PM
So after thinking awhile, I think this would be better to store in the $userdata array, but I don't want it to be premanent (drawn from the database) but rather dynamically generated when the userdata is retrieved. I think this will be easier than to do a check on every postbit, considering you may have the same poster multiple times. Is there a way to do that?

kh99
04-15-2015, 02:33 PM
I think you can use the hook fetch_userinfo and look at the user info in $user, and add whatever you want to it. Then it should appear in $post in the postbit. (And it's cached for you).

KGodel
04-15-2015, 07:48 PM
So I could essentially just add $user['blabla'] = xxx; to set that within the userinfo? After that wouldn't I still need a template hook to pass those variables, or does userdata work by allowing anything in that data set to be referenced?

kh99
04-15-2015, 08:34 PM
So I could essentially just add $user['blabla'] = xxx; to set that within the userinfo? After that wouldn't I still need a template hook to pass those variables, or does userdata work by allowing anything in that data set to be referenced?

I'm not sure if I follow that, but when the postbit tremplate is being rendered, the userinfo is combined with the post info and registered to the template as 'post', so you should be able to use {vb:raw post.blabla} in the postbit (or postbit_legacy) template without doing anything else.

KGodel
04-23-2015, 09:00 PM
So it doesn't seem to be working. Here is what I have at the hook "fetch_userinfo".

$lrank = $srank = "";

switch ($userinfo['usergroupid']) {
// Leadership Ranks
case 6: // Clan Leader
if ($userinfo['userid'] == 1) {
$lrank = "<img src='images/ranks/v10/cf.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Clan Founder' \>";
$srank = "<img src='images/ranks/v9/cf.png' alt='Clan Founder' \>";
} else {
$lrank = "<img src='images/ranks/v10/cl.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Clan Leader' \>";
$srank = "<img src='images/ranks/v9/cl.png' alt='Clan Leader' \>";
}
break;
case 5: // Clan Officer
$lrank = "<img src='images/ranks/v10/co.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Clan Officer' \>";
$srank = "<img src='images/ranks/v9/co.png' alt='Clan Officer' \>";
break;
case 24: // Clan Coordinator
$lrank = "<img src='images/ranks/v10/cc.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Clan Coordinator' \>";
$srank = "<img src='images/ranks/v9/cc.png' alt='Clan Coordinator' \>";
break;
case 17: // Division Leader
$lrank = "<img src='images/ranks/v10/dl.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Division Leader' \>";
$srank = "<img src='images/ranks/v9/dl.png' alt='Division Leader' \>";
break;
case 18: // Division Officer
$lrank = "<img src='images/ranks/v10/do.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Division Officer' \>";
$srank = "<img src='images/ranks/v9/do.png' alt='Division Officer' \>";
break;
case 62: // Team Manager
$lrank = "<img src='images/ranks/v10/tm.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Team Manager' \>";
$srank = "<img src='images/ranks/v9/tm.png' alt='Team Manager' \>";
break;
case 38: // Leadership Trainee
$lrank = "<img src='images/ranks/v10/lt.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Leadership Trainee' \>";
$srank = "<img src='images/ranks/v9/lt.png' alt='Leadership Trainee' \>";
break;
// Member Ranks
case 53: // Visionary
// Custom Rank Files
$ltagfile = "images/ranks/v10/vis/" . strtolower(str_replace(' ', '', $userinfo['username'])) . ".png";
$stagfile = "images/ranks/v9/vis/" . strtolower(str_replace(' ', '', $userinfo['username'])) . ".png";

// Default VM
$lrank = "<img src='images/ranks/v10/vm.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Visionary Member' \>";
$srank = "<img src='images/ranks/v9/vm.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Visionary Member' \>";

// Check for a Custom Ranks
if (file_exists($ltagfile)) {
$lrank = "<img src='" . $ltagfile . "' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Visionary Member' \>";
}
if (file_exists($stagfile)) {
$srank = "<img src='" . $stagfile . "' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Visionary Member' \>";
}
break;
case 52: // Renowned
$lrank = "<img src='images/ranks/v10/rm.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Renowned Member' \>";
$srank = "<img src='images/ranks/v9/rm.png' alt='Renowned Member' \>";
case 51: // Senior
$lrank = "<img src='images/ranks/v10/srm.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Senior Member' \>";
$srank = "<img src='images/ranks/v9/srm.png' alt='Senior Member' \>";
case 35: // Steadfast
$lrank = "<img src='images/ranks/v10/sm.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Steadfast Member' \>";
$srank = "<img src='images/ranks/v9/sm.png' alt='Steadfast Member' \>";
case 2: // Apprentice
$lrank = "<img src='images/ranks/v10/ap.png' style='margin-left:auto; margin-right:auto; margin-top:-4px; display:block; overflow:visible; position:relative; z-index:10;' alt='Apprentice Member' \>";
$srank = "<img src='images/ranks/v9/ap.png' alt='Apprentice Member' \>";
// Other Statuses
case 21: // Missing in Action
$lrank = "Missing in Action";
$srank = "Missing in Action";
case 20: // Extended Leave
$lrank = "Extended Leave";
$srank = "Extended Leave";
case 23: // Resigned
$lrank = "Resigned";
$srank = "Resigned";
case 65: // Inactive
$lrank = "Inactive";
$srank = "Inactive";
case 45: // Guest Account
$lrank = "Official Guest";
$srank = "Official Guest";
case 8: // Dismissed
$lrank = "Dismissed";
$srank = "Dismissed";
}

// Set the User Info
$userinfo['lrank'] = $lrank;
$userinfo['srank'] = $srank;

kh99
04-23-2015, 09:26 PM
Well, using hook fetch_userinfo you'd want to use $user instead of $userinfo. You could try that, since it should be pretty quick to make that change, but looking at showthread.php I think I may have been wrong. Sorry about that, but it looks like showthread.php gets the userinfo as part of the query for posts in a thread, so I guess it doesn't call fetch_userinfo(). So it doesn't look like you can do much better than doing it in postbit_complete and using $post in place of $userinfo (although you should otherwise be able to use the same code, of course). And if you want to cache it, you'll have to build your own (just save arrays with lrank and srank in a global array with userid as index, and check that before executing your code). But you're not doing a db query or anything to get your answer, so if it were me I probably wouldn't bother.

KGodel
04-23-2015, 09:50 PM
Yea, after I thought about it I realized even if it did call fetch_userinfo, it would still be calling that on each individual post so I wouldn't really be saving any time code-wise. I just switched it to $post and moved to the postbit_display_complete hook and it works like a charm. Now just a bit more and it'll be doing what I want.