You are here

function uc_addresses_token_values in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 6 uc_addresses.module \uc_addresses_token_values()

Implementation of hook_token_values().

File

./uc_addresses.module, line 1083
Adds user profile address support to Ubercart.

Code

function uc_addresses_token_values($type, $object = NULL) {
  $values = array();
  switch ($type) {
    case 'uc_addresses':
      if ($object instanceof UcAddressesAddress) {
        $address = $object;
        $fields = uc_addresses_get_address_field_handler_instances($address, 'token');
        foreach ($fields as $fieldname => $handler) {
          $value = $address
            ->getField($fieldname);

          // Check if handler supports multiple output formats.
          $formats = $handler
            ->getOutputFormats();
          if (count($formats) > 0) {
            foreach ($formats as $format => $label) {
              if ($value != '') {
                $values['uc_addresses_' . $format] = $handler
                  ->outputValue($value, $format);
              }
              else {
                $values['uc_addresses_' . $format] = '';
              }
            }
          }
          else {

            // Only a single format is supported.
            if ($value != '') {
              $values['uc_addresses_' . $fieldname] = $handler
                ->outputValue($value);
            }
            else {
              $values['uc_addresses_' . $fieldname] = '';
            }
          }
          $values['uc_addresses_' . $fieldname . '-raw'] = $value;
        }
      }
      break;
    case 'order':
      $address_types = array(
        'shipping',
        'billing',
      );
      foreach ($address_types as $address_type) {
        if (isset($object->uc_addresses[$address_type]) && $object->uc_addresses[$address_type] instanceof UcAddressesAddress) {
          $address = $object->uc_addresses[$address_type];
          $values['uc-addresses-' . $address_type . '-address'] = uc_addresses_format_address($address, 'token');
          $address_tokens = uc_addresses_token_values('uc_addresses', $address);
          foreach ($address_tokens as $token => $value) {
            if (strpos($token, 'uc_addresses_default_') === FALSE && strpos($token, 'uc_addresses_aid') === FALSE && strpos($token, 'uc_addresses_uid') === FALSE && strpos($token, 'uc_addresses_address_name') === FALSE && strpos($token, 'uc_addresses_created') === FALSE && strpos($token, 'uc_addresses_modified') === FALSE) {
              $order_token = str_replace('uc_addresses_', 'order-uc_addresses-' . $address_type . '-', $token);
              $values[$order_token] = $value;
            }
          }
        }
      }
      break;
    case 'user':
      $default_types = array(
        'shipping',
        'billing',
      );
      foreach ($default_types as $default_type) {
        if ($address = UcAddressesAddressBook::get($object->uid)
          ->getDefaultAddress($default_type)) {
          $address_tokens = uc_addresses_token_values('uc_addresses', $address);
          foreach ($address_tokens as $token => $value) {
            if (strpos($token, 'uc_addresses_default_') === FALSE) {
              $user_token = str_replace('uc_addresses', 'uc_addresses-' . $default_type, $token);
              $values[$user_token] = $value;
            }
          }

          // Address display.
          $values['uc-addresses-' . $default_type . '-address'] = uc_addresses_format_address($address, 'token');
        }
      }
      break;
  }
  return $values;
}