You are here

function commerce_order_rules_compare_address in Commerce Core 7

Condition callback: compares an address component against the given value.

1 string reference to 'commerce_order_rules_compare_address'
commerce_order_rules_condition_info in modules/order/commerce_order.rules.inc
Implements hook_rules_condition_info().

File

modules/order/commerce_order.rules.inc, line 231
Rules integration for orders.

Code

function commerce_order_rules_compare_address($order, $address_field, $component, $operator, $value) {
  list($field_name, $address_field_name) = explode('|', $address_field);

  // If we actually received a valid order...
  if (!empty($order)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);

    // And if we can actually find the requested address data...
    if (!empty($wrapper->{$field_name}) && !empty($wrapper->{$field_name}->{$address_field_name})) {
      $address = $wrapper->{$field_name}->{$address_field_name}
        ->value();

      // Perform the comparison in upper case.
      $address_component = drupal_strtoupper($address[$component]);
      $value = drupal_strtoupper($value);

      // Make a comparison based on the operator.
      switch ($operator) {
        case 'equals':
          return $address_component == $value;
        case 'begins with':
          return strpos($address_component, $value) === 0;
        case 'contains':
          return strpos($address_component, $value) !== FALSE;
        case 'is one of':
          $list = preg_split('/[\\n\\r]+/', $value);
          return array_search($address_component, $list) !== FALSE;
        case 'begins with one of':
          $list = preg_split('/[\\n\\r]+/', $value);
          foreach ($list as $item) {
            if (strpos($address_component, $item) === 0) {
              return TRUE;
            }
          }
          break;
      }
    }
  }
  return FALSE;
}