Skip to content
Snippets Groups Projects
Commit 3fb163d4 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

V6.2: Add error/warning/notice/message functions to display messages to the user

Fixes #30
Fixes #31
parent 6bd93af3
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ BASE=rules_shipping ...@@ -2,7 +2,7 @@ BASE=rules_shipping
BASE_ADV=rules_shipping_advanced BASE_ADV=rules_shipping_advanced
PLUGINTYPE=vmshipment PLUGINTYPE=vmshipment
ZIPBASE=opentools_vm 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=$(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 PLUGINFILES_ADV=$(BASE_ADV).php $(BASE)_base.php $(BASE)_framework_joomla.php $(BASE_ADV).script.php $(BASE_ADV).xml index.html
......
images/plg_vmshipping_rules_shipping_messages.png

31.1 KiB

images/plg_vmshipping_rules_shipping_plugins_icon.png

47.5 KiB

...@@ -171,13 +171,33 @@ class RulesShippingFramework { ...@@ -171,13 +171,33 @@ class RulesShippingFramework {
return $this->custom_functions; return $this->custom_functions;
} }
/** @tag system-specific /** @tag public-api
* @function printWarning() * @tag system-specific
* Print a warning in the system-specific way. * @function message()
* @param $message the warning message to be printed (already properly translated) * 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) { public function message($message, $type) {
echo($message); $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 /** @tag public-api
...@@ -189,16 +209,44 @@ class RulesShippingFramework { ...@@ -189,16 +209,44 @@ class RulesShippingFramework {
*/ */
public function warning($message) { public function warning($message) {
$args = func_get_args(); $args = func_get_args();
$msg = call_user_func_array(array($this, "__"), $args); array_splice($args, 1, 0, 'warning'); // insert msg type in second position
$this->printWarning($msg); 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 public-api
* @tag system-specific
* @function debug() * @function debug()
* Print a debug message (untranslated) in the system-specific way. * Print a debug message in the system-specific way.
* @param $message the debug message to be printed * @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) { 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 /** @tag public-api
...@@ -422,6 +470,12 @@ class RulesShippingFramework { ...@@ -422,6 +470,12 @@ class RulesShippingFramework {
$this->warning('OTSHIPMENT_RULES_UNKNOWN_TYPE', $r->getType(), $r->rulestring); $this->warning('OTSHIPMENT_RULES_UNKNOWN_TYPE', $r->getType(), $r->rulestring);
break; 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"])) { if (!is_null($result["rule"])) {
$this->match[$id] = $result; $this->match[$id] = $result;
...@@ -599,6 +653,7 @@ class ShippingRule { ...@@ -599,6 +653,7 @@ class ShippingRule {
var $countries = array(); var $countries = array();
var $ruleinfo = 0; var $ruleinfo = 0;
var $includes_tax = 0; var $includes_tax = 0;
var $messages = array('error' => array(), 'warning' => array(), 'notice' => array(), 'debug' => array());
function __construct ($framework, $rule, $countries, $ruleinfo) { function __construct ($framework, $rule, $countries, $ruleinfo) {
$this->framework = $framework; $this->framework = $framework;
...@@ -630,6 +685,11 @@ class ShippingRule { ...@@ -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 '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 '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 '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; case 'condition': $this->conditions[] = $value; break;
default: $this->framework->warning('OTSHIPMENT_RULES_UNKNOWN_VARIABLE', $var, $rulepart); default: $this->framework->warning('OTSHIPMENT_RULES_UNKNOWN_VARIABLE', $var, $rulepart);
} }
...@@ -652,9 +712,8 @@ class ShippingRule { ...@@ -652,9 +712,8 @@ class ShippingRule {
$rulepart = trim($rulepart); $rulepart = trim($rulepart);
if (!isset($rulepart) || $rulepart==='') return; if (!isset($rulepart) || $rulepart==='') return;
// Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression! // 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); $this->handleAssignment ($matches[1], $matches[3], $rulepart);
return; return;
} }
...@@ -1033,9 +1092,25 @@ class ShippingRule { ...@@ -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); 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) { protected function evaluateRule (&$vals, $products, $cartvals_callback) {
if ($this->evaluated) if ($this->evaluated)
...@@ -1059,21 +1134,15 @@ class ShippingRule { ...@@ -1059,21 +1134,15 @@ class ShippingRule {
} }
// All conditions match // All conditions match
$this->match = True; $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) // Calculate the value (i.e. shipping cost or modifier)
$this->value = $this->calculateShipping($vals, $products, $cartvals_callback); $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) { $this->rulename = $this->stringReplaceVariables($this->framework->__($this->name), $vals);
$val = $this->evaluateVariable($m, $vals);
if ($val !== null) {
$name = str_replace("{".$m."}", $val, $name);
}
}
$this->rulename = $name;
} }
function matches(&$vals, $products, $cartvals_callback) { function matches(&$vals, $products, $cartvals_callback) {
...@@ -1158,7 +1227,7 @@ class ShippingRule_Advanced extends ShippingRule { ...@@ -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! // 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); $this->handleAssignment ($matches[1], $matches[3], $rulepart);
return; return;
} }
......
File added
File added
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<authorUrl>http://www.open-tools.net</authorUrl> <authorUrl>http://www.open-tools.net</authorUrl>
<copyright>Copyright (C) 2013-2014, Reinhold Kainhofer</copyright> <copyright>Copyright (C) 2013-2014, Reinhold Kainhofer</copyright>
<license>GPL v3+</license> <license>GPL v3+</license>
<version>6.1.7</version> <version>6.2</version>
<description>OTSHIPMENT_RULES_DESC</description> <description>OTSHIPMENT_RULES_DESC</description>
<files> <files>
<filename plugin="rules_shipping">rules_shipping.php</filename> <filename plugin="rules_shipping">rules_shipping.php</filename>
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<authorUrl>http://www.open-tools.net</authorUrl> <authorUrl>http://www.open-tools.net</authorUrl>
<copyright>Copyright (C) 2013-2014, Reinhold Kainhofer</copyright> <copyright>Copyright (C) 2013-2014, Reinhold Kainhofer</copyright>
<license>GPL v3+</license> <license>GPL v3+</license>
<version>6.1.7</version> <version>6.2</version>
<description>OTSHIPMENT_RULES_ADV_DESC</description> <description>OTSHIPMENT_RULES_ADV_DESC</description>
<files> <files>
<filename plugin="rules_shipping_advanced">rules_shipping_advanced.php</filename> <filename plugin="rules_shipping_advanced">rules_shipping_advanced.php</filename>
......
...@@ -44,24 +44,20 @@ class RulesShippingFrameworkJoomla extends RulesShippingFramework { ...@@ -44,24 +44,20 @@ class RulesShippingFrameworkJoomla extends RulesShippingFramework {
return $custfuncdefs; return $custfuncdefs;
} }
protected function printWarning($message) { protected function printMessage($message, $type) {
// Keep track of warning messages, so we don't print them twice: // Keep track of messages, so we don't print them twice:
global $printed_warnings; global $printed_messages;
if (!isset($printed_warnings)) if (!isset($printed_messages))
$printed_warnings = array(); $printed_messages = array();
if (!in_array($message, $printed_warnings)) { if (!in_array($message, $printed_messages)) {
JFactory::getApplication()->enqueueMessage($message, 'error'); if ($type=='debug') {
$printed_warnings[] = $message; 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) { public function __($string) {
$args = func_get_args(); $args = func_get_args();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment