Beispiel #1
0
/**
 * Validate rule
 *
 * @param int $promotion_id promotion ID
 * @param array $promotion rule data
 * @param array $data data array
 * @param array $auth auth array (for cart rules)
 * @param array $cart_products cart products array (for cart rules)
 * @return bool true if rule can be applied, false - otherwise
 */
function fn_promotion_validate($promotion_id, $promotion, &$data, &$auth, &$cart_products)
{
    $schema = fn_promotion_get_schema('conditions');
    $stop_validating = false;
    $result = true;
    static $parent_orders = array();
    fn_set_hook('pre_promotion_validate', $promotion_id, $promotion, $data, $stop_validating, $result, $auth, $cart_products);
    if ($stop_validating) {
        return $result;
    }
    if (empty($promotion['condition'])) {
        // if promotion is unconditional, apply it
        return true;
    }
    $promotion['value'] = !isset($promotion['value']) ? '' : $promotion['value'];
    if (!empty($schema[$promotion['condition']])) {
        $value = '';
        $parent_order_value = '';
        if (!empty($data['parent_order_id']) && empty($parent_orders[$data['parent_order_id']])) {
            $parent_orders[$data['parent_order_id']] = array('cart' => array(), 'cart_products' => array(), 'product_groups' => array());
            fn_form_cart($data['parent_order_id'], $parent_orders[$data['parent_order_id']]['cart'], $auth);
            list($parent_orders[$data['parent_order_id']]['cart_products'], $parent_orders[$data['parent_order_id']]['product_groups']) = fn_calculate_cart_content($parent_orders[$data['parent_order_id']]['cart'], $auth);
        }
        // Ordinary field
        if (!empty($schema[$promotion['condition']]['field'])) {
            // Array definition, parse it
            if (strpos($schema[$promotion['condition']]['field'], '@') === 0) {
                $value = fn_promotion_get_object_value($schema[$promotion['condition']]['field'], $data, $auth, $cart_products);
            } else {
                // If field can be used in both zones, it means that we're using products
                if (in_array('catalog', $schema[$promotion['condition']]['zones']) && in_array('cart', $schema[$promotion['condition']]['zones']) && !empty($cart_products)) {
                    // this is the "cart" zone. FIXME!!!
                    foreach ($cart_products as $v) {
                        if ($promotion['operator'] == 'nin') {
                            if (fn_promotion_validate_attribute($v[$schema[$promotion['condition']]['field']], $promotion['value'], 'in')) {
                                return false;
                            }
                        } else {
                            if (fn_promotion_validate_attribute($v[$schema[$promotion['condition']]['field']], $promotion['value'], $promotion['operator'])) {
                                return true;
                            }
                        }
                    }
                    return $promotion['operator'] == 'nin' ? true : false;
                }
                if (!isset($data[$schema[$promotion['condition']]['field']])) {
                    return false;
                }
                $value = $data[$schema[$promotion['condition']]['field']];
                if (!empty($data['parent_order_id']) && !empty($parent_orders[$data['parent_order_id']]['cart'][$schema[$promotion['condition']]['field']])) {
                    $parent_order_value = $parent_orders[$data['parent_order_id']]['cart'][$schema[$promotion['condition']]['field']];
                }
            }
            // Field is the result of function
        } elseif (!empty($schema[$promotion['condition']]['field_function'])) {
            $function_args = $schema[$promotion['condition']]['field_function'];
            $function_name = array_shift($function_args);
            $function_args_definitions = $function_args;
            // If field can be used in both zones, it means that we're using products
            if (in_array('catalog', $schema[$promotion['condition']]['zones']) && in_array('cart', $schema[$promotion['condition']]['zones']) && !empty($cart_products)) {
                // this is the "cart" zone. FIXME!!!
                foreach ($cart_products as $product) {
                    $function_args = $function_args_definitions;
                    foreach ($function_args as $k => $v) {
                        if (strpos($v, '@') !== false) {
                            $function_args[$k] =& fn_promotion_get_object_value($v, $product, $auth, $cart_products);
                        } elseif ($v == '#this') {
                            $function_args[$k] =& $promotion;
                        } elseif ($v == '#id') {
                            $function_args[$k] =& $promotion_id;
                        }
                    }
                    $value = call_user_func_array($function_name, $function_args);
                    if ($promotion['operator'] == 'nin') {
                        if (fn_promotion_validate_attribute($value, $promotion['value'], 'in')) {
                            return false;
                        }
                    } else {
                        if (fn_promotion_validate_attribute($value, $promotion['value'], $promotion['operator'])) {
                            return true;
                        }
                    }
                }
                return $promotion['operator'] == 'nin' ? true : false;
            }
            foreach ($function_args as $k => $v) {
                if (strpos($v, '@') !== false) {
                    $function_args[$k] =& fn_promotion_get_object_value($v, $data, $auth, $cart_products);
                } elseif ($v == '#this') {
                    $function_args[$k] =& $promotion;
                } elseif ($v == '#id') {
                    $function_args[$k] =& $promotion_id;
                }
            }
            $value = call_user_func_array($function_name, $function_args);
            if (!empty($data['parent_order_id']) && !empty($parent_orders[$data['parent_order_id']])) {
                $parent_p = $function_args_definitions;
                foreach ($parent_p as $k => $v) {
                    if (strpos($v, '@') !== false) {
                        $parent_p[$k] =& fn_promotion_get_object_value($v, $parent_orders[$data['parent_order_id']]['cart'], $auth, $parent_orders[$data['parent_order_id']]['cart_products']);
                    } elseif ($v == '#this') {
                        $parent_p[$k] =& $promotion;
                    } elseif ($v == '#id') {
                        $parent_p[$k] =& $promotion_id;
                    }
                }
                $parent_order_value = call_user_func_array($function_name, $parent_p);
            }
        }
        // Value is validated
        $result = fn_promotion_validate_attribute($value, $promotion['value'], $promotion['operator']);
        if ($parent_order_value) {
            $result = $result || fn_promotion_validate_attribute($parent_order_value, $promotion['value'], $promotion['operator']);
        }
        return $result;
    }
    return false;
}
Beispiel #2
0
/**
 * Validate rule
 *
 * @param int $promotion_id promotion ID
 * @param array $promotion rule data
 * @param array $data data array
 * @param array $auth auth array (for cart rules)
 * @param array $cart_products cart products array (for cart rules)
 * @return bool true if rule can be applied, false - otherwise
 */
function fn_promotion_validate($promotion_id, $promotion, &$data, &$auth, &$cart_products)
{
    $schema = fn_promotion_get_schema('conditions');
    if (empty($promotion['condition'])) {
        // if promotion is unconditional, apply it
        return true;
    }
    $promotion['value'] = !isset($promotion['value']) ? '' : $promotion['value'];
    if (!empty($schema[$promotion['condition']])) {
        $value = '';
        // Ordinary field
        if (!empty($schema[$promotion['condition']]['field'])) {
            // Array definition, parse it
            if (strpos($schema[$promotion['condition']]['field'], '@') === 0) {
                $value = fn_promotion_get_object_value($schema[$promotion['condition']]['field'], $data, $auth, $cart_products);
            } else {
                // If field can be used in both zones, it means that we're using products
                if (in_array('catalog', $schema[$promotion['condition']]['zones']) && in_array('cart', $schema[$promotion['condition']]['zones']) && !empty($cart_products)) {
                    // this is the "cart" zone. FIXME!!!
                    foreach ($cart_products as $v) {
                        if (fn_promotion_validate_attribute($v[$schema[$promotion['condition']]['field']], $promotion['value'], $promotion['operator']) == true) {
                            return true;
                        }
                    }
                    return false;
                }
                if (!isset($data[$schema[$promotion['condition']]['field']])) {
                    return false;
                }
                $value = $data[$schema[$promotion['condition']]['field']];
            }
            // Field is the result of function
        } elseif (!empty($schema[$promotion['condition']]['field_function'])) {
            $p = $schema[$promotion['condition']]['field_function'];
            $func = array_shift($p);
            $p_orig = $p;
            // If field can be used in both zones, it means that we're using products
            if (in_array('catalog', $schema[$promotion['condition']]['zones']) && in_array('cart', $schema[$promotion['condition']]['zones']) && !empty($cart_products)) {
                // this is the "cart" zone. FIXME!!!
                foreach ($cart_products as $product) {
                    $p = $p_orig;
                    foreach ($p as $k => $v) {
                        if (strpos($v, '@') !== false) {
                            $p[$k] =& fn_promotion_get_object_value($v, $product, $auth, $cart_products);
                        } elseif ($v == '#this') {
                            $p[$k] =& $promotion;
                        } elseif ($v == '#id') {
                            $p[$k] =& $promotion_id;
                        }
                    }
                    $value = call_user_func_array($func, $p);
                    if (fn_promotion_validate_attribute($value, $promotion['value'], $promotion['operator'])) {
                        return true;
                    }
                }
                return false;
            }
            foreach ($p as $k => $v) {
                if (strpos($v, '@') !== false) {
                    $p[$k] =& fn_promotion_get_object_value($v, $data, $auth, $cart_products);
                } elseif ($v == '#this') {
                    $p[$k] =& $promotion;
                } elseif ($v == '#id') {
                    $p[$k] =& $promotion_id;
                }
            }
            $value = call_user_func_array($func, $p);
        }
        // Value is validated
        return fn_promotion_validate_attribute($value, $promotion['value'], $promotion['operator']);
    }
    return false;
}