You are here

function _ca_evaluate_condition in Ubercart 6.2

Evaluate a single condition.

1 call to _ca_evaluate_condition()
_ca_evaluate_conditions_tree in ca/ca.module
Recursively evaluate conditions to accommodate nested logical groups.

File

ca/ca.module, line 398
This is a demonstration module for the new conditional actions API.

Code

function _ca_evaluate_condition($condition, $arguments, $condition_data) {
  $args = array();

  // Make sure the condition tree is sane.
  if (!is_array($condition_data[$condition['#name']])) {
    return NULL;
  }

  // Get the callback function for the current condition.
  $callback = $condition_data[$condition['#name']]['#callback'];

  // Skip this condition if the function does not exist.
  if (!function_exists($callback)) {
    return NULL;
  }

  // Loop through the expected arguments for a condition.
  // Cast to an array to accommodate conditions that need no arguments.
  foreach ((array) $condition_data[$condition['#name']]['#arguments'] as $key => $value) {

    // Using the argument map for the condition on this predicate, fetch the
    // argument that was passed to the trigger that matches to the argument
    // needed to evaluate this condition.
    if (isset($condition['#argument_map'][$key])) {
      if ($value['#entity'] == 'arguments') {
        $args[] = $arguments;
      }
      else {
        $args[] = $arguments[$condition['#argument_map'][$key]]['#data'];
      }
    }
    else {

      // Skip this condition of the predicate didn't map the arguments needed.
      return NULL;
    }
  }

  // Add the condition settings to the argument list.
  $args[] = $condition['#settings'];

  // Call the condition's function with the appropriate arguments.
  $result = call_user_func_array($callback, $args);

  // If the negate operator is TRUE, then switch the result!
  if (isset($condition['#settings']['negate']) && $condition['#settings']['negate']) {
    $result = !$result;
  }
  return $result;
}