diff --git a/Makefile b/Makefile index 4af0db2287c9cbcb77e28893738320ca5ed4406a..2b12c7027b90ede1a30510ca3fda74004df270b0 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ BASE=rules_shipping BASE_ADV=rules_shipping_advanced PLUGINTYPE=vmshipment ZIPBASE=opentools_vm -VERSION=6.1.7 +VERSION=6.2 PLUGINFILES=$(BASE).php $(BASE)_base.php $(BASE)_framework_joomla.php $(BASE).script.php $(BASE).xml index.html PLUGINFILES_ADV=$(BASE_ADV).php $(BASE)_base.php $(BASE)_framework_joomla.php $(BASE_ADV).script.php $(BASE_ADV).xml index.html diff --git a/images/plg_vmshipping_rules_shipping_messages.png b/images/plg_vmshipping_rules_shipping_messages.png new file mode 100644 index 0000000000000000000000000000000000000000..137db5ecd5920b26f7cb0abed95a3eb454203f11 Binary files /dev/null and b/images/plg_vmshipping_rules_shipping_messages.png differ diff --git a/images/plg_vmshipping_rules_shipping_plugins_icon.png b/images/plg_vmshipping_rules_shipping_plugins_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..7896782784605297bb2f487ae0e3e440af8e1d8f Binary files /dev/null and b/images/plg_vmshipping_rules_shipping_plugins_icon.png differ diff --git a/library/rules_shipping_framework.php b/library/rules_shipping_framework.php index 93fd054f7c672a3928cc1e8fc3984ac2dad059d8..f3d42dd0fa9e6e4f0f6bb65b7b1f2eb6f6f1dc80 100644 --- a/library/rules_shipping_framework.php +++ b/library/rules_shipping_framework.php @@ -171,13 +171,33 @@ class RulesShippingFramework { return $this->custom_functions; } - /** @tag system-specific - * @function printWarning() - * Print a warning in the system-specific way. - * @param $message the warning message to be printed (already properly translated) + /** @tag public-api + * @tag system-specific + * @function message() + * Print a message (to be translated) with given type in the system-specific way. + * @param $message the message to be printed + * @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug") + * @param $args optional arguments to be inserted into the translated message in sprintf-style */ - protected function printWarning($message) { - echo($message); + public function message($message, $type) { + $args = func_get_args(); + // Remove the $type from the args passed to __ + unset($args[1]); + $msg = call_user_func_array(array($this, "__"), $args); + $this->printMessage($msg, $type); + } + + /** @tag public-api + * @tag system-specific + * @function error() + * Print an error message (to be translated) in the system-specific way. + * @param $message the error message to be printed + * @param $args optional arguments to be inserted into the translated message in sprintf-style + */ + public function error($message) { + $args = func_get_args(); + array_splice($args, 1, 0, 'error'); // insert msg type in second position + call_user_func_array(array($this, "message"), $args); } /** @tag public-api @@ -189,16 +209,44 @@ class RulesShippingFramework { */ public function warning($message) { $args = func_get_args(); - $msg = call_user_func_array(array($this, "__"), $args); - $this->printWarning($msg); + array_splice($args, 1, 0, 'warning'); // insert msg type in second position + call_user_func_array(array($this, "message"), $args); + } + + /** @tag public-api + * @tag system-specific + * @function notice() + * Print a message (to be translated) in the system-specific way. + * @param $message the message to be printed + * @param $args optional arguments to be inserted into the translated message in sprintf-style + */ + public function notice($message) { + $args = func_get_args(); + array_splice($args, 1, 0, 'notice'); // insert msg type in second position + call_user_func_array(array($this, "message"), $args); } /** @tag public-api + * @tag system-specific * @function debug() - * Print a debug message (untranslated) in the system-specific way. - * @param $message the debug message to be printed + * Print a debug message in the system-specific way. + * @param $message the message to be printed + * @param $args optional arguments to be inserted into the translated message in sprintf-style */ public function debug($message) { + $args = func_get_args(); + array_splice($args, 1, 0, 'debug'); // insert msg type in second position + call_user_func_array(array($this, "message"), $args); + } + + /** @tag system-specific + * @function printMessage() + * Print a message of given type in the system-specific way. + * @param $message the message to be printed (already properly translated) + * @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug") + */ + protected function printMessage($message, $type) { + echo($message); } /** @tag public-api @@ -422,6 +470,12 @@ class RulesShippingFramework { $this->warning('OTSHIPMENT_RULES_UNKNOWN_TYPE', $r->getType(), $r->rulestring); break; } + // Handle messages (error, warning, message/notice, debug: + foreach ($r->messages as $k=>$msgs) { + foreach ($msgs as $msg) { + $this->message($msg, $k); + } + } } if (!is_null($result["rule"])) { $this->match[$id] = $result; @@ -599,6 +653,7 @@ class ShippingRule { var $countries = array(); var $ruleinfo = 0; var $includes_tax = 0; + var $messages = array('error' => array(), 'warning' => array(), 'notice' => array(), 'debug' => array()); function __construct ($framework, $rule, $countries, $ruleinfo) { $this->framework = $framework; @@ -630,6 +685,11 @@ class ShippingRule { case 'extrashippingcharge': $this->shipping = $value; $this->ruletype = 'modifiers_add'; break; // modifiers are also stored in the shipping member! case 'extrashippingmultiplier': $this->shipping = $value; $this->ruletype = 'modifiers_multiply'; break; // modifiers are also stored in the shipping member! case 'comment': break; // Completely ignore all comments! + case 'error': $this->messages['error'][] = $value; break; + case 'warning': $this->messages['warning'][] = $value; break; + case 'notice': + case 'message': $this->messages['notice'][] = $value; break; + case 'debug': $this->messages['debug'][] = $value; break; case 'condition': $this->conditions[] = $value; break; default: $this->framework->warning('OTSHIPMENT_RULES_UNKNOWN_VARIABLE', $var, $rulepart); } @@ -652,9 +712,8 @@ class ShippingRule { $rulepart = trim($rulepart); if (!isset($rulepart) || $rulepart==='') return; - // Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression! - if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) { + if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) { $this->handleAssignment ($matches[1], $matches[3], $rulepart); return; } @@ -1033,9 +1092,25 @@ class ShippingRule { } } - protected function calculateShipping ($vals, $products, $cartvals_callback) { + protected function calculateShipping($vals, $products, $cartvals_callback) { return $this->evaluateTerm($this->shipping, $vals, $products, $cartvals_callback); } + + protected function stringReplaceVariables($str, $vals) { + // Evaluate the rule name as a translatable string with variables inserted: + // Replace all {variable} tags in the name by the variables from $vals + $matches = array(); + preg_match_all('/{([A-Za-z0-9_]+)}/', $str, $matches); + + foreach ($matches[1] as $m) { + $val = $this->evaluateVariable($m, $vals); + if ($val !== null) { + $str = str_replace("{".$m."}", $val, $str); + } + } + return $str; + + } protected function evaluateRule (&$vals, $products, $cartvals_callback) { if ($this->evaluated) @@ -1059,21 +1134,15 @@ class ShippingRule { } // All conditions match $this->match = True; + foreach ($this->messages as $k=>$msgs) { + foreach ($msgs as $i=>$m) { + $this->messages[$k][$i] = $this->stringReplaceVariables($m, $vals); + } + } // Calculate the value (i.e. shipping cost or modifier) $this->value = $this->calculateShipping($vals, $products, $cartvals_callback); - // Evaluate the rule name as a translatable string with variables inserted: - // Replace all {variable} tags in the name by the variables from $vals - $matches = array(); - $name = $this->framework->__($this->name); - preg_match_all('/{([A-Za-z0-9_]+)}/', $name, $matches); - foreach ($matches[1] as $m) { - $val = $this->evaluateVariable($m, $vals); - if ($val !== null) { - $name = str_replace("{".$m."}", $val, $name); - } - } - $this->rulename = $name; + $this->rulename = $this->stringReplaceVariables($this->framework->__($this->name), $vals); } function matches(&$vals, $products, $cartvals_callback) { @@ -1158,7 +1227,7 @@ class ShippingRule_Advanced extends ShippingRule { // Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression! - if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) { + if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) { $this->handleAssignment ($matches[1], $matches[3], $rulepart); return; } diff --git a/releases/plg_opentools_vm_rules_shipping_advanced_v6.2.zip b/releases/plg_opentools_vm_rules_shipping_advanced_v6.2.zip new file mode 100644 index 0000000000000000000000000000000000000000..50ff70e63d1d399308d3bb2b9a9507d61b9f0a90 Binary files /dev/null and b/releases/plg_opentools_vm_rules_shipping_advanced_v6.2.zip differ diff --git a/releases/plg_opentools_vm_rules_shipping_v6.2.zip b/releases/plg_opentools_vm_rules_shipping_v6.2.zip new file mode 100644 index 0000000000000000000000000000000000000000..ed5b0b8845264598c667d88fb8a62366f590e3aa Binary files /dev/null and b/releases/plg_opentools_vm_rules_shipping_v6.2.zip differ diff --git a/rules_shipping.xml b/rules_shipping.xml index 433c537ab35f8e5ab6f9b2f388060e56fa6c2030..90eb54301ebfa235d624a422e7db14d719b9f8d0 100644 --- a/rules_shipping.xml +++ b/rules_shipping.xml @@ -6,7 +6,7 @@ <authorUrl>http://www.open-tools.net</authorUrl> <copyright>Copyright (C) 2013-2014, Reinhold Kainhofer</copyright> <license>GPL v3+</license> - <version>6.1.7</version> + <version>6.2</version> <description>OTSHIPMENT_RULES_DESC</description> <files> <filename plugin="rules_shipping">rules_shipping.php</filename> diff --git a/rules_shipping_advanced.xml b/rules_shipping_advanced.xml index 1af779b22664c4a5d7990c5a864ef7d0f5f9c658..98fe4c5190e838607537923511682ff600d131ba 100644 --- a/rules_shipping_advanced.xml +++ b/rules_shipping_advanced.xml @@ -6,7 +6,7 @@ <authorUrl>http://www.open-tools.net</authorUrl> <copyright>Copyright (C) 2013-2014, Reinhold Kainhofer</copyright> <license>GPL v3+</license> - <version>6.1.7</version> + <version>6.2</version> <description>OTSHIPMENT_RULES_ADV_DESC</description> <files> <filename plugin="rules_shipping_advanced">rules_shipping_advanced.php</filename> diff --git a/rules_shipping_framework_joomla.php b/rules_shipping_framework_joomla.php index a050e7e0fbede80d658e2a020c31e72749639e87..cea80d4f3bfba5a9638a354a76dbde8d5a602423 100644 --- a/rules_shipping_framework_joomla.php +++ b/rules_shipping_framework_joomla.php @@ -44,24 +44,20 @@ class RulesShippingFrameworkJoomla extends RulesShippingFramework { return $custfuncdefs; } - protected function printWarning($message) { - // Keep track of warning messages, so we don't print them twice: - global $printed_warnings; - if (!isset($printed_warnings)) - $printed_warnings = array(); - if (!in_array($message, $printed_warnings)) { - JFactory::getApplication()->enqueueMessage($message, 'error'); - $printed_warnings[] = $message; + protected function printMessage($message, $type) { + // Keep track of messages, so we don't print them twice: + global $printed_messages; + if (!isset($printed_messages)) + $printed_messages = array(); + if (!in_array($message, $printed_messages)) { + if ($type=='debug') { + vmDebug($message); + } else { + JFactory::getApplication()->enqueueMessage($message, $type); + } + $printed_messages[] = $message; } } - /** @tag public-api - * @function debug() - * Print a debug message (untranslated) in the system-specific way. - * @param $message the debug message to be printed - */ - public function debug($message) { - vmDebug($message); - } public function __($string) { $args = func_get_args();