You are here

function regcode_tokens in Registration codes 7

Same name and namespace in other branches
  1. 8 regcode.tokens.inc \regcode_tokens()
  2. 7.2 regcode.module \regcode_tokens()

Implements hook_tokens().

File

./regcode.module, line 839
Main functionality and hooks of regcode module.

Code

function regcode_tokens($type, $tokens, array $data = array(), array $options = array()) {
  if (isset($options['language'])) {
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $replacements = array();
  if ($type == 'regcode' && !empty($data['regcode'])) {
    $regcode = $data['regcode'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'created':
          $replacements[$original] = format_date($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'lastused':
          $replacements[$original] = format_date($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'begins':
          $replacements[$original] = format_date($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'expires':
          $replacements[$original] = format_date($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'code':
          $replacements[$original] = $regcode->code;
          break;
        case 'regurl':
          $replacements[$original] = url('user/register', array(
            'query' => array(
              'regcode' => $regcode->code,
            ),
            'absolute' => TRUE,
          ));
          break;
        case 'is_active':
          $replacements[$original] = $regcode->is_active ? t('active') : t('inactive');
          break;
        case 'maxuses':
          $replacements[$original] = $regcode->maxuses;
          break;
        case 'uses':
          $replacements[$original] = $regcode->uses;
          break;
      }
    }
    if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
      $replacements += token_generate('date', $created_tokens, array(
        'date' => $regcode->created,
      ), $options);
    }
    if ($lastused_tokens = token_find_with_prefix($tokens, 'lastused')) {
      $replacements += token_generate('date', $lastused_tokens, array(
        'date' => $regcode->lastused,
      ), $options);
    }
    if ($begins_tokens = token_find_with_prefix($tokens, 'begins')) {
      $replacements += token_generate('date', $begins_tokens, array(
        'date' => $regcode->begins,
      ), $options);
    }
    if ($expires_tokens = token_find_with_prefix($tokens, 'expires')) {
      $replacements += token_generate('date', $expires_tokens, array(
        'date' => $regcode->expires,
      ), $options);
    }
  }
  return $replacements;
}