View Full Version : HTML Code in Widget
thunderclap82
10-06-2013, 03:40 PM
I have AJAX Chat installed on my site, and there is code someone shared that allows the number of users using chat, as well as listing the names, that appear in the What's Going On section. (See here. (https://vborg.vbsupport.ru/showthread.php?t=269509&highlight=ajax+chat)) It involves a plugin and template change and, once done, works. I'd like to use this code in a Widget on the front page but no matter how I alter the code it doesn't work in the widget. The code for the templates is:
<!-- chat users -->
<div id="wgo_onlineusers" class="wgo_subblock section">
<h3 class="blocksubhead"><img src="{vb:stylevar imgdir_misc}/users_online.png" alt="Users In Chat" />{vb:raw num_chatting} Users In Chat</h3>
<div>
<p>{vb:raw chat_userlist}</p>
</div>
</div>
<!-- end chat users -->
Basically, in the widget, it doesn't convert the num_chatting or chat_userlist. Is it because widgets can't handle these calls? Is there a way to get this code working in a static HTML widget?
You probably need to preRegister the variables to the template you're using. In the code on that page there are calls to vB_Template::preRegister() for FORUMHOME and navbar, but you need to change it (or add calls) to register to your template.
thunderclap82
10-06-2013, 07:00 PM
You probably need to preRegister the variables to the template you're using. In the code on that page there are calls to vB_Template::preRegister() for FORUMHOME and navbar, but you need to change it (or add calls) to register to your template.
I created a new template called "ajax_chat_widget" and added the HTML code to that:
<h3 class="blocksubhead"><img src="{vb:stylevar imgdir_misc}/users_online.png" alt="Users In Chat" />{vb:raw num_chatting} Users In Chat</h3>
<div>
<p>{vb:raw chat_userlist}</p>
</div>
I had created the plug-in as instructed:
global $vbulletin;
$results = $vbulletin->db->query_read_slave("SELECT userName FROM ajax_chat_online");
while ($row = $vbulletin->db->fetch_array($results))
$chat_userlist[] = $row['userName'];
if (is_array($chat_userlist))
{
$chat_userlist = implode(', ', $chat_userlist);
$vbulletin->db->free_result($results);
}
else
{
// set $chat_userlist to a "no one chatting" message if you want, or leave blank.
$chat_userlist = '';
}
vB_Template::preRegister('FORUMHOME', array('chat_userlist' => $chat_userlist));
And added a second preRegister:
vB_Template::preRegister('ajax_chat_widget', array('chat_userlist' => $chat_userlist));
I then created the Static HTML Widget and have it configured to use the template "ajax_chat_widget", and in the HTML code box I add "a". If I leave it blank the widget isn't displayed at all.
The widget now appears, but only says "Users in Chat". Looking at the code it should show the number of users (i.e. 2 Users in Chat) as well as list their names, but it doesn't.
Thoughts?
Instead of a static html widget, try creating a php direct execution widget and use code like this:
$templater = vB_Template::create('ajax_chat_widget');
$output = $templater->render();
Also, those instructions you linked to show another plugin using hook global_bootstrap_init_start that calculates the number chatting, so you'd need to add another preRegister call there as well.
When configuring the php direct execution widget, there's a field for cache time. If you set it to 0 it will execute every time someone accesses a page that shows the widget, but if you set it to 1 (the minimum allowed), then the widget will only update once per minute. You can choose which one works for you. Setting it to 0 probably isn't a problem since you aren't doing much in your widget code, and the database queries in the plugin are being done each time anyway.
thunderclap82
10-06-2013, 08:12 PM
Appreciate the help. I enabled the other plug-in and added the preRegister for ajax_chat_widget in that plugin, and now the number of people chatting shows but the list of users is still empty. I even attempted to add
vB_Template::preRegister('ajax_chat_widget', array('chat_userlist' => $chat_userlist));
to the other template but that didn't work.
Hmm...it might have to do with the order that things happen. I'm away from my test system so I can't try it right now. You left the preRegister in for $chat_userlist, right?
Did you set the cache time to 0? You'd want to do that while working on it even if you've decided to set it to 1 minute, otherwise it's easy to get confused when things don't change right away.
thunderclap82
10-06-2013, 08:55 PM
Hmm...it might have to do with the order that things happen. I'm away from my test system so I can't try it right now. You left the preRegister in for $chat_userlist, right?
You mean this code?
vB_Template::preRegister('ajax_chat_widget', array('chat_userlist' => $chat_userlist));
Yes. I left it in.
Did you set the cache time to 0? You'd want to do that while working on it even if you've decided to set it to 1 minute, otherwise it's easy to get confused when things don't change right away.
I set it to 1 and left it for 5-10 min and came back, but it was still empty.
--------------- Added 1381161136 at 1381161136 ---------------
I fixed the problem. I ended up changing the hook from global_start to global_bootstrap_init_start. Not sure if that will mess anything else up, but it got the job done. Thanks again for all your help, kh99.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.