You are here

function classified_tokens in Classified Ads 7.3

Implements hook_tokens().

D7 tokens are chainable, unlike those in D6.

token_replace() options:

  • language: ignored
  • callback: unused in hook_tokens() implementations
  • clear: unused in hook_tokens() implementations
  • sanitized

File

./classified.tokens.inc, line 72
Token hooks for Classified Ads module.

Code

function classified_tokens($type, $tokens, array $data = array(), array $options = array()) {
  global $user;
  $ret = array();
  if ($type != 'user' || empty($data['user'])) {
    return $ret;
  }
  $account = $data['user'];
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }
  foreach ($tokens as $name => $original) {
    switch ($name) {
      case 'classified-ads-url':
        $ret[$original] = url('user/' . $account->uid . '/classified', $url_options);
        break;
      case 'classified-ads':
        $min_status = $account->uid == $user->uid || user_access('administer nodes') ? 0 : 1;
        $q = db_select('node', 'n')
          ->comment(__FUNCTION__)
          ->range(0, 10);
        $cn = $q
          ->innerJoin('classified_node', 'cn', 'n.vid = cn.vid');
        $q
          ->addField('n', 'nid', 'id');
        $q
          ->condition('n.type', 'classified')
          ->condition('n.uid', $account->uid)
          ->condition('n.status', $min_status, '>=')
          ->orderBy("{$cn}.expires")
          ->orderBy("n.changed")
          ->orderBy('n.created')
          ->addTag('node_access');
        $results = $q
          ->execute();
        $ads = _classified_list_nodes($results);
        unset($cn, $min_status, $q);
        $ret[$original] = empty($ads['#rows']) ? t('No Classified Ads') : render($ads);
        break;
      case 'classified-ads-plain':
        $min_status = $account->uid == $user->uid || user_access('administer nodes') ? 0 : 1;
        $q = db_select('node', 'n')
          ->comment(__FUNCTION__)
          ->fields('n', array(
          'nid',
          'title',
        ))
          ->range(0, 10);
        $cn = $q
          ->innerJoin('classified_node', 'cn', 'n.vid = cn.vid');
        $q
          ->condition('n.type', 'classified')
          ->condition('n.uid', $account->uid)
          ->condition('n.status', $min_status, '>=')
          ->orderBy("{$cn}.expires")
          ->orderBy("n.changed")
          ->orderBy('n.created')
          ->addTag('node_access');
        $results = $q
          ->execute();
        unset($cn, $min_status, $q);
        $ads = '';
        foreach ($results as $result) {

          // Always sanitized.
          $ads .= t("(@nid) @title\n", array(
            '@nid' => $result->nid,
            '@title' => $result->title,
          ));
        }
        $ret[$original] = empty($ads) ? t('No Classified Ads') : $ads;
        break;
    }
  }
  if ($url_tokens = token_find_with_prefix($tokens, 'classified-ads-url')) {
    $ret += token_generate('url', $url_tokens, array(
      'path' => 'user/' . $account->uid . '/ads',
    ), $options);
  }
  return $ret;
}