You are here

fillpdf_token.module in FillPDF 7

Same filename and directory in other branches
  1. 7.2 modules/fillpdf_token/fillpdf_token.module

Provides additional tokens for use in FillPDF mappings.

This module is intended to provide token support for token types that we need but aren't available in the module from which the data comes yet. This is a rare need, and currently this module only needs to be enabled for Ubercart Ordered Products tokens.

@todo Deprecate this module when Ubercart core has the functionality.

File

modules/fillpdf_token/fillpdf_token.module
View source
<?php

/**
 * @file
 * Provides additional tokens for use in FillPDF mappings.
 *
 * This module is intended to provide token support for token types that we need
 * but aren't available in the module from which the data comes yet. This is a
 * rare need, and currently this module only needs to be enabled for Ubercart
 * Ordered Products tokens.
 *
 * @todo Deprecate this module when Ubercart core has the functionality.
 */

/**
 * Implements hook_token_info().
 */
function fillpdf_token_token_info() {
  $token_info = $types = $tokens = array();
  if (module_exists('uc_order')) {
    $ubercart_tokens = uc_order_token_info();

    // Don't add our tokens if this functionality has been added to Ubercart,
    // to avoid user confusion.
    if (!isset($ubercart_tokens['types']['uc_order_product'])) {
      $types = array(
        'name' => t('Ordered products'),
        'description' => t('Tokens related to ordered products in Ubercart.'),
        'needs-data' => 'uc_order_product',
      );
      $tokens = array();
      $tokens['order-product-id'] = array(
        'name' => 'Ordered product ID',
        'description' => 'The unique identifier that ties the product, as configured, to its order.',
      );
      $tokens['order'] = array(
        'name' => t('Order'),
        'description' => t('The order to which this ordered product belongs.'),
        'type' => 'uc_order',
      );
      $tokens['title'] = array(
        'name' => t('Title'),
        'description' => t('The title of the ordered product.'),
      );
      $tokens['model'] = array(
        'name' => t('Model/SKU'),
        'description' => t('The model/SKU that was ordered.'),
      );
      $tokens['qty'] = array(
        'name' => t('Quantity'),
        'description' => t('The quantity ordered.'),
      );
      $tokens['cost'] = array(
        'name' => t('Cost'),
        'description' => t('The cost (not price) of this product.'),
      );
      $tokens['price'] = array(
        'name' => t('Price'),
        'description' => t('The price (not cost) of this product.'),
      );
      $tokens['weight'] = array(
        'name' => t('Weight'),
        'description' => t('The product weight without units.'),
      );
      $tokens['weight-units'] = array(
        'name' => t('Weight units'),
        'description' => t('The weight unit (for example, lb) that the numerical weight represents.'),
      );
      $tokens['attributes:?'] = array(
        'name' => t('Ordered attributes'),
        'description' => t('An attribute with which the product was ordered. Replace the ? with the name of your attribute, with hyphens (-) instead of spaces, as displayed on <a href="!attributes_page">the Attributes overview</a>. Example: <em>[uc-order-product:attributes:Favorite-pet-name]</em>. NOTE: Only tested with text fields. Does not support multiple attribute values; only prints the first value.', array(
          '!attributes_page' => url('admin/store/products/attributes'),
        )),
      );
    }
  }
  return array(
    'types' => array(
      'uc_order_product' => $types,
    ),
    'tokens' => array(
      'uc_order_product' => $tokens,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
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;
}

Functions