From 422d5b509be88260e995c7e889c171b8d304be78 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Thu, 13 Nov 2014 19:11:16 +0100 Subject: [PATCH] Implement plugin API for custom functions --- .../de-DE.plg_vmshipment_rules_shipping.ini | 3 ++ .../en-GB.plg_vmshipment_rules_shipping.ini | 6 ++++ rules_shipping_base.php | 35 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/language/de-DE/de-DE.plg_vmshipment_rules_shipping.ini b/language/de-DE/de-DE.plg_vmshipment_rules_shipping.ini index ec6c6ec..33d8eaf 100644 --- a/language/de-DE/de-DE.plg_vmshipment_rules_shipping.ini +++ b/language/de-DE/de-DE.plg_vmshipment_rules_shipping.ini @@ -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." diff --git a/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini b/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini index d18c37c..4f9631a 100755 --- a/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini +++ b/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini @@ -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." diff --git a/rules_shipping_base.php b/rules_shipping_base.php index 493180b..5811056 100644 --- a/rules_shipping_base.php +++ b/rules_shipping_base.php @@ -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(); -- GitLab