You are here

function UserpointsTransaction::getReason in User Points 7.2

Returns a descriptive reason for this 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

$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 use the generated description.

Return value

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

1 call to UserpointsTransaction::getReason()
UserpointsTransaction::getTableRow in ./userpoints.transaction.inc
Returns a single row for a transaction listing.

File

./userpoints.transaction.inc, line 1001
Contains the UserpointsTransaction and related classes.

Class

UserpointsTransaction
A Userpoints transaction.

Code

function getReason(array $options = array()) {

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

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

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

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

  // Truncate description.
  $attributes = array();
  $stripped_reason = strip_tags($reason);
  if ($options['truncate'] && drupal_strlen($stripped_reason) > 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_reason, ENT_QUOTES);
    $reason = truncate_utf8($stripped_reason, variable_get('userpoints_truncate', 30), FALSE, TRUE);
  }

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

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