Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-07-2012, 07:55 AM
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Posts: 305
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default trying to get user name in widget

I'm trying to retrieve the user name - or just see if the user is signed in - in an HTML widget using javascript but it isn't working

I tried using this but it didn't work

var username =$bbuserinfo[username];


Can someone post a sample script?

Thanks
Reply With Quote
  #2  
Old 05-07-2012, 08:36 AM
LifesGreatestGift's Avatar
LifesGreatestGift LifesGreatestGift is offline
 
Join Date: Jul 2009
Location: Louisville, KY USA
Posts: 885
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

cant you just use a php widget and do this
PHP Code:
if ($vbulletin->userinfo['userid'] > 0) {
    
$username $vbulletin->userinfo['username'];
    echo 
'Welcome ' $username '!';
} else {
    echo 
'Welcome Guest!';

P.S. I doubt the code above works, its a proof of concept.

Try this if you want to stick to JS

HTML Code:
var username =$vbulletin->bbuserinfo['username'];
not sure if single quotes are needed in JS.
Reply With Quote
  #3  
Old 05-07-2012, 10:52 AM
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Posts: 305
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The code doesn't work in my java function and causes the function to stop due to invalid syntax

Code:
var username =$vbulletin->bbuserinfo['username'];
I'm trying to get the user name in a Static HTML widget using java.
Reply With Quote
  #4  
Old 05-07-2012, 11:09 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The problem is that a static html widget is not a template, so you can't use any variables, only html. You would have to use a php direct execution type widget and set $output to your html (although getting quotes and escape characters right can be tricky). You could also make your html a template then just use a php type widget that renders your template, like:

Code:
$template = vB_Template::create('my_widget_template');
$output = $template->render();
unset($template);

Then in your template you can use

Code:
var name = '{vb:escapeJS {vb:raw bbuserinfo.username}}';
Reply With Quote
  #5  
Old 05-07-2012, 11:13 AM
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Posts: 305
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Are there any functions or API I can call from java that will return the current user name or tell me if the user is logged in? I don't want to rewrite the widget as it has a lot of java code in it.

I tried using the userid cookie but for some reason it is not always present even when I log in.
Reply With Quote
  #6  
Old 05-07-2012, 11:19 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Javascript runs in the browser, so it only has access to data that's included in the html document. If you look at the headinclude template there's a section that sets some javascript variables just for that purpose (so the data is available in js functions on the page). So you could edit the headinclude template and add the username there.

If you switched to a php widget with a template I don't think you'd have to rewrite the widget, you'd just copy what you have to a template and the widget code would be a few lines as posted above (if you wanted you could try it by creating a new widget so if it doesn't work out you can just delete it).
Reply With Quote
  #7  
Old 05-07-2012, 11:22 AM
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Posts: 305
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'll try the head include approach and see if that works. Do I simply assign the variable value there?

Also, any idea why I don't always see the bb_userid cookie even when logged in? At what point is it created?
Reply With Quote
  #8  
Old 05-07-2012, 11:24 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't know about that cookie. I almost suggested that in my previous post but I seemed to remember that it wasn't always set.
Reply With Quote
  #9  
Old 05-07-2012, 11:27 AM
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Posts: 305
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Could you give me an example of the syntax for the header include? I set a variable to the userid but my widget doesn't seem to recognize the variable so I must be doing something wrong
Reply With Quote
  #10  
Old 05-07-2012, 11:30 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

In the template headinclude, there's this code:

Code:
var SESSIONURL = "{vb:raw session.sessionurl_js}";
var SECURITYTOKEN = "{vb:raw bbuserinfo.securitytoken}";
var IMGDIR_MISC = "{vb:stylevar imgdir_misc}";

So I was thinking of adding a line like:

Code:
var USER_NAME = "{vb:raw bbuserinfo.username}";

To be honest I'm not sure what special chars in a user name might cause problems with this, but in any case it seems like that should allow you to use USER_NAME in your js code.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:44 AM.


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.03914 seconds
  • Memory Usage 2,253KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete