[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/token_profile/ -> token_profile.module (source)

   1  <?php
   2  // $Id: token_profile.module,v 1.2.2.1 2010/07/29 14:46:32 fizk Exp $
   3  /**
   4   * @file
   5   * Provides profile tokens in Drupal 6.
   6   */
   7  
   8  /**
   9   * Implementation of hook_help().
  10   */
  11  function token_profile_help($path, $arg) {
  12    if ($path == 'admin/help#profile_token') {
  13      $txt = <<<TXT
  14  The <strong>Profile Tokens</strong> module provides tokens exposing each
  15  profile field defined through the Profile module.
  16  TXT;
  17      return '<p>'. $txt .'</p>';
  18    }
  19  }
  20  
  21  /**
  22   * Implementation of hook_token_values().
  23   */
  24  function token_profile_token_values($type, $object = NULL) {
  25    $values = array();
  26    if ($type == 'node' || $type == 'user') {
  27      switch ($type) {
  28        case 'user':
  29          $account = $object;
  30          break;
  31        case 'node':
  32          $account = user_load($object->uid);
  33          break;
  34      }
  35  
  36      profile_load_profile($account);
  37      $fields = _token_profile_profile_fields();
  38  
  39      foreach ($fields as $name => $field) {
  40        $field->value = $account->$name;
  41        $values[$type . ':' . $name] = _token_profile_format_field($field);
  42      }
  43    }
  44  
  45    return $values;
  46  }
  47  
  48  /**
  49   * Implementation of hook_token_list().
  50   */
  51  function token_profile_token_list($type = 'all') {
  52    if ($type == 'node' || $type == 'user' || $type == 'all') {
  53      $show_user = ($type == 'user' || $type == 'all');
  54      $show_node = ($type == 'node' || $type == 'all');
  55      $fields = _token_profile_profile_fields();
  56      $tokens = array();
  57  
  58      foreach ($fields as $name => $field) {
  59        if($show_node) {
  60          $tokens['node']['node:' . $name] = $field->title;
  61        }
  62        if($show_user) {
  63          $tokens['user']['user:' . $name] = $field->title;
  64        }
  65      }
  66  
  67      return $tokens;
  68    }
  69  }
  70  
  71  function _token_profile_profile_fields() {
  72    static $fields;
  73  
  74    if (!isset($fields)) {
  75      $fields = array();
  76      $result = db_query('SELECT name, title, type FROM {profile_fields}');
  77      while ($field = db_fetch_object($result)) {
  78        $fields[$field->name] = $field;
  79      }
  80    }
  81  
  82    return $fields;
  83  }
  84  
  85  function _token_profile_format_field($field) {
  86    switch ($field->type) {
  87      case 'checkbox':
  88        return ($field->value) ? check_plain($field->title) : '';
  89      case 'url':
  90        return  '<a href="'. check_url($field->value) .'">'. check_plain($field->value) .'</a>';
  91      case 'textarea':
  92        return check_markup($field->value);
  93      case 'date':
  94        $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
  95        // Note: Avoid PHP's date() because it does not handle dates before
  96        // 1970 on Windows. This would make the date field useless for e.g.
  97        // birthdays.
  98        $replace = array('d' => sprintf('%02d', $field->value['day']),
  99                'j' => $field->value['day'],
 100                'm' => sprintf('%02d', $field->value['month']),
 101                'M' => map_month($field->value['month']),
 102                'Y' => $field->value['year'],
 103                'H:i' => NULL,
 104                'g:ia' => NULL);
 105        return strtr($format, $replace);
 106      case 'list':
 107        $values = split("[,\n\r]", $field->value);
 108        $fields = array();
 109        foreach ($values as $value) {
 110          $value = trim($value);
 111          if ($value) {
 112            $fields[] = check_plain($value);
 113          }
 114        }
 115        return implode(', ', $fields);
 116      default:
 117        return check_plain($field->value);
 118    }
 119  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7