PDA

View Full Version : Using non-global phrase groups (problem)


mfyvie
06-18-2007, 09:45 PM
Hi guys, I've been working on this for a few hours, but haven't come up with any result yet. The problem is displaying global phrases - no problem, but any custom phrase group - won't display.

I've looked in member.php which is also displaying my custom phrase group - but I can't see anything that is in there that isn't in my code. I've tried loads of different combinations and haven't got it to work.

Consider this code placed at the top of a plugin which fires at login_verify_success:

global $vbulletin, $vbphrase;
$phrasegroups = array('cprofilefield', 'arcade');
print $vbphrase['field1_title'];
print $vbphrase['field1_desc'];
print $vbphrase['arcade_end'];
print $vbphrase['accept'];

I've double checked that all the variable names are correct, but the only output I get is from the final print statement - as this is a global phrase.

Am I missing something obvious here?

Dismounted
06-19-2007, 07:30 AM
You can only specify which phrase groups to fetch BEFORE global.php is included.

mfyvie
06-19-2007, 07:37 AM
You can only specify which phrase groups to fetch BEFORE global.php is included.

Thanks - I just discovered that while writing a little test file (running directly from a shell script) about 2 minutes ago :D

However, how does someone best approach this problem when working from a hook?

Since global.php has been included already by the script which calls the hook. Or is it no problem to include global.php again inside the plugin? Normally we call global.php with a "require_once" statement?

FractalizeR
06-19-2007, 02:33 PM
I am looking for the same solution...

Just found it:

- <plugin active="1">
<title>Mod Auto-PM :: Include Phrase Groups</title>
<hookname>init_startup</hookname>
- <phpcode>
- <![CDATA[ $phrasegroups[] = 'autopm_title';
$phrasegroups[] = 'autopm_text';
]]>
</phpcode>
</plugin>


So, just create init_startup hook and write only there:

$phrasegroups[] = '<your_phrase_group_name>';

mfyvie
06-19-2007, 03:20 PM
Thanks for that! I spent the entire day today sniffing through the various functions and trying to figure out a way to call the right functions to add a phrase group later in the execution. I didn't even think of looking for an earlier hook to append to the array BEFORE it gets processed.

Just in case anyone finds this thread later on, I've included an example below of what I did (added an additional product at the init_startup hook) :

if (THIS_SCRIPT == 'login')
{
$phrasegroups[] = 'cprofilefield';
}

Note that mod autoPM probably isn't the best example to follow since these phrase groups will be loaded on ALL pages. I limited mine to only appear where I needed it, that is in login.php.

FractalizeR
06-21-2007, 02:22 PM
Exactly. Thank you for idea.

FractalizeR
06-22-2007, 08:44 PM
BTW, don't forget to check admin pages against THIS_SCRIPT also if your hack is using them

mfyvie
06-28-2007, 11:55 PM
I've run into a bit of a problem again with this.

I'm running on the hook in cleanup.php (cron_script_cleanup). I have a phrase which I have defined as belonging to the scheduled tasks group.

If I log into the admincp and manually run the scheduled task (which runs it as my login), then the phrase works.

However, if I just leave the task to run by itself (running as unregistered) the phrase doesn't work. I tried making the phrase global, but this doesn't help either.

Obviously these scheduled tasks normally run as unregistered, so I'd like to find a solution for this.

Anyone have any ideas as to why this would be happening?

FractalizeR
06-29-2007, 09:59 AM
Have you created init_startup hook to load your phrase group? Cron script should execute this hook also when running as unregistered.

mfyvie
06-29-2007, 10:02 AM
Yes, see post #5. However, the cron task doesn't set its script name.

However, I don't believe this is the problem, because when I run the cron job in the admincp, where the job runs as me, the phrase works. Only when the job runs by itself (as unregistered) does the phrase not work.

I'm guessing that somehow an unregistered cronjob has less rights, and doesn't get access to phrases?

Ok, I've done some more testing and discovered what the problem is.

basically /includes/cron/cleanup.php is the only cron job with a hook in it.

However, this file needs to have:

global $vbphrase;

At the top, otherwise phrases are not available to this file, nor the hook.

I could work around this by building my own cron file for what I am working on, but would have preferred to work without additional files.

It seems a little strange when there is an entire phrase group for scheduled tasks, yet phrases aren't available in the only cron job with a hook :confused:

Work-around solution to being unable to get phrases

I found one way to get around this for situations where you can't get $vbphrase into your routine for whatever reason. I just decided to pull the phrase out of the database. Obviously this isn't good for something that is executing many times on every page, but for occassional use (like in a cron job) it should be no drama.

$myphrase = pullphrase("the_name_of_my_phrase");

function pullphrase($phrasename, $language=-1)
{
global $db;
return implode("", $db->query_first("SELECT text FROM " . TABLE_PREFIX . "phrase WHERE varname = '$phrasename' AND languageid = $language"));
}

This could be easily modified to load up an entire phrase group with a single query.

I hope someone else who runs into the same kind of problems I did finds this and it makes life easier for them.