Commit 1078bf72 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Version 4.0: Fix globals warning; Add Values_Debug variable; Add...

Version 4.0: Fix globals warning; Add Values_Debug variable; Add contains_any/contains_all functions
parent 3cc285f7
......@@ -2,7 +2,7 @@ BASE=rules_shipping
BASE_ADV=rules_shipping_advanced
PLUGINTYPE=vmshipment
ZIPBASE=opentools_vm2
VERSION=3.97
VERSION=4.0
PLUGINFILES=$(BASE).php $(BASE)_base.php $(BASE).script.php $(BASE).xml index.html
PLUGINFILES_ADV=$(BASE_ADV).php $(BASE)_base.php $(BASE_ADV).script.php $(BASE_ADV).xml index.html
......
......@@ -44,4 +44,10 @@ VMSHIPMENT_RULES_EVALUATE_SYNTAXERROR="Syntax error during evaluation, RPN is no
VMSHIPMENT_RULES_EVALUATE_UNKNOWN_OPERATOR="Unknown operator '%s' encountered during evaluation of rule '%s'."
VMSHIPMENT_RULES_EVALUATE_UNKNOWN_ERROR="Unknown error occurred during evaluation of rule '%s'."
VMSHIPMENT_RULES_EVALUATE_ASSIGNMENT_TOPLEVEL="Assignments are not allows inside expressions (rule given was '%s')"
VMSHIPMENT_RULES_EVALUATE_UNKNOWN_VALUE="Evaluation yields unknown value while evaluating rule part '%s'."
\ No newline at end of file
VMSHIPMENT_RULES_EVALUATE_UNKNOWN_VALUE="Evaluation yields unknown value while evaluating rule part '%s'."
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_ARGS="List function '%s' requires all arguments to be lists. (Full rule: '%s')"
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_CONTAIN_ARGS="List function '%s' requires the first argument to be lists. (Full rule: '%s')"
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN="Unknown list function '%s' encountered. (Full rule: '%s')"
VMSHIPMENT_RULES_NOSHIPPING_MESSAGE="%s"
......@@ -55,6 +55,7 @@ VMSHIPMENT_RULES_EVALUATE_ASSIGNMENT_TOPLEVEL="Assignments are not allows inside
VMSHIPMENT_RULES_EVALUATE_UNKNOWN_VALUE="Evaluation yields unknown value while evaluating rule part '%s'."
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_ARGS="List function '%s' requires all arguments to be lists. (Full rule: '%s')"
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_CONTAIN_ARGS="List function '%s' requires the first argument to be lists. (Full rule: '%s')"
VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN="Unknown list function '%s' encountered. (Full rule: '%s')"
VMSHIPMENT_RULES_NOSHIPPING_MESSAGE="%s"
......@@ -6,7 +6,7 @@
<authorUrl>http://www.open-tools.net</authorUrl>
<copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright>
<license>GPL v3+</license>
<version>3.97</version>
<version>4.0</version>
<description>VMSHIPMENT_RULES_DESC</description>
<files>
<filename plugin="rules_shipping">rules_shipping.php</filename>
......
......@@ -6,7 +6,7 @@
<authorUrl>http://www.open-tools.net</authorUrl>
<copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright>
<license>GPL v3+</license>
<version>3.97</version>
<version>4.0</version>
<description>VMSHIPMENT_RULES_ADV_DESC</description>
<files>
<filename plugin="rules_shipping_advanced">rules_shipping_advanced.php</filename>
......
......@@ -29,8 +29,6 @@ if (class_exists ('plgVmShipmentRules_Shipping_Base')) {
return;
}
// Keep track of warning messages, so we don't print them twice:
$printed_warnings = array();
function is_equal($a, $b) {
if (is_array($a) && is_array($b)) {
......@@ -70,12 +68,14 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
}
public 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;
}
}
/**
......@@ -619,6 +619,9 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin {
$cartvals = array_merge ($cartvals, $this->getOrderDimensions ($cart, $method->length_unit));
// Let child classes update the $cartvals array, or add new variables
$this->addCustomCartValues($cart, $cart_prices, $cartvals);
// Add the whole list of cart value to the values, so we can print them out as a debug statement!
$cartvals['values_debug'] = print_r($cartvals,1);
$cartvals['values'] = $cartvals;
// JFactory::getApplication()->enqueueMessage("<pre>cart values: ".print_r($cartvals,1)."</pre>", 'error');
return $cartvals;
}
......@@ -880,6 +883,34 @@ class ShippingRule {
}
}
function evaluateListContainmentFunction ($function, $args) {
# First make sure that the first argument is a list:
if (!is_array($args[0])) {
JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_CONTAIN_ARGS', $function, $this->rulestring), 'error');
return false;
}
// Extract the array from the args, the $args varialbe will now only contain the elements to be checked:
$array = array_shift($args);
switch ($function) {
case "contains_any":
foreach ($args as $a) {
if (in_array($a, $array))
return true;
}
return false;
case "contains_all":
foreach ($args as $a) {
if (!in_array($a, $array))
return false;
}
return true;
default:
JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN', $function, $this->rulestring), 'error');
return false;
}
}
function evaluateFunction ($function, $args) {
$func = strtolower($function);
// Functions with no argument:
......@@ -920,16 +951,28 @@ class ShippingRule {
case "substring": return substr($args[0], $args[1]-1, $args[2]); break;
}
}
// List functions
if (in_array($func, array("length", "complement", "issubset", "contains", "union", "join", "intersection", "list_equal"))) {
return $this->evaluateListFunction ($func, $args);
}
// Functions with variable number of args
switch ($func) {
case "max": return max($args); break;
case "min": return min($args); break;
case "max":
return max($args); break;
case "min":
return min($args); break;
case "list":
case "array": return $args; break;
case "array":
return $args; break;
// List functions:
case "length":
case "complement":
case "issubset":
case "contains":
case "union":
case "join":
case "intersection":
case "list_equal":
return $this->evaluateListFunction ($func, $args); break;
case "contains_any":
case "contains_all":
return $this->evaluateListContainmentFunction($func, $args); break;
}
// No known function matches => print an error, return 0
JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_UNKNOWN_FUNCTION', $function, $this->rulestring), 'error');
......
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