News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Trying to add global functions to all .template files

Started by Glyph, November 25, 2015, 03:42:34 AM

Previous topic - Next topic

Glyph

i am currently trying to use an add_integration_function ("call_endpoint", '$sourcedir/pvp.php', true);
into display.template.php

i have a function in pvp.php that i would like to use in display.template.php and other template files, is there any way to do this?

I know i could use a get or post or include the file, but i'm trying to avoid that if SMF has a function to integrate it in its own way; i was assuming hooks could do this.


my current install file:
<?php 
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF')){
{
require_once(dirname(__FILE__) . '/SSI.php');

}
// Hmm... no SSI.php and no SMF?
}elseif (!defined('SMF')){
die('<b>Error:</b> Cannot install - please verify you put this in the same place as SMF\'s SSI.php.');
//      Start building and creating the database
}
add_integration_function ("call_endpoint"'$sourcedir/pvp.php'true);


i figured the <?php  add_integration_function ("call_endpoint"'$sourcedir/pvp.php'true); would make an update to the mysql database, so that anywhere i called a "call_integration_hook" i could use my function (function call_endpoint) in pvp.php from any file that had the associated  "call_integration_hook" much in the same way an "include" works in php.

in pvp.php my function starts like this:
<?php
function call_endpoint(&$userid,&$gw2action)


i thought i could call it from display.tempate.php by adding this to the script:
<?php
$"1"
$2="action1"
$action_result=call_integration_hook('call_endpoint', array($1, $2));
echo 
$action_result[0]['name'];


i also tried directly ccalling the function but it wasnt found.
Personal TODO:

Glyph

just incase anyones interested,

<file name="$sourcedir/Load.php">
has to do with alot of the database loading.

so your install file should be something like:
if(!array_key_exists('db_add_column', $smcFunc))
db_extend('packages');
foreach ($column_array as $key => $data)
{
$smcFunc['db_add_column'](
'{db_prefix}members',
$data,
array(),
'update',
'fatal'
);
}


with the column structure in between the loop and db_extend.

There are a ton of select statments in load.php, so based on what you're trying to o you'll need to modify it accordingly. fortunately alot of mods have already edited key areas of the forums so it may just be a matter of modifying the code to your needs.

once that is done, you should then be able to call  $context['member']['whateverdatauwanthere']['andhere'] (code may vary)
Personal TODO:

Suki

Don't really sure if I understand what are you trying to do.  Seems irrelevant to call a hook just to call a function,  you might as well just call the function directly
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Glyph

Quote from: Suki on November 25, 2015, 09:15:19 PM
Don't really sure if I understand what are you trying to do.  Seems irrelevant to call a hook just to call a function,  you might as well just call the function directly

I have
function example(&$param1){
return array($values)
}

i would like to call this function in Display.template.php, and many other .template.php's (for lack of a better word) without the use of includes, or using GET/POST requests. I would like to get the return value from a global variable from SMF itself. (e.g. $context or $modSettings)

i've seen other mods do it so i'm trying to learn how to do it. i'm not familiar with the terminology.
Personal TODO:

Suki

CAn you post a link to a mod or mods that are doing the same thing? I'm just trying to get a clearer view of what you want.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Glyph

Quote from: Suki on November 26, 2015, 12:00:02 PM
CAn you post a link to a mod or mods that are doing the same thing? I'm just trying to get a clearer view of what you want.

http://custom.simplemachines.org/mods/index.php?mod=4001

in install.xml it modifies load.php

also display.tempate.php where
$message['member']['twitter']['link'] for example.

and under install.php it updates the SQL information
Personal TODO:

Glyph

Looking through http://custom.simplemachines.org/mods/index.php?mod=417
the mod adds a function to Subs.php
CountryFlag()
that looks like its called "globally", this is exactly what i was looking for!

now i ran into another issue, i am using the prettyurls mod for my website and i'm trying to make everything work.


<?php
// Show a link to the member's profile.
                    
$endpoint_action "account_name";
                    
//echo 'my id is: '.$message['member']['id'].'<br />';
                    
$endpoint_data call_endpoint($message['member']['id'], $endpoint_action);
                        if (
$endpoint_data != ''){
                            
//split the original link by '<a href="http://gw2pvp.com/forum/index.php?action=profile;u='
                            
$pass1_member_link explode('<a href="http://gw2pvp.com/forum/index.php?action=profile;u='$message['member']['link']);
                            
var_dump($message['member']['link']);
                            
//split the link again by '" title'
                            
$pass2_member_link explode('" title'$pass1_member_link);
                            
//and split it again by '">'
                            
$pass3_member_link explode('">'$pass2_member_link);
                            
//substr($pass3_member_link[1],0,-4); //FORUM DISPLAY USERNAME if you need it
                            
echo $pass3_member_link[0].'">'$endpoint_data[0]['name']."</a>";
                        }else {
                            echo 
$message['member']['link'];
                        }


basically, this will work without prettyurls, but with prettyurls it changes the URL so that my explode functions are doing nothing.

fixed with:

<?php
// Show a link to the member's profile.
                    
$endpoint_action "account_name";
                    
$endpoint_data call_endpoint($message['member']['id'], $endpoint_action);
                            echo 
str_replace($message['member']['name'],$endpoint_data[0]['name'],$message['member']['link']);
Personal TODO:

Advertisement: