You are here

function _uc_price_get_handlers in Ubercart 6.2

Return an array of price handler data.

Parameters

$options: An associative array of options used when building the array with keys:

  • 'rebuild_handlers' => TRUE or FALSE indicating whether we should nuke the cache and rebuild the handler data.
  • 'all_handlers' => TRUE or FALSE indicating whether or not to return all defined price handlers' alterers or just enabled ones.

Return value

A structured array of price handler data.

2 calls to _uc_price_get_handlers()
uc_price in uc_store/includes/uc_price.inc
The heart of the price modification/displaying system, this function handles price alteration, formatting, theming, and caching.
uc_price_settings_form in uc_store/uc_store.admin.inc
Form for enabling/weighting price alterers and selecting a price formatter.

File

uc_store/includes/uc_price.inc, line 162
Price handling functions and hooks.

Code

function _uc_price_get_handlers($options = array()) {
  static $handlers = array();

  // Set default options.
  $options += array(
    'rebuild_handlers' => FALSE,
    'all_handlers' => FALSE,
  );

  // Get handlers only if we haven't already, or if this is a rebuild.
  if (empty($handlers) || $options['rebuild_handlers']) {

    // Get the handlers and sort them by weight.
    $config = variable_get('uc_price_handler_config', array());
    foreach (module_implements('uc_price_handler') as $module) {

      // Create a price handler hook data array and merge in sensible defaults.
      $hooks[$module] = module_invoke($module, 'uc_price_handler') + array(
        'weight' => 0,
        'enabled' => TRUE,
      );

      // Merge any configuration state in.
      if (isset($config[$module])) {
        $hooks[$module] = $config[$module] + $hooks[$module];
      }

      // Unset disabled hooks if we're not building the selection form.
      if (!$options['all_handlers'] && !$hooks[$module]['enabled']) {
        unset($hooks[$module]);
      }
    }

    // Sort the hook data by weight.
    uasort($hooks, 'uc_weight_sort');

    // Store the raw data for selection form building.
    $handlers['hook_data'] = $hooks;

    // Store the selected formatter, defaulting to uc_store's implementation.
    $formatter = variable_get('uc_price_format_callback', 'uc_store_price_handler_format');
    if (function_exists($formatter)) {
      $handlers['formatter'] = $formatter;
    }
    else {
      $handlers['formatter'] = 'uc_store_price_handler_format';
    }

    // Grab all the alter/format callbacks, as well as merging the options.
    // This happens in order by weight, so we're kosher.
    $handlers['alterers'] = array();

    // We set some default options here. We could set them in the uc_store price handler,
    // but that means if that handler is disabled, we won't get them merged in.
    $handlers['options'] = array(
      'sign' => variable_get('uc_currency_sign', '$'),
      'sign_after' => variable_get('uc_sign_after_amount', FALSE),
      'prec' => variable_get('uc_currency_prec', 2),
      'dec' => variable_get('uc_currency_dec', '.'),
      'thou' => variable_get('uc_currency_thou', ','),
      'label' => TRUE,
    );
    foreach ($hooks as $hook) {
      if (isset($hook['alter']['callback']) && function_exists($hook['alter']['callback'])) {
        $handlers['alterers'][] = $hook['alter']['callback'];
      }
      if (isset($hook['format']['callback']) && function_exists($hook['format']['callback'])) {
        $handlers['formatters'][] = $hook['format']['callback'];
      }
      if (isset($hook['options']) && is_array($hook['options'])) {
        $handlers['options'] = $hook['options'] + $handlers['options'];
      }
    }
  }
  return $handlers;
}