You are here

function uc_addresses_tokens in Ubercart Addresses 7

Implements hook_tokens().

File

./uc_addresses.tokens.inc, line 75
Hook implementations for token module.

Code

function uc_addresses_tokens($type, $tokens, $data = array(), $options = array()) {
  $replacements = array();
  switch ($type) {
    case 'uc_addresses':
      $address = $data['uc_addresses'];
      if ($address instanceof UcAddressesAddress) {
        foreach ($tokens as $name => $original) {
          $handler = NULL;
          $fieldname = $name;
          $output_format = NULL;

          // Check if this token represents a existing address field.
          if (UcAddressesSchemaAddress::fieldExists($fieldname)) {
            $handler = uc_addresses_get_address_field_handler($address, $fieldname, 'token');
          }
          else {

            // Check if this token represents a particular output format of an existing address field.
            $token_pieces = explode(':', $name);
            if (count($token_pieces) >= 2) {
              $fieldname = $token_pieces[0];
              array_shift($token_pieces);
              $output_format = implode(':', $token_pieces);
              if (UcAddressesSchemaAddress::fieldExists($fieldname)) {

                // Check if this field supports the specified output format.
                $handler = uc_addresses_get_address_field_handler($address, $fieldname, 'token');
              }
            }
          }
          if ($handler instanceof UcAddressesFieldHandler) {

            // Use the handler to output the right value.
            $replacements[$original] = $handler
              ->outputValue($address
              ->getField($fieldname), $output_format);
          }
        }
      }
      break;
    case 'uc_order':
      if (empty($data['uc_order'])) {

        // No order object, can happen. RulesTokenEvaluator sometimes sends empty data.
        // Found this out after a long debug session in the automated tests.
        return array();
      }
      $order = $data['uc_order'];
      $order_tokens = array(
        'billing' => 'uc-addresses-billing-address',
        'shipping' => 'uc-addresses-shipping-address',
      );
      foreach ($order_tokens as $address_type => $token) {
        $address_tokens = token_find_with_prefix($tokens, $token);
        if (!isset($tokens[$token]) && !$address_tokens) {
          continue;
        }
        if (isset($order->uc_addresses[$address_type]) && $order->uc_addresses[$address_type] instanceof UcAddressesAddress) {
          $address = $order->uc_addresses[$address_type];
          if (isset($tokens[$token])) {
            $replacements[$tokens[$token]] = uc_addresses_format_address($address, 'token');
          }
          if ($address_tokens) {
            $replacements += token_generate('uc_addresses', $address_tokens, array(
              'uc_addresses' => $address,
            ), $options);
          }
        }
      }
      break;
    case 'user':
      $user = $data['user'];
      $user_tokens = array(
        'billing' => 'default-billing-address',
        'shipping' => 'default-shipping-address',
      );
      foreach ($user_tokens as $address_type => $token) {
        $address_tokens = token_find_with_prefix($tokens, $token);
        if (!isset($tokens[$token]) && !$address_tokens) {
          continue;
        }
        $address = UcAddressesAddressBook::get($user)
          ->getDefaultAddress($address_type);
        if (!$address) {
          continue;
        }
        if (isset($tokens[$token])) {
          $replacements[$tokens[$token]] = uc_addresses_format_address($address, 'token');
        }
        if ($address_tokens) {
          $replacements += token_generate('uc_addresses', $address_tokens, array(
            'uc_addresses' => $address,
          ), $options);
        }
      }
      break;
  }
  return $replacements;
}