From my searches on the forums, sometimes we all want to access some XOOPS variable(s) that aren't available in our templates and themes. The biggest one I run up against is wanting to use the XOOPS Real Name (I usually call it the Display Name in my implementations) instead of the login (this is actually somewhat good, from a security standpoint). But there may be others, too, that you need, like the ID/Name/Title of the current module and such. After some hacking, I came up with the methodology below. The concept should work in themes as well as templates. I've also included a small example for an enhanced search bar that I've been playing with. Would certainly love to hear any additional suggestions and epiphanies this one generates.
Proof of Concept Code <{php}>
// to access Xoops, Smarty, Module variables, must define global
global $xoopsUser, $xoopsModule, $xoopsModuleConfig, $smarty;
// to add a Smarty Constant accessible via
define("XOOPS_MODULE_ID",$xoopsModule->getVar('id'));
define("XOOPS_MODULE_NAME",$xoopsModule->getVar('name'));
// to add a Smarty Variable to be accessible later in template
$xoopsTpl->assign('module_name',XOOPS_MODULE_NAME);
<{/php}>
<{* if global var doesn't exist (say $xoopsUser for Anonymous User) then php will fail and page will be blank, so you have to not access unless able to, like this example *}>
<{if $xoops_isuser}>
<{php}>
// Add a Smarty Constant from $xoopsUser (but fails if Anonymous User and $xoopsUser doesn't exist)
define("XOOPS_REALNAME",$xoopsUser->getVar('name'));
<{/php}>
<{/if}>
<p>Welcome, <{if $smarty.const.XOOPS_REALNAME}><{$smarty.const.XOOPS_REALNAME}><{else}>Guest<{/if}>! The currently displayed module mame is: <strong><{$module_name}>strong> (ID: <{$smarty.const.XOOPS_MODULE_ID}>).p>
Note: this solution works only for the later releases of XOOPS with the updated Smarty engine. Also for the example above, you must be logged in and on a module page (or the code fails and gives a blank page, see the search example below for more information). The global definition allows access to the existing variables in XOOPS or the active module code. The $smarty variable must be in the globals definition to allow a define to show up in the $smart.const.CONSTANTS. And if you try to access the non-existent $xoopsUser variable as an anonymous user, you'll get a blank page, thus the second set of code for that even though the global is defined above that (I couldn't quickly find a way to test it in PHP without getting a blank page, thus this hack).
Maybe I should look at making some of this stuff available in the core if there is enough desire/need. I somewhat expected to find this information available.
Search Bar ExampleI wanted a search bar that allowed me to search either the current module or the entire site. I had found code to accomplish this using the core Search block (system module), but like many other themes I had seen, I wanted it in a Search Bar in the header, thus that solution didn't work.
I placed this code near the top of theme.html:
<{php}>
global $xoopsUser, $xoopsModule, $smarty;
<{/php}>
<{if $xoops_dirname != 'system'}>
<{php}>
define("MODULE_ID", $xoopsModule->getVar('mid'));
define("MODULE_NAME",$xoopsModule->getVar('name'));
if ($xoopsModule->getVar('hassearch')) {
define("MODULE_HASSEARCH",true);
} else {
define("MODULE_HASSEARCH",false);
}
<{/php}>
<{/if}>
<{if $xoops_isuser}>
<{php}>
define("XOOPS_REALNAME",$xoopsUser->getVar('name'));
<{/php}>
<{/if}>
Then, in my searchbar theme code, I used this (all display via CSS):
<div id="xo-searchbar">
<form name="searchbar" method="post" action="<{xoAppUrl /search.php}>" title="<{$smarty.const.THEME_DESC_SEARCH}>"><input type="text" id="query" name="query" class="keyword" value="<{$smarty.const.THEME_KEYWORDS}>" onfocus="this.value=(this.value=='<{$smarty.const.THEME_KEYWORDS}>')? '' : this.value ;" maxlength="255" /><input type="hidden" name="action" id="action" value="results" />
<input type="image" title="Search Entire Site" src="<{xoImgUrl /img/nav/search-global.gif}>" onclick="javascript:for(i=0;t=document.searchbar.elements[i++];t.checked=false);" /><{if $xoops_dirname != 'system' && $smarty.const.MODULE_HASSEARCH == true}><input type="checkbox" name="mids[]" value="<{$smarty.const.MODULE_ID}>" style="display:none;" /><input type="image" title="Search <{$smarty.const.MODULE_NAME}> ONLY" src="<{xoImgUrl /img/nav/search-module.gif}>" onclick="javascript:for(i=0;t=document.searchbar.elements[i++];t.checked=true);" /><{/if}>form>
div>
[Note: this could also be extended fairly easily to allow a user to search a defined web search engine, and/or maybe even allow them to pick which search engine, done creatively. Also, this search capability requires Javascript; otherwise it won't function properly making both buttons search the entire site (does that count as failing gracefully?).]
I'll note that I looked around for how to do some of this and couldn't find it, although it could exist on the on-long-hiatus dev site that I used to contribute to (and instead now post on X.o and on my personal site to keep for future reference). Anyhoos, I hope it's helpful. Comments appreciated.