Skip to content

Instantly share code, notes, and snippets.

@leemason
Last active June 25, 2016 12:59
Show Gist options
  • Save leemason/2d92629c04e7d2b9d13bbc476b97977a to your computer and use it in GitHub Desktop.
Save leemason/2d92629c04e7d2b9d13bbc476b97977a to your computer and use it in GitHub Desktop.
Example assignment of custom triggers for WHMCS Achievements & Rewards Programme
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
//we register the triggers on this hook to ensure the functions and class is loaded without needing to check if the module is enabled.
add_hook('achievements/register_triggers', 1, function($achievements) {
$achievements->registerTrigger([
'group' => 'forum',//the group this trigger belongs to
'name' => 'newtopic',//the name of this trigger (must be unique)
'description' => 'Creates Topic',//the text to us in the dropdown when an admin selects a trigger
'notify_text' => 'Thanks for creating topic: {$topic}, you have been awarded {$award} points!',//example text for what a notification of this trigger should look like. It should detail any custom replacements this trigger offers.
'notify' => true,//would you expect this notification to be displayed (user can override default)
//the matches key should be a callback which recieves the scenario, user id and any custom vars supplied by the fire method
//this is optional and defaults to true, return false to not run the trigger in a scenario
//the example below is from the "purchases" built in trigger, and checks if the product purchased is selected in the scenario meta
'matches' => function ($scenario, $user_id, $vars = []) {
//if
if (isset($scenario->meta->pid) && in_array($vars['pid'], (array) $scenario->meta->pid)) {
return true;
}
return false;
},
//the meta key is again optional and allow you to define additional fields for this trigger when selected in a scenario
//each key is used as the "id" and should be an array
'meta' => [
'pid' => [
'multiple' => true,//if select is a multi select?
'label' => '',//if text the label for the text field
'type' => 'select',//select or text
'options' => function () {
//these are the options for the dropdown, should be an array of arrays with id and name key/values
return Capsule::table('tblproducts')->get(['id', 'name']);
}
]
]
]);
});
//This is how we fire the trigger, you select a whmcs hook point (or custom hook point from your module)
//then check if the class exists and fire the trigger with the name, user id, and any vars needed.
add_hook('PreModuleCreate', 1, function($vars){
//we create an array of vars to pass through, the "matches" callback can use values from this array to determine if the scenario matches (for example here we send the product id)
//we also add an array of key value pairs for "replacements"
//these key assigned to smarty so they can be used within the notify text option
//here "purchase" would become {$purchase} for use in the text.
//{$award} doesnt need to be added to replacements as this is added on every request for notifications.
$_vars = [
'pid' => $vars['params']['pid'],
'replacements' => [
'purchase' => Capsule::table('tblproducts')->where('id', $vars['params']['pid'])->pluck('name')
]
];
if(class_exists('Achievements')){
Achievements::getInstance()->fire('purchase', $vars['params']['userid'], $_vars);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment