Commit 422d5b50 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Implement plugin API for custom functions

parent 42a971be
......@@ -51,3 +51,6 @@ VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_CONTAIN_ARGS="List function '%s' requires
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN="Unknown list function '%s' encountered. (Full rule: '%s')"
VMSHIPMENT_RULES_NOSHIPPING_MESSAGE="%s"
VMSHIPMENT_RULES_CUSTOMFUNCTIONS_NOARRAY="Definition of custom functions (returned by a vmshipmentrules plugin) is not a proper array. Ignoring."
VMSHIPMENT_RULES_CUSTOMFUNCTIONS_ALREADY_DEFINED="Custom function %s already defined. Ignoring this definition and using previous one."
......@@ -59,3 +59,9 @@ VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_CONTAIN_ARGS="List function '%s' requires
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN="Unknown list function '%s' encountered. (Full rule: '%s')"
VMSHIPMENT_RULES_NOSHIPPING_MESSAGE="%s"
VMSHIPMENT_RULES_UNKNOWN_TYPE="Unknown rule type '%s' encountered for rule '%s'"
VMSHIPMENT_RULES_CUSTOMFUNCTIONS_NOARRAY="Definition of custom functions (returned by a vmshipmentrules plugin) is not a proper array. Ignoring."
VMSHIPMENT_RULES_CUSTOMFUNCTIONS_ALREADY_DEFINED="Custom function %s already defined. Ignoring this definition and using previous one."
......@@ -64,6 +64,34 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
$this->tableFields = array_keys ($this->getTableSQLFields ());
$varsToPush = $this->getVarsToPush ();
$this->setConfigParameterable ($this->_configTableFieldName, $varsToPush);
// PLUGIN FUNCTIONALITY:
// Let other plugins add custom functions!
// The onVmShippingRulesRegisterCustomFunctions() trigger is expected to return an array of the form:
// array ('functionname1' => 'function-to-be-called',
// 'functionname2' => array($classobject, 'memberfunc')),
// ...);
JPluginHelper::importPlugin('vmshipmentrules');
$dispatcher = JDispatcher::getInstance();
$custfuncdefs = $dispatcher->trigger('onVmShippingRulesRegisterCustomFunctions',array());
// Loop through the return values of all plugins:
foreach ($custfuncdefs as $custfuncs) {
if (empty($custfuncs))
continue;
if (!is_array($custfuncs)) {
$this->printWarning(JText::sprintf('VMSHIPMENT_RULES_CUSTOMFUNCTIONS_NOARRAY', $method->rule_name));
}
// Now loop through all custom function definitions of this plugin
// If a function was registered before, print a warning and use the first definition
foreach ($custfuncs as $fname => $func) {
if (isset($this->custom_functions[$fname])) {
$this->printWarning(JText::sprintf('VMSHIPMENT_RULES_CUSTOMFUNCTIONS_ALREADY_DEFINED', $fname));
} else {
vmDebug("Defining custom function $fname");
$this->custom_functions[strtolower($fname)] = $func;
}
}
}
}
/**
......@@ -1087,6 +1115,13 @@ class ShippingRule {
function evaluateFunction ($function, $args) {
$func = strtolower($function);
// Check if we have a custom function definition and use that if so.
// This is done first to allow plugins to override even built-in functions!
if (isset($this->plugin->custom_functions[$func])) {
vmDebug("Evaluating custom function $function, defined by a plugin");
return call_user_func($this->plugin->custom_functions[$func], $args);
}
// Functions with no argument:
if (count($args) == 0) {
$dt = getdate();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment