Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 03-10-2006, 01:12 AM
mdevour mdevour is offline
 
Join Date: Feb 2006
Posts: 26
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Variable scope for a plugin...

I want to create a variable and use it in template conditionals in a few places. So far I'm not getting the scope right, or I haven't figured out the right way to access it within the templates...

I've created a plugin with just this code in it:

Code:
include('./plugins/my_mod.php');
The code in my_mod.php looks something like this:

Code:
<?php

foreach ($vbulletin->forumcache as $forumdata)
	{
	if ( strpos($forumdata["description"], '<!-- something -->') !== false )
		{
		$something[] = $forumdata["forumid"];
		}
	}
?>
This should create an array with elements that are the forumid's for those forums that I've hidden a particular string in the forum descriptions.

The plugin needs to affect the forumhome_forumbit_level1_nopost template as well as the display of navbits in forumdisplay and showthread, at least.

Which hook should I use to make sure the variable $something will be valid when and accessible where I need it?

How do I reference the variable from a template? I've written and tested template conditionals using vbulletin class variables, so I've got the basic syntax working okay. How do I make a variable I create have the same sort of scope?

Finally, as an aside, is hiding stuff in the forum description a really stupid way to customize something to begin with?

Thanks! All help will be much appreciated.

Mike D.
Reply With Quote
  #2  
Old 03-10-2006, 08:36 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You could add a global $something in the top.

And yes, hiding something in the title is not a good way.
Reply With Quote
  #3  
Old 03-10-2006, 10:14 AM
mdevour mdevour is offline
 
Join Date: Feb 2006
Posts: 26
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarcoH64
You could add a global $something in the top.
Please clarify, Marco. The top of what? The my_mod.php file?

I'm still learning php. I thought $global allows a function to use a variable that has scope in the place the function is called from. I have not seen anything to indicate that it forces a variable used in the function to have scope in the script that is calling it. Is that what you're saying?

[edit: I realize the snippet of code in my_mod.php isn't a function. It still leaves the question of how to make a variable global in scope?]

Quote:
Originally Posted by MarcoH64
And yes, hiding something in the title is not a good way.
I want a setting that will let me change how the forumhome_forumbit_level1_nopost template displays and whether the forum shows in the navbar on other pages. I've got the template conditional in the forumbit working great. If I hard-code the forumid's into the condition, it displays just as I want it to.

Rather than have code hidden away in a template that has to be hand tweaked if I decide to change the setting on a forum, it would be ideal to add a new forum attribute, just like post/nopost or colors, or whatever.

Hiding something in the forum description that won't be displayed looks like one way to accomplish that without hacking the code. In fact, not only does it not display, it doesn't even get sent to the browser as an html comment.

I've been trying to avoid modifying vBulletin code. In fact, I haven't even looked at any of the adminCP code or templates yet. If I really can't do this in a clean way without a hack, I guess now is the time to learn.

Is it possible to add my extra forum attribute as a template mod to the adminCP? If not, please point me in the right direction and give me a nudge? I don't even know where to start looking. <sigh>

Thanks,

Mike D.
Reply With Quote
  #4  
Old 03-10-2006, 10:29 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
<?php
global $something;

foreach (
$vbulletin->forumcache as $forumdata)
    {
    if ( 
strpos($forumdata["description"], '<!-- something -->') !== false )
        {
        
$something[] = $forumdata["forumid"];
        }
This will make the variable $something that is in the namespace of the calling script (ie. outside the function) accessible within the function. If the variable has not been defined/used yet it will be created implicit on the first use. If you now set $something to a value in your function, it will be accessible outside the function.

For adding new options to a Forum, see: https://vborg.vbsupport.ru/showthread.php?t=93445
Reply With Quote
  #5  
Old 03-10-2006, 02:20 PM
mdevour mdevour is offline
 
Join Date: Feb 2006
Posts: 26
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarcoH64
PHP Code:
<?php
global $something;

foreach (
$vbulletin->forumcache as $forumdata)
    {
    if ( 
strpos($forumdata["description"], '<!-- something -->') !== false )
        {
        
$something[] = $forumdata["forumid"];
        }
This will make the variable $something that is in the namespace of the calling script (ie. outside the function) accessible within the function.
This is what I understood about scope in PHP, so far.

Quote:
If the variable has not been defined/used yet it will be created implicit on the first use. If you now set $something to a value in your function, it will be accessible outside the function.
That is new to me. I did not realize that a function can create a variable and assign it scope outside of itself, if that's what you're saying. It's not something they emphasize in any of the documentation I've read.

Also, that raises a question about the plugin system. I was thinking that the my_mod.php file was being included, but it must be being executed within a call to the fetch_hook function. If the $global pushes the variable scope up to the calling context, then it makes sense. Do I have that right?

Quote:
For adding new options to a Forum, see: https://vborg.vbsupport.ru/showthread.php?t=93445
Am I reading that right, it is all done with plugins? No code hacks? I'm still allowed to talk to vbulletin.com??? :nervous:

That looks like exactly what I'm trying to do, Marco. Bless you!

I'll try the $global statement and get it working my way. Then I'll go back and do it the right way. I don't mind finding out better ways to do what I've already done. The learning doesn't go away when I delete the dumb code!

Thank you for the help.

Be well,

Mike D.
Reply With Quote
  #6  
Old 03-10-2006, 02:28 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It is not the plugin system itself that create a new scope. But some hooks are called from the main scope, and some are called from within a function. Hence sometimes you will need a global $var, and sometimes not.
Reply With Quote
  #7  
Old 03-11-2006, 02:06 AM
mdevour mdevour is offline
 
Join Date: Feb 2006
Posts: 26
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Okay, I have my variable showing up where I wanted, but I still have a question, below.

I had to analyse the way the scripts call each other and the templates before I could get things right...
  • index.php calls the function construct_forum_bit (which is found in functions_forumlist.php)
  • construct_forum_bit fetches the template forumhome_forumbit_level1_nopost
The only way I could get my array, $something, to be available in the forumhome_forumbit_level1_nopost template was to add another hook:

The hook named forumbit_display, which is near the end of construct_forum_bit, now contains:
PHP Code:
global $something
I can now make the template conditional in the forumbit template use the $something variable, and it works except for one problem...

The forumhome_forumbit_level1_nopost template now contains this (the code I added is in color):

Code:
<tbody>
	<tr>
...code that shows the moderator column if enabled...

		<if condition="in_array($forum[forumid],$something)">

			$forum[title] 

		<else />
	
...original code that displays the forum title as a link, and the collapse button...

		</if>

...code that displays the forum description... 
	</tr>
</tbody>
...code that displays any subforums...
Let me stress again that this code works. If the forum is a category and the forumid is in the $something array, my simplified code executes. If not, the original code executes and the category is displayed as the designers intended.

The only problem is that when I put the in_array($forum[forumid],$something) function in the template conditional and try to save the template, it reports:

Quote:
The following error occurred when attempting to evaluate this template:

Warning: in_array(): Wrong datatype for second argument in /includes/adminfunctions_template.php(3537) : eval()'d code on line 5

This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.
I tinkered with this for a long time before I discovered that if I forced it to continue everything would work just fine. Several minor variations I had attempted all worked as well when I tried them again.

So am I bumping up against a limitation of the Template Manager's parsing algorythms, or what? Is my code still flawed, despite the fact it works?

I'd like these final questions answered so I can learn all I can from this attempt, before I move on to doing it the way Marco suggested above.

I'm getting better. I really am! Thanks for all the help people!

Mike D.
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 03:21 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.02331 seconds
  • Memory Usage 2,243KB
  • 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
  • (3)bbcode_code
  • (3)bbcode_php
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)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_postinfo_query
  • fetch_postinfo
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete