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 ec6c6ec1355c692ce595d6ae681c57e580f301a0..33d8eaf56e79bd2d44d7082df39550da667027a2 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 d18c37c4fc1216de8c2ca7abb24f02ce0252360c..4f9631a70115482b80ea801f0f576195c8738cf1 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 493180b7be0258d53934a173526d354653216775..5811056b22669ed39d037aa223af31b311c34a37 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();