From 31403f63a264a067da23ee8f7634b66980a5caaf Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Thu, 13 Nov 2014 19:12:29 +0100
Subject: [PATCH] Work on scoping functions. Not yet finished

---
 rules_shipping_base.php | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/rules_shipping_base.php b/rules_shipping_base.php
index 5811056..819bc9c 100644
--- a/rules_shipping_base.php
+++ b/rules_shipping_base.php
@@ -1113,7 +1113,31 @@ class ShippingRule {
 		}
 	}
 	
-	function evaluateFunction ($function, $args) {
+	/** Evaluate the given expression $expr only for the products that match the filter given by the scoping 
+	 * function and the corresponding conditions */
+	protected function evaluateScoping($expr, $scoping, $conditionvals, $vals) {
+		if (count($conditions)<1)
+			return $this->evaluateTerm($expr, $vals);
+			
+		$filterkeys = array( 
+			"evaluate_for_categories" =>    'categories',
+			"evaluate_for_products" =>      'products',
+			"evaluate_for_vendors" =>       'vendors',
+			"evaluate_for_manufacturers" => 'manufacturers'
+		);
+		
+		$conditions = array();
+		if (isset($filterkeys[$scoping])) 
+			$conditions[$filterkeys[$scoping]] = $conditionsvals;
+
+		// Pass the conditions to the parent plugin class to filter the current list of products:
+		$products = filterProducts($currentproducts, $conditions);
+		$vals = $this->plugin->getCartValues ($cart, $products, /* FIXME: method*/ $this->method, $cart_prices);
+		// FIXME: IMPLEMENT
+		// TODO
+	}
+
+	protected 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!
@@ -1225,6 +1249,13 @@ class ShippingRule {
 			} else {
 				return $this->evaluateVariable($expr, $vals);
 			}
+		} elseif ($is_scoping) {
+			$op = array_shift($expr); // ignore the "FUNCTION"
+			$func = array_shift($expr); // The scoping function name
+			$expression = array_shift($expr); // The expression to be evaluated
+			$conditions = $expr; // the remaining $expr list now contains the conditions
+			return $this->evaluateScoping ($expression, $func, $conditions, $vals);
+			
 		} elseif (is_array($expr)) {
 			// Operator
 			$op = array_shift($expr);
-- 
GitLab