You are here

function fillpdf_token_tokens in FillPDF 7

Same name and namespace in other branches
  1. 7.2 modules/fillpdf_token/fillpdf_token.module \fillpdf_token_tokens()

Implements hook_tokens().

File

modules/fillpdf_token/fillpdf_token.module, line 96
Provides additional tokens for use in FillPDF mappings.

Code

function fillpdf_token_tokens($type, $tokens, $data = array(), $options = array()) {

  // If Ubercart is handling these tokens, our function won't provide anything.
  static $fillpdf_token_info;
  $replacements = array();
  if (!isset($fillpdf_token_info)) {
    $fillpdf_token_info = fillpdf_token_token_info();
  }
  if (module_exists('uc_order') && isset($fillpdf_token_info['types']) && !empty($fillpdf_token_info['types'])) {
    $language_code = NULL;
    if (isset($options['language'])) {
      $language_code = $options['language']->language;
    }
    $sanitize = !empty($options['sanitize']);
    if ($type == 'uc_order_product' && !empty($data['uc_order_product'])) {
      $order_product = $data['uc_order_product'];
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'order-product-id':
            $replacements[$original] = $order_product->order_product_id;
            break;
          case 'title':
            $replacements[$original] = $sanitize ? check_plain($order_product->title) : $order_product->title;
            break;
          case 'model':
            $replacements[$original] = $sanitize ? check_plain($order_product->model) : $order_product->model;
            break;
          case 'qty':
            $replacements[$original] = $order_product->qty;
            break;
          case 'cost':
            $replacements[$original] = uc_currency_format($order_product->cost);
            break;
          case 'cost-value':
            $replacements[$original] = $order_product->cost;
            break;
          case 'price':
            $replacements[$original] = uc_currency_format($order_product->price);
            break;
          case 'price-value':
            $replacements[$original] = $order_product->price;
            break;
          case 'weight':
            $replacements[$original] = $order_product->weight;
            break;
          case 'weight-units':
            $replacements[$original] = $order_product->weight_units;
            break;
        }

        // Skip this check if replacement already found.
        if (!isset($replacements[$original]) || empty($replacements[$original])) {
          $token_parts = explode(':', $name);
          if ($token_parts[0] == 'attributes' && !empty($order_product->data['attributes'])) {
            $order_product_attributes = $order_product->data['attributes'];

            // Try to match the attribute name. Replace hyphens with spaces,
            // but if that doesn't match then try it verbatim (though unlikely,
            // there might be an attribute name with actual spaces).
            $without_hyphens = str_replace('-', ' ', $token_parts[1]);

            // Set to unhyphenated version, hyphenated version, or NULL if
            // neither match.
            $attribute_value = isset($order_product_attributes[$without_hyphens]) ? $order_product_attributes[$without_hyphens] : (isset($order_product_attributes[$token_parts[1]]) ? $order_product_attributes[$token_parts[1]] : NULL);
            if ($attribute_value && count($attribute_value)) {
              $replacements[$original] = $attribute_value[0];
            }
          }
        }
      }
      $order_tokens = token_find_with_prefix($tokens, 'order');
      if ($order_tokens) {
        $order = uc_order_load($order_product->order_id);
        $replacements += token_generate('uc_order', $order_tokens, array(
          'uc_order' => $order,
        ), $options);
      }
    }
  }
  return $replacements;
}