You are here

function userpoints_tokens in User Points 7

Same name and namespace in other branches
  1. 7.2 userpoints.module \userpoints_tokens()

Implements hook_tokens().

File

./userpoints.module, line 447

Code

function userpoints_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'user' && !empty($data['user'])) {
    $user = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'points':
          $replacements[$original] = userpoints_get_current_points($user->uid);
          break;
        case 'maxpoints':
          $replacements[$original] = userpoints_get_max_points($user->uid);
        default:
          break;
      }
    }
    if ($points_tokens = token_find_with_prefix($tokens, 'points')) {
      $replacements += token_generate('userpoints', $points_tokens, $data, $options);
    }
    if ($points_tokens = token_find_with_prefix($tokens, 'maxpoints')) {
      $replacements += token_generate('maxuserpoints', $points_tokens, $data, $options);
    }
  }
  if ($type == 'userpoints' && !empty($data['user'])) {
    foreach ($tokens as $name => $original) {
      $tid = NULL;
      if ($name == 'all') {
        $tid = 'all';
      }
      elseif (strpos($name, 'category-') === 0) {

        // Extract the category id from the string that looks like category-1.
        list(, $tid) = explode('-', $name);
      }
      if ($tid) {
        $replacements[$original] = userpoints_get_current_points($data['user']->uid, $tid);
      }
    }
  }
  if ($type == 'maxuserpoints' && !empty($data['user'])) {
    $uid = is_object($data['user']->uid) ? $data['user']
      ->getIdentifier() : $data['user']->uid;
    foreach ($tokens as $name => $original) {
      $tid = NULL;
      if ($name == 'all') {
        $tid = 'all';
      }
      elseif (strpos($name, 'category-') === 0) {

        // Extract the category id from the string that looks like category-1.
        list(, $tid) = explode('-', $name);
      }
      if ($tid) {
        $replacements[$original] = userpoints_get_max_points($uid, $tid);
      }
    }
  }
  if ($type == 'userpoints_transaction' && !empty($data['userpoints_transaction'])) {
    $txn = (object) $data['userpoints_transaction'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'points':
          $replacements[$original] = $txn->{$name};
          break;
        case 'points-abs':
          $replacements[$original] = abs($txn->points);
          break;
        case 'display':
          $replacements[$original] = !empty($txn->{$name}) ? t('Yes') : t('No');
          break;
        case 'status':
          $stati = userpoints_txn_status();
          $replacements[$original] = $stati[$txn->{$name}];
          break;
        case 'description':
        case 'reference':
        case 'operation':
          $replacements[$original] = $sanitize ? filter_xss($txn->{$name}) : $txn->{$name};
          break;
        case 'reason':
          $replacements[$original] = userpoints_create_description($txn);
          break;
        case 'entity':
          $entity = !empty($txn->entity_type) && !empty($txn->entity_id) ? entity_load($txn->entity_type, array(
            $txn->entity_id,
          )) : array();
          $entity = reset($entity);
          $label = $entity ? entity_label($txn->entity_type, $entity) : '';
          $replacements[$original] = $sanitize ? filter_xss($label) : $label;
          break;

        // Default values for the chained tokens handled below.
        case 'user':
          $user = user_load($txn->uid);
          $replacements[$original] = $sanitize ? filter_xss(format_username($user)) : format_username($user);
          break;
        case 'time-stamp':
          $replacements[$original] = format_date($txn->time_stamp, 'medium', '', NULL, $language_code);
          break;
        case 'expirydate':
          if ($txn->{$name} > 0) {
            $replacements[$original] = format_date($txn->{$name}, 'medium', '', NULL, $language_code);
          }
          else {
            $replacements[$original] = t('Never');
          }
          break;
        case 'tid':
          if (module_exists('taxonomy') && $txn->tid > 0) {
            $term = taxonomy_term_load($txn->tid);
            $term_name = $term ? $term->name : '';
          }
          else {
            $term_name = t('!Uncategorized', userpoints_translation());
          }
          $replacements[$original] = $sanitize ? filter_xss($term_name) : $term_name;
          break;
      }
    }
    if ($user_tokens = token_find_with_prefix($tokens, 'user')) {
      $replacements += token_generate('user', $user_tokens, array(
        'user' => user_load($txn->uid),
      ), $options);
    }
    if (module_exists('taxonomy') && ($term_tokens = token_find_with_prefix($tokens, 'tid'))) {
      if ($txn->tid > 0) {
        $replacements += token_generate('term', $term_tokens, array(
          'term' => taxonomy_term_load($txn->tid),
        ), $options);
      }
      else {

        // For the general category, only tid and name is supported.
        foreach ($term_tokens as $name => $original) {
          switch ($name) {
            case 'tid':
              $replacements[$original] = '0';
              break;
            case 'name':
              $term_name = t('!Uncategorized', userpoints_translation());
              $replacements[$original] = $sanitize ? filter_xss($term_name) : $term_name;
              break;
          }
        }
      }
    }
    if ($timestamp_tokens = token_find_with_prefix($tokens, 'time-stamp')) {
      $replacements += token_generate('date', $timestamp_tokens, array(
        'date' => $txn->time_stamp,
      ), $options);
    }
    if ($expiry_tokens = token_find_with_prefix($tokens, 'expirydate')) {
      if ($txn->expirydate > 0) {
        $replacements += token_generate('date', $expiry_tokens, array(
          'date' => $txn->expirydate,
        ), $options);
      }
      else {

        // Create array with $original as key and 'Never' as value.
        $replacements += array_fill_keys($expiry_tokens, t('Never'));
      }
    }
  }
  return $replacements;
}