MyBB Community Forums

Full Version: How to add script only if admin to footer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there

I want to added the following to my footer template:

<script type="text/javascript" src="jscripts/inline_moderation.js"></script>
But, only if he user is the admin (or a moderator)

I dont see the point of everyone having to download this script, when only one type of user needs it.

Is there a way to customise the footer template to say

"[if user is moderator] the do this [/If]"


One thought I had was to add to the $lang array(in global.lang.php), so that I use: {$lang->ScriptInlinemod} and this is empty if not mod, and the above string if mod. I was just wondering if you already have a more 'correct' way of doing this sort of thing

On seconds thoughts, lang isnt the best place to do this (since I need to get to some included functionality that will allow me to check if the user is a mod)

Any thoughts?

functions_forumlist is where I've added to the lightbulb previoulsy, so I might look there:
http://community.mybb.com/thread-85158.html
Erm, it already does only get included for people that are able to use it... Undecided If it's getting included for everybody on your forum, then you've changed the location of where the file is included.
Yes, you're right I have changed the location. I've put it in the footer template now (there's no point in loading scripts before the page is rendered)

But, I also obvioulsy dont want to load this for everyone

(2011-01-27, 12:56 AM)Tenants Wrote: [ -> ]I want to add the following to my footer template:
<script type="text/javascript" src="jscripts/inline_moderation.js"></script>
But, only if he user is the admin (or a moderator)


This bit of script has dependance on the prototype framework, in such a way that prototype needs to be loaded before this script can be used. The way mybb does this, is to load prototype in the head section. Loading prototype in the head section is a little slow and not good practice for users perception of performance.

I've now moved all of my scripts (and merged many) to the footer section, and the inline_moderation.js now also needs to be placed in the footer section (after the prototype script).

I just need a way to only load it when the user is a moderator



In forumdisplay.php I have found this:

if(is_moderator($fid))
{
	eval("\$inlinemodcol = \"".$templates->get("forumdisplay_inlinemoderation_col")."\";");
	$ismod = true;
	$inlinecount = "0";
	$inlinecookie = "inlinemod_forum".$fid;
	$visibleonly = " AND (visible='1' OR visible='0')";
	$tvisibleonly = " AND (t.visible='1' OR t.visible='0')";
}

So, I'm going to start playing with this is_moderator() function and see what I can come up with
If you really need to do it like this then you'll need a plugin, which is going to mean having a plugin run on every page, and having an extra query on every page, there's no way of doing it otherwise, unless you want to start editing core files. Personally I think you're going a bit OTT with the javascript merging and moving, but if you want to do it like this, you'll need to run additional code and run additional queries.
I am OTT... Plugins are a no-no for me, every millisecond counts.

I've done it by changing core files:



global.php makes use of the mybb object and is_moderator function
(I've also seen the mybb object used in the templates)
So this seems as good a place as any to add a bit custom script

The mybb object is defiend in class_core.php

I've edited the mybb object to include a public string:
public $inline_script = "";


The footer template will now include a line:
 {$mybb->inline_script}


and now I've updated global.php to include:
// custom hack, include the inline mod script only if the user is mod:
if($mybb->user['ismoderator']){$mybb->inline_script = "<script type=\"text/javascript\" src=\"jscripts/inline_moderation.js\"></script>";}


This works!
When the script loads, it only loads in the footer and only if the user is a mod

Cheers

<others users should be warned not to do this if they want updates in the future... this is a custom modification and could really screw things up if you upgrade / want to change anything in the future.... do this at your own risk>

I'm getting google page speed scores for the forum of about 94/100, so I still have a little bit of work (yup.. OTT)
The non-cached page load times are about 400-600ms {it was much more before doing this custom work}

mybb is great for hacking away at and making your own customisations (its much easier than vb / phpbb)

a quick non-cached comparison of my stripped down forum, compared to this page:
<you can also manually time this by holding shift and refresh>

This page: (google page speed score: 80/100 ... which by the way, is really good for a forum)
http://tools.pingdom.com/?url=http://com...save=false (Takes about 5 seconds to load)
Total objects: 64 (326.3 KB)

[I think firebug shows more realistic results than pingom tools, but you can get the idea of what is happening using pingdom and in what order... Although pingdom seems to grab everything defiend in the css, which doesnt actaully happen]

One of my pages: (google page speed score: 94/100)
http://tools.pingdom.com/?url=http://www...save=false (Takes about 0.5 seconds to load)
Total objects: 11 (294.4 KB)

- I've added the total size of the data so you can see the differance is more to do with design rather than size of data
?

What do you mean by a good inderstanding of users, can you clarify? The users of my forum dont care what happens with the scripts, they just want a fast and nice forum...

The hack in post #5 works, and can be used to add custom scripts by adding a public string to the mybb object, and then later using it by calling the string from the mybb object inside a template...