vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   HTML Code in Widget (https://vborg.vbsupport.ru/showthread.php?t=303038)

thunderclap82 10-06-2013 02:40 PM

HTML Code in Widget
 
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.) 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:

Code:

<!-- 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?

kh99 10-06-2013 03: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.

thunderclap82 10-06-2013 06:00 PM

Quote:

Originally Posted by kh99 (Post 2450462)
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:

Code:

                                <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:

PHP Code:

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:

PHP Code:

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?

kh99 10-06-2013 06:26 PM

Instead of a static html widget, try creating a php direct execution widget and use code like this:

Code:

$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 07: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

PHP Code:

vB_Template::preRegister('ajax_chat_widget', array('chat_userlist' => $chat_userlist)); 

to the other template but that didn't work.

kh99 10-06-2013 07:44 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?

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 07:55 PM

Quote:

Originally Posted by kh99 (Post 2450513)
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?

PHP Code:

vB_Template::preRegister('ajax_chat_widget', array('chat_userlist' => $chat_userlist)); 

Yes. I left it in.

Quote:

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 [DATE]1381161136[/DATE] at [TIME]1381161136[/TIME] ---------------

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.


All times are GMT. The time now is 04:00 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01709 seconds
  • Memory Usage 1,749KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (4)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete