PDA

View Full Version : PHP function in a template


T_Richardson
09-18-2007, 10:53 AM
I have this users.php function code to list online users. I would like to include this on a non-vb page that uses templates.

<?
chdir('./');
require_once('./global.php');

// ---------------------------------------------------
// Start Globalized Function - LoadTemplate
// ---------------------------------------------------

function LoadTemplate($template = ""){

$template = "vBExternal/{$template}";

if(!file_exists($template)){
RunError("System was unable to find the template '{$template}'");
}

if(!$Handler = fopen($template,'r')){
RunError("System was unable to open the template '{$template}'");
}

$template = fread($Handler,filesize($template));
fclose($Handler);

return $template;
}

// ---------------------------------------------------
// Start Globalized Function - RunError
// ---------------------------------------------------

function RunError($message = ""){
echo "<font size='1' face='verdana'>There was an error while processing vBExternal:<br />{$message}</font>";
exit;
}

// ---------------------------------------------------
// End Globalized Function - RunError
// ---------------------------------------------------

// ---------------------------------------------------
// Start Globalized Function - ParseTemplate
// ---------------------------------------------------

function ParseTemplate($template, $parser = array(), $doGlobals = 0){
global $vbulletin;

if(is_array($parser)){
foreach($parser as $find => $replace){
$template = str_replace("{".$find."}", $replace, $template);
}
} else if($doGlobals){
$RepGlobals = array(
'url' => $vbulletin->options['bburl'],
);

foreach($RepGlobals as $find => $replace){
$template = str_replace("{".$find."}", $replace, $template);
}

}

return $template;
}

// ---------------------------------------------------
// End Globalized Function - ParseTemplate
// ---------------------------------------------------

function output_UsersOnline(){
global $db, $Data, $vbulletin;

// Load Template
$Template = LoadTemplate("users_online.html");

$cache = array();

// Collect Data
$datecut = TIMENOW - $vbulletin->options['cookietimeout'];
$UsersOnline = $db->query("
SELECT
user.username, (user.options) AS invisible, user.usergroupid,
session.userid,
IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid
FROM " . TABLE_PREFIX . "session AS session
LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = session.userid)
WHERE session.lastactivity > $datecut and user.userid > 0
" . iif($vbulletin->options['displayloggedin'] == 1, "ORDER BY username ASC") . "
");

while($User = $db->fetch_array($UsersOnline)){
if(!$cache[$User['userid']]){
$cache[$User['userid']] = $User['userid'];

$Data .= ParseTemplate($Template,
array(
'userid' => $User['userid'],
'username' => $User['username'],
)
);
}
}

unset($cache);
doOutput();
}

function doOutput(){
global $Data, $vbulletin;

$Data = str_replace('images/',"{$vbulletin->options[bburl]}/images/",$Data);

echo ParseTemplate($Data,"",1);
$Data = "";
}

?>




That php file works if you include output_UsersOnline() in the file itself.

What would be the proper way construct this code in a plugin so I can call it in a template with a variable ie; $onlineusers

Guest190829
09-18-2007, 11:14 AM
If you are including global.php already, why making custom functions for already built in functionality? (Fetching templates and parsing them.)

Have you looked at this tutorial? :

https://vborg.vbsupport.ru/showthread.php?t=98009

It should point you in the right direction.

T_Richardson
09-18-2007, 11:16 AM
The template that it's parsing is an html page that puts the users online in clickable link to they're profile.

T_Richardson
09-19-2007, 08:38 PM
Anyone have any idea. Surely there's some guru out there that can point me in the right direction :)