You are here

function userpoints_create_description in User Points 7

Creates a descriptive reason for a userpoints_transaction.

The following resources are considered, in this order:

  • description key in the information array for that operation.
  • description of the transaction.
  • name of the operation.

Parameters

$transaction: The transaction object for which the description shall be generated.

$options: Array of options:

  • link: If FALSE, no link is generated to the linked entity even if there were one. Defaults to TRUE.
  • truncate: Define if the reason should be truncated. Defaults to TRUE.
  • skip_description: Allows to skip the eventually existing custom description a transaction has and always uses the generated description.

Return value

The reason for that transaction, linked to the referenced entity if available.

4 calls to userpoints_create_description()
userpoints_confirm_approve in ./userpoints.admin.inc
userpoints_get_transaction_row in ./userpoints.module
Returns a single row for a transaction listing.
userpoints_tokens in ./userpoints.module
Implements hook_tokens().
userpoints_view_transaction in ./userpoints.pages.inc
Menu callback; display details about a specific transaction.

File

./userpoints.module, line 1780

Code

function userpoints_create_description($transaction, array $options = array()) {

  // Default options.
  $options += array(
    'link' => TRUE,
    'truncate' => TRUE,
  );

  // Check if there is a valid entity referenced and which can be linked to.
  $entity = NULL;
  if ($transaction->entity_type && entity_get_info($transaction->entity_type)) {
    $entity = entity_load($transaction->entity_type, array(
      $transaction->entity_id,
    ));
    $entity = reset($entity);
  }
  $safe = FALSE;

  // Check transaction description first to allow custom overrides.
  if (!empty($transaction->description) && empty($options['skip_description'])) {
    $description = $transaction->description;
  }
  else {
    $info = userpoints_get_info($transaction->operation);

    // Check if there is a valid description callback defined for this
    // operation.
    if (!empty($info['description callback']) && function_exists($info['description callback'])) {
      $description = $info['description callback']($transaction, $entity);
      $safe = TRUE;
    }
    elseif (!empty($info['description'])) {
      $description = $info['description'];
      $safe = TRUE;
    }
  }

  // Fallback to the operation name if there is no source.
  if (empty($description)) {
    $description = $transaction->operation;
  }

  // Truncate description.
  $attributes = array();
  $stripped_description = strip_tags($description);
  if ($options['truncate'] && drupal_strlen($stripped_description) > variable_get('userpoints_truncate', 30) + 3) {

    // The title attribute will be check_plain()'d again drupal_attributes(),
    // avoid double escaping.
    $attributes['title'] = html_entity_decode($stripped_description, ENT_QUOTES);
    $description = truncate_utf8($stripped_description, variable_get('userpoints_truncate', 30), FALSE, TRUE);
  }

  // Link to the referenced entity, if available.
  if ($entity && $options['link']) {
    $uri = entity_uri($transaction->entity_type, $entity);
    if (isset($uri['path'])) {
      $description = l($description, $uri['path'], $uri['options'] + array(
        'html' => $safe,
        'attributes' => $attributes,
      ));
    }
  }
  if ((empty($entity) || empty($uri)) && !$safe) {

    // Escape possible user provided reason.
    $description = check_plain($description);
  }
  return $description;
}