681 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			Plaintext
		
	
	
			
		
		
	
	
			681 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			Plaintext
		
	
	
<?php
 | 
						|
// $Id$
 | 
						|
 | 
						|
/**
 | 
						|
 * @file
 | 
						|
 * Support for configurable user profiles.
 | 
						|
 */
 | 
						|
 | 
						|
/**
 | 
						|
 * Flags to define the visibility of a profile field.
 | 
						|
 */
 | 
						|
define('PROFILE_PRIVATE', 1);
 | 
						|
define('PROFILE_PUBLIC', 2);
 | 
						|
define('PROFILE_PUBLIC_LISTINGS', 3);
 | 
						|
define('PROFILE_HIDDEN', 4);
 | 
						|
 | 
						|
/**
 | 
						|
 * Implementation of hook_help().
 | 
						|
 */
 | 
						|
function profile_help($section) {
 | 
						|
  switch ($section) {
 | 
						|
    case 'admin/modules#description':
 | 
						|
      return t('Supports configurable user profiles.');
 | 
						|
    case 'admin/settings/profile':
 | 
						|
      return t('<p>Here you can define custom fields that users can fill in in their user profile (such as <em>country</em>, <em>real name</em>, <em>age</em>, ...).</p>');
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implementation of hook_block().
 | 
						|
 */
 | 
						|
function profile_block($op = 'list', $delta = 0, $edit = array()) {
 | 
						|
 | 
						|
  if ($op == 'list') {
 | 
						|
     $blocks[0]['info'] = t('Author information');
 | 
						|
 | 
						|
     return $blocks;
 | 
						|
  }
 | 
						|
  else if ($op == 'configure' && $delta == 0) {
 | 
						|
    // Compile a list of fields to show
 | 
						|
    $fields = array();
 | 
						|
    $result = db_query('SELECT name, title FROM {profile_fields} ORDER BY weight');
 | 
						|
    while ($record = db_fetch_object($result)) {
 | 
						|
      $fields[$record->name] = $record->title;
 | 
						|
    }
 | 
						|
    $fields['user_profile'] = t('Link to full user profile');
 | 
						|
    $output .= form_checkboxes(t('Profile fields to display'), 'profile_block_author_fields', variable_get('profile_block_author_fields', NULL), $fields, t('Select which profile fields you wish to display in the block.  Only fields designated as public in the <a href="%profile-admin">profile field configuration</a> are available.', array('%profile-admin' => url('admin/settings/profile'))));
 | 
						|
    return $output;
 | 
						|
  }
 | 
						|
  else if ($op == 'save' && $delta == 0) {
 | 
						|
    variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
 | 
						|
  }
 | 
						|
  else if ($op == 'view') {
 | 
						|
    if (user_access('access user profiles')) {
 | 
						|
      if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
 | 
						|
        $result = db_query('SELECT uid FROM {node} WHERE nid = %d ORDER BY uid DESC', arg(1));
 | 
						|
        $node = db_fetch_object($result);
 | 
						|
        $account = user_load(array('uid' => $node->uid));
 | 
						|
 | 
						|
        if ($use_fields = variable_get('profile_block_author_fields', array())) {
 | 
						|
          // Compile a list of fields to show
 | 
						|
          $fields = array();
 | 
						|
          $result = db_query('SELECT name, title, type, visibility FROM {profile_fields} WHERE visibility = %d ORDER BY weight', PROFILE_PUBLIC, PROFILE_PUBLIC_LISTING);
 | 
						|
            while ($record = db_fetch_object($result)) {
 | 
						|
              // Endure that field is displayed only if it is among the defined block fields and, if it is private, the user has appropriate permissions.
 | 
						|
              if (in_array($record->name, $use_fields)) {
 | 
						|
              $fields[] = $record;
 | 
						|
            }
 | 
						|
          }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($fields) {
 | 
						|
          $output .= theme('profile_block', $account, $fields, true);
 | 
						|
        }
 | 
						|
 | 
						|
        if (in_array('user_profile', $use_fields)) {
 | 
						|
          $output .= '<div>' . l(t('View full user profile'), 'user/' . $account->uid) . '</div>';
 | 
						|
        }
 | 
						|
      }
 | 
						|
 | 
						|
      if ($output) {
 | 
						|
         $block['subject'] = t('About %name', array('%name' => $account->name));
 | 
						|
         $block['content'] = $output;
 | 
						|
         return $block;
 | 
						|
      }
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implementation of hook_menu().
 | 
						|
 */
 | 
						|
function profile_menu($may_cache) {
 | 
						|
  global $user;
 | 
						|
  $items = array();
 | 
						|
 | 
						|
  if ($may_cache) {
 | 
						|
    $items[] = array('path' => 'profile', 'title' => t('user list'),
 | 
						|
      'callback' => 'profile_browse',
 | 
						|
      'access' => user_access('access user profiles'),
 | 
						|
      'type' => MENU_SUGGESTED_ITEM);
 | 
						|
    $items[] = array('path' => 'admin/settings/profile', 'title' => t('profiles'),
 | 
						|
      'callback' => 'profile_admin_overview',
 | 
						|
      'access' => user_access('administer users'));
 | 
						|
    $items[] = array('path' => 'admin/settings/profile/add', 'title' => t('add field'),
 | 
						|
      'callback' => 'profile_admin_add',
 | 
						|
      'access' => user_access('administer users'),
 | 
						|
      'type' => MENU_CALLBACK);
 | 
						|
    $items[] = array('path' => 'admin/settings/profile/edit', 'title' => t('edit field'),
 | 
						|
      'callback' => 'profile_admin_edit',
 | 
						|
      'access' => user_access('administer users'),
 | 
						|
      'type' => MENU_CALLBACK);
 | 
						|
    $items[] = array('path' => 'admin/settings/profile/delete', 'title' => t('delete field'),
 | 
						|
      'callback' => 'profile_admin_delete',
 | 
						|
      'access' => user_access('administer users'),
 | 
						|
      'type' => MENU_CALLBACK);
 | 
						|
  }
 | 
						|
 | 
						|
  return $items;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; display a list of user information.
 | 
						|
 */
 | 
						|
function profile_browse() {
 | 
						|
 | 
						|
  $name = arg(1);
 | 
						|
  $value = arg(2);
 | 
						|
 | 
						|
  $field = db_fetch_object(db_query("SELECT DISTINCT(fid), type, title, page, visibility FROM {profile_fields} WHERE name = '%s'", $name));
 | 
						|
 | 
						|
  if ($name && $field->fid) {
 | 
						|
    // Do not allow browsing of private fields by non-admins
 | 
						|
    if (!user_access('administer users') && $field->visibility == PROFILE_PRIVATE) {
 | 
						|
       drupal_access_denied();
 | 
						|
       return;
 | 
						|
    }
 | 
						|
 | 
						|
    // Compile a list of fields to show
 | 
						|
    $fields = array();
 | 
						|
    $result = db_query('SELECT name, title, type FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight', $field->fid, PROFILE_PUBLIC_LISTINGS);
 | 
						|
    while ($record = db_fetch_object($result)) {
 | 
						|
      $fields[] = $record;
 | 
						|
    }
 | 
						|
 | 
						|
    // Determine what query to use:
 | 
						|
    switch ($field->type) {
 | 
						|
      case 'checkbox':
 | 
						|
        $query = 'v.value = 1';
 | 
						|
        break;
 | 
						|
      case 'selection':
 | 
						|
        $query = "v.value = '". db_escape_string($value) ."'";
 | 
						|
        break;
 | 
						|
      case 'list':
 | 
						|
        $query = "v.value LIKE '%%". db_escape_string($value) ."%%'";
 | 
						|
        break;
 | 
						|
      default:
 | 
						|
        drupal_not_found();
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    // Extract the affected users:
 | 
						|
    $result = pager_query("SELECT u.uid FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = %d AND $query ORDER BY u.access DESC", 20, 0, NULL, $field->fid);
 | 
						|
 | 
						|
    $output = '<div id="profile">';
 | 
						|
    while ($account = db_fetch_object($result)) {
 | 
						|
      $output .= theme('profile_listing', user_load(array('uid' => $account->uid)), $fields);
 | 
						|
    }
 | 
						|
    $output .= theme('pager', NULL, 20);
 | 
						|
 | 
						|
    if ($field->type == 'selection' || $field->type == 'list') {
 | 
						|
      $title = strtr($field->page, array('%value' => theme('placeholder', $value)));
 | 
						|
    }
 | 
						|
    else {
 | 
						|
      $title = $field->page;
 | 
						|
    }
 | 
						|
    $output .= '</div>';
 | 
						|
 | 
						|
    drupal_set_title($title);
 | 
						|
    return $output;
 | 
						|
  }
 | 
						|
  else if ($name && !$field->id) {
 | 
						|
    drupal_not_found();
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    // Compile a list of fields to show
 | 
						|
    $fields = array();
 | 
						|
    $result = db_query('SELECT name, title, type FROM {profile_fields} WHERE visibility = %d', PROFILE_PUBLIC_LISTINGS);
 | 
						|
    while ($record = db_fetch_object($result)) {
 | 
						|
      $fields[] = $record;
 | 
						|
    }
 | 
						|
 | 
						|
    // Extract the affected users:
 | 
						|
    $result = pager_query("SELECT uid FROM {users} WHERE uid > 0 ORDER BY access DESC", 20, 0, NULL);
 | 
						|
 | 
						|
    $output = '<div id="profile">';
 | 
						|
    while ($account = db_fetch_object($result)) {
 | 
						|
      $output .= theme('profile_listing', user_load(array('uid' => $account->uid)), $fields);
 | 
						|
    }
 | 
						|
    $output .= '</div>';
 | 
						|
    $output .= theme('pager', NULL, 20);
 | 
						|
 | 
						|
    drupal_set_title(t('user list'));
 | 
						|
    return $output;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function profile_load_profile(&$user) {
 | 
						|
  $result = db_query('SELECT f.name, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
 | 
						|
  while ($field = db_fetch_object($result)) {
 | 
						|
    if (empty($user->{$field->name})) {
 | 
						|
      $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function profile_save_profile(&$edit, &$user, $category) {
 | 
						|
  if (($_GET['q'] == 'user/register') ? 1 : 0) {
 | 
						|
    $result = db_query('SELECT fid, name, type FROM {profile_fields} WHERE register = 1 AND visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $result = db_query("SELECT fid, name, type FROM {profile_fields} WHERE LOWER(category) = LOWER('%s') AND visibility != %d", $category, PROFILE_HIDDEN);
 | 
						|
    // We use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
 | 
						|
  }
 | 
						|
  while ($field = db_fetch_object($result)) {
 | 
						|
    if (_profile_field_serialize($field->type)) {
 | 
						|
       $edit[$field->name] = serialize($edit[$field->name]);
 | 
						|
    }
 | 
						|
    db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid);
 | 
						|
    db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
 | 
						|
    // Mark field as handled (prevents saving to user->data).
 | 
						|
    $edit[$field->name] = NULL;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function profile_view_field($user, $field) {
 | 
						|
  // Only allow browsing of private fields for admins
 | 
						|
  $browse = user_access('administer users') || $field->visibility != PROFILE_PRIVATE;
 | 
						|
 | 
						|
  if ($field->fid == 18 || $field->fid == 19 || $field->fid == 20) {
 | 
						|
    return;
 | 
						|
  }
 | 
						|
  
 | 
						|
  if ($value = $user->{$field->name}) {
 | 
						|
    switch ($field->type) {
 | 
						|
      case 'textfield':
 | 
						|
        return check_plain($value);
 | 
						|
      case 'textarea':
 | 
						|
        return check_output($value);
 | 
						|
      case 'selection':
 | 
						|
        return $browse ? l($value, "profile/$field->name/$value") : check_plain($value);
 | 
						|
      case 'checkbox':
 | 
						|
        return $browse ? l($field->title, "profile/$field->name") : check_plain($field->title);
 | 
						|
      case 'url':
 | 
						|
        return '<a href="'. check_url($value) .'">'. check_plain($value) .'</a>';
 | 
						|
      case 'date':
 | 
						|
        list($format) = explode(' - ', variable_get('date_format_short', 'm/d/Y - H:i'), 2);
 | 
						|
        // Note: we avoid PHP's date() because it does not handle dates before
 | 
						|
        // 1970 on Windows. This would make the date field useless for e.g.
 | 
						|
        // birthdays.
 | 
						|
        $replace = array('d' => sprintf('%02d', $value['day']),
 | 
						|
                         'j' => $value['day'],
 | 
						|
                         'm' => sprintf('%02d', $value['month']),
 | 
						|
                         'M' => _profile_map_month($value['month']),
 | 
						|
                         'Y' => $value['year']);
 | 
						|
        return strtr($format, $replace);
 | 
						|
      case 'list':
 | 
						|
        $values = split("[,\n\r]", $value);
 | 
						|
        $fields = array();
 | 
						|
        foreach ($values as $value) {
 | 
						|
          if ($value = trim($value)) {
 | 
						|
            $fields[] = $browse ? l($value, "profile/$field->name/$value") : check_plain($value);
 | 
						|
          }
 | 
						|
        }
 | 
						|
        return implode(', ', $fields);
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function profile_view_profile($user) {
 | 
						|
 | 
						|
  profile_load_profile($user);
 | 
						|
 | 
						|
  // Show private fields to administrators and people viewing their own account.
 | 
						|
  if (user_access('administer users') || $GLOBALS['user']->uid == $user->uid) {
 | 
						|
    $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d AND visibility != %d ORDER BY category, weight', PROFILE_PRIVATE, PROFILE_HIDDEN);
 | 
						|
  }
 | 
						|
 | 
						|
  while ($field = db_fetch_object($result)) {
 | 
						|
    if ($value = profile_view_field($user, $field)) {
 | 
						|
      $description = ($field->visibility == PROFILE_PRIVATE) ? t('The content of this field is private and only visible to yourself.') : '';
 | 
						|
      $title = ($field->type != 'checkbox') ? check_plain($field->title) : '';
 | 
						|
      $fields[$field->category] .= form_item($title, $value, $description);
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  return $fields;
 | 
						|
}
 | 
						|
 | 
						|
function _profile_form_explanation($field) {
 | 
						|
  $output = $field->explanation;
 | 
						|
 | 
						|
  if ($field->type == 'list') {
 | 
						|
    $output .= ' '. t('Put each item on a separate line or separate them by commas.  No HTML allowed.');
 | 
						|
  }
 | 
						|
 | 
						|
  if ($field->visibility == PROFILE_PRIVATE) {
 | 
						|
    $output .= ' '. t('The content of this field is kept private and will not be shown publicly.');
 | 
						|
  }
 | 
						|
 | 
						|
  return $output;
 | 
						|
}
 | 
						|
 | 
						|
function profile_form_profile($edit, $user, $category) {
 | 
						|
 | 
						|
  if (($_GET['q'] == 'user/register') ? 1 : 0) {
 | 
						|
    $result = db_query('SELECT * FROM {profile_fields} WHERE register = 1 ORDER BY category, weight');
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $result = db_query("SELECT * FROM {profile_fields} WHERE LOWER(category) = LOWER('%s') ORDER BY weight", $category);
 | 
						|
    // We use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
 | 
						|
  }
 | 
						|
 | 
						|
  $fields = array();
 | 
						|
  while ($field = db_fetch_object($result)) {
 | 
						|
    $category = $field->category;
 | 
						|
    switch ($field->type) {
 | 
						|
      case 'textfield':
 | 
						|
      case 'url':
 | 
						|
        $fields[$category] .= form_textfield(check_plain($field->title), $field->name, $edit[$field->name], 70, 255, _profile_form_explanation($field), NULL, $field->required);
 | 
						|
        break;
 | 
						|
      case 'textarea':
 | 
						|
        $fields[$category] .= form_textarea(check_plain($field->title), $field->name, $edit[$field->name], 70, 5, _profile_form_explanation($field), NULL, $field->required);
 | 
						|
        break;
 | 
						|
      case 'list':
 | 
						|
        $fields[$category] .= form_textarea(check_plain($field->title), $field->name, $edit[$field->name], 70, 5, _profile_form_explanation($field), NULL, $field->required);
 | 
						|
        break;
 | 
						|
      case 'checkbox':
 | 
						|
        $fields[$category] .= form_checkbox(check_plain($field->title), $field->name, 1, $edit[$field->name], _profile_form_explanation($field), NULL, $field->required);
 | 
						|
        break;
 | 
						|
      case 'selection':
 | 
						|
        $options = array('--');
 | 
						|
        $lines = split("[,\n\r]", $field->options);
 | 
						|
        foreach ($lines as $line) {
 | 
						|
          if ($line = trim($line)) {
 | 
						|
            $options[$line] = $line;
 | 
						|
          }
 | 
						|
        }
 | 
						|
 | 
						|
        $fields[$category] .= form_select(check_plain($field->title), $field->name, $edit[$field->name], $options, _profile_form_explanation($field), 0, 0, $field->required);
 | 
						|
        break;
 | 
						|
      case 'date':
 | 
						|
        $fields[$category] .= _profile_date_field($field, $edit);
 | 
						|
        break;
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  if ($fields) {
 | 
						|
    foreach ($fields as $category => $data) {
 | 
						|
      $output[] = array('title' => $category, 'data' => $data);
 | 
						|
    }
 | 
						|
    return $output;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Helper function: output a date selector
 | 
						|
 */
 | 
						|
function _profile_date_field($field, $edit) {
 | 
						|
  // Default to current date
 | 
						|
  if (!isset($edit[$field->name])) {
 | 
						|
    $edit[$field->name] = array('day' => format_date(time(), 'custom', 'j'),
 | 
						|
                                'month' => format_date(time(), 'custom', 'n'),
 | 
						|
                                'year' => format_date(time(), 'custom', 'Y'));
 | 
						|
  }
 | 
						|
 | 
						|
  // Determine the order of day, month, year in the site's chosen date format.
 | 
						|
  $format = variable_get('date_format_short', 'm/d/Y');
 | 
						|
  $sort = array();
 | 
						|
  $sort['day'] = max(strpos($format, 'd'), strpos($format, 'j'));
 | 
						|
  $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
 | 
						|
  $sort['year'] = strpos($format, 'Y');
 | 
						|
  asort($sort);
 | 
						|
  $order = array_keys($sort);
 | 
						|
 | 
						|
  // Output multi-selector for date
 | 
						|
  $output = '<div class="container-inline">';
 | 
						|
  foreach ($order as $type) {
 | 
						|
    switch ($type) {
 | 
						|
      case 'day':
 | 
						|
        $options = drupal_map_assoc(range(1, 31));
 | 
						|
        break;
 | 
						|
      case 'month':
 | 
						|
        $options = drupal_map_assoc(range(1, 12), '_profile_map_month');
 | 
						|
        break;
 | 
						|
      case 'year':
 | 
						|
        $options = drupal_map_assoc(range(1900, 2050));
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    $output .= form_select('', $field->name .']['. $type, $edit[$field->name][$type], $options, '', 0, 0);
 | 
						|
  }
 | 
						|
  $output .= '</div>';
 | 
						|
 | 
						|
  return form_item(check_plain($field->title), $output, _profile_form_explanation($field), NULL, $field->required);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Helper function for usage with drupal_map_assoc
 | 
						|
 */
 | 
						|
function _profile_map_month($month) {
 | 
						|
  return format_date(gmmktime(0, 0, 0, $month, 2, 1970), 'custom', 'M', 0);
 | 
						|
}
 | 
						|
 | 
						|
function profile_validate_profile($edit, $category) {
 | 
						|
 | 
						|
  if (($_GET['q'] == 'user/register') ? 1 : 0) {
 | 
						|
    $result = db_query('SELECT * FROM {profile_fields} WHERE register = 1 ORDER BY category, weight');
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $result = db_query("SELECT * FROM {profile_fields} WHERE LOWER(category) = LOWER('%s') ORDER BY weight", $category);
 | 
						|
    // We use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
 | 
						|
  }
 | 
						|
 | 
						|
  while ($field = db_fetch_object($result)) {
 | 
						|
    if ($edit[$field->name]) {
 | 
						|
      if ($field->type == 'url') {
 | 
						|
        if (!valid_url($edit[$field->name], true)) {
 | 
						|
          form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => theme('placeholder', $field->title))));
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
    else if ($field->required && !user_access('administer users')) {
 | 
						|
      form_set_error($field->name, t('The field %field is required.', array('%field' => theme('placeholder', $field->title))));
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  return $edit;
 | 
						|
}
 | 
						|
 | 
						|
function profile_categories() {
 | 
						|
  $result = db_query("SELECT DISTINCT(category) FROM {profile_fields}");
 | 
						|
  while ($category = db_fetch_object($result)) {
 | 
						|
    $data[] = array('name' => check_plain($category->category), 'title' => $category->category, 'weight' => 3);
 | 
						|
  }
 | 
						|
  return $data;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implementation of hook_user().
 | 
						|
 */
 | 
						|
function profile_user($type, &$edit, &$user, $category = NULL) {
 | 
						|
  switch ($type) {
 | 
						|
    case 'load':
 | 
						|
      return profile_load_profile($user);
 | 
						|
    case 'register':
 | 
						|
      return profile_form_profile($edit, $user, $category);
 | 
						|
    case 'update':
 | 
						|
    case 'insert':
 | 
						|
      return profile_save_profile($edit, $user, $category);
 | 
						|
    case 'view':
 | 
						|
      return profile_view_profile($user);
 | 
						|
    case 'form':
 | 
						|
      return profile_form_profile($edit, $user, $category);
 | 
						|
    case 'validate':
 | 
						|
      return profile_validate_profile($edit, $category);
 | 
						|
    case 'categories':
 | 
						|
      return profile_categories();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function profile_validate_form($edit) {
 | 
						|
 | 
						|
  // Validate the title:
 | 
						|
 | 
						|
  if (!$edit['title']) {
 | 
						|
    form_set_error('title', t('You must enter a title.'));
 | 
						|
  }
 | 
						|
 | 
						|
  // Validate the 'form name':
 | 
						|
 | 
						|
  if (eregi('[^a-z0-9_-]', $edit['name'])) {
 | 
						|
    form_set_error('name', t('The specified form name contains one or more illegal characters.  Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.'));
 | 
						|
  }
 | 
						|
 | 
						|
  if (in_array($edit['name'], user_fields())) {
 | 
						|
    form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
 | 
						|
  }
 | 
						|
 | 
						|
  // Validate the category:
 | 
						|
  if (!$edit['category']) {
 | 
						|
    form_set_error('category', t('You must enter a category.'));
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; adds a new field to all user profiles.
 | 
						|
 */
 | 
						|
function profile_admin_add($type) {
 | 
						|
  if ($_POST['op']) {
 | 
						|
    $data = $_POST['edit'];
 | 
						|
 | 
						|
    // Validate the form:
 | 
						|
    profile_validate_form($data);
 | 
						|
 | 
						|
    if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s' AND category = '%s'", $data['title'], $data['category']))) {
 | 
						|
      form_set_error('title', t('The specified title is already in use.'));
 | 
						|
    }
 | 
						|
 | 
						|
    if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) {
 | 
						|
      form_set_error('name', t('The specified name is already in use.'));
 | 
						|
    }
 | 
						|
 | 
						|
    if (!form_get_errors()) {
 | 
						|
      db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['required'], $data['register'], $data['visibility'], $data['options'], $data['page']);
 | 
						|
 | 
						|
      cache_clear_all();
 | 
						|
 | 
						|
      drupal_set_message(t('The field has been created.'));
 | 
						|
      drupal_goto('admin/settings/profile');
 | 
						|
    }
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $data = array('name' => 'profile_');
 | 
						|
  }
 | 
						|
 | 
						|
  drupal_set_title(t('Add new %type', array('%type' => _profile_field_types($type))));
 | 
						|
  return _profile_field_form($type, $data);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; displays the profile field editing form.
 | 
						|
 */
 | 
						|
function profile_admin_edit($fid) {
 | 
						|
 | 
						|
  if ($_POST['op']) {
 | 
						|
    $data = $_POST['edit'];
 | 
						|
 | 
						|
    // Validate form:
 | 
						|
    profile_validate_form($data);
 | 
						|
 | 
						|
    if (!form_get_errors()) {
 | 
						|
      db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, register = %d, visibility = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['required'], $data['register'], $data['visibility'], $data['options'], $data['page'], $fid);
 | 
						|
 | 
						|
      cache_clear_all();
 | 
						|
 | 
						|
      drupal_set_message(t('The field has been updated.'));
 | 
						|
      drupal_goto('admin/settings/profile');
 | 
						|
    }
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $data = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
 | 
						|
  }
 | 
						|
 | 
						|
  drupal_set_title(t('Edit %type', array('%type' => $data['type'])));
 | 
						|
  return _profile_field_form($data['type'], $data);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; deletes a field from all user profiles.
 | 
						|
 */
 | 
						|
function profile_admin_delete($fid) {
 | 
						|
  $field = db_fetch_object(db_query("SELECT title FROM {profile_fields} WHERE fid = %d", $fid));
 | 
						|
  if ($_POST['edit']['confirm']) {
 | 
						|
    db_query('DELETE FROM {profile_fields} WHERE fid = %d', $fid);
 | 
						|
    cache_clear_all();
 | 
						|
    drupal_set_message(t('The field %field has been deleted.', array('%field' => theme('placeholder', $field->title))));
 | 
						|
    drupal_goto('admin/settings/profile');
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $output = theme('confirm',
 | 
						|
                    t('Do you want to remove the field %field?',
 | 
						|
                    array('%field' => $field->title)),
 | 
						|
                    'admin/settings/profile');
 | 
						|
    return $output;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function _profile_field_form($type, $edit = array()) {
 | 
						|
 | 
						|
  $group  = form_textfield(t('Category'), 'category', $edit['category'], 70, 128, t('The category the new field should be part of.  Categories are used to group fields logically.  An example category is "Personal information".'));
 | 
						|
  $group .= form_textfield(t('Title'), 'title', $edit['title'], 70, 128, t('The title of the new field.  The title will be shown to the user.  An example title is "Favorite color".'));
 | 
						|
  $group .= form_textfield(t('Form name'), 'name', $edit['name'], 70, 128, t('The name of the field.  The form name is not shown to the user but used internally in the HTML code and URLs.
 | 
						|
Unless you know what you are doing, it is highly recommended that you prefix the form name with <code>profile_</code> to avoid name clashes with other fields.  Spaces or any other special characters except dash (-) and underscore (_) are not allowed. An example name is "profile_favorite_color" or perhaps just "profile_color".'));
 | 
						|
  $group .= form_textarea(t('Explanation'), 'explanation', $edit['explanation'], 70, 3, t('An optional explanation to go with the new field.  The explanation will be shown to the user.'));
 | 
						|
  if ($type == 'selection') {
 | 
						|
    $group .= form_textarea(t('Selection options'), 'options', $edit['options'], 70, 8, t('A list of all options.  Put each option on a separate line.  Example options are "red", "blue", "green", etc.'));
 | 
						|
  }
 | 
						|
  $group .= form_weight(t('Weight'), 'weight', $edit['weight'], 5, t('The weights define the order in which the form fields are shown.  Lighter fields "float up" towards the top of the category.'));
 | 
						|
  $group .= form_radios(t('Visibility'), 'visibility', $edit['visibility'], array(PROFILE_HIDDEN => t('Hidden profile field, only accessible by administrators, modules and themes.'), PROFILE_PRIVATE => t('Private field, content only available to privileged users.'), PROFILE_PUBLIC => t('Public field, content shown on profile page but not used on member list pages.'), PROFILE_PUBLIC_LISTINGS => t('Public field, content shown on profile page and on member list pages.')));
 | 
						|
  if ($type == 'selection' || $type == 'list') {
 | 
						|
    $group .= form_textfield(t('Page title'), 'page', $edit['page'], 70, 128, t('The title of the page showing all users with the specified field.  The word <code>%value</code> will be substituted with the corresponding value.  An example page title is "People whose favorite color is %value".  Only applicable if the field is configured to be shown on member list pages.'));
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $group .= form_textfield(t('Page title'), 'page', $edit['page'], 70, 128, t('The title of the page showing all users with the specified field.  Only applicable if the field is configured to be shown on member listings.'));
 | 
						|
  }
 | 
						|
  $group .= form_checkbox(t('The user must enter a value.'), 'required', 1, $edit['required']);
 | 
						|
  $group .= form_checkbox(t('Visible in user registration form.'), 'register', 1, $edit['register']);
 | 
						|
 | 
						|
  $output  = form_group(t('Field settings'), $group);
 | 
						|
  $output .= form_submit(t('Save field'));
 | 
						|
 | 
						|
  return form($output);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; display a listing of all editable profile fields.
 | 
						|
 */
 | 
						|
function profile_admin_overview() {
 | 
						|
 | 
						|
  $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
 | 
						|
  $rows = array();
 | 
						|
  while ($field = db_fetch_object($result)) {
 | 
						|
    $rows[] = array(check_plain($field->title), $field->name, _profile_field_types($field->type), $field->category, l(t('edit'), "admin/settings/profile/edit/$field->fid"), l(t('delete'), "admin/settings/profile/delete/$field->fid"));
 | 
						|
  }
 | 
						|
  if (count($rows) == 0) {
 | 
						|
    $rows[] = array(array('data' => t('No fields defined.'), 'colspan' => '6'));
 | 
						|
  }
 | 
						|
 | 
						|
  $header = array(t('Title'), t('Name'), t('Type'), t('Category'), array('data' => t('Operations'), 'colspan' => '2'));
 | 
						|
 | 
						|
  $output  = theme('table', $header, $rows);
 | 
						|
  $output .= '<h2>'. t('Add new field') .'</h2>';
 | 
						|
  $output .= '<ul>';
 | 
						|
  foreach (_profile_field_types() as $key => $value) {
 | 
						|
    $output .= '<li>'. l($value, "admin/settings/profile/add/$key") .'</li>';
 | 
						|
  }
 | 
						|
  $output .= '</ul>';
 | 
						|
 | 
						|
  return $output;
 | 
						|
}
 | 
						|
 | 
						|
function theme_profile_block($user, $fields = array()) {
 | 
						|
 | 
						|
  $output .= theme('user_picture', $user);
 | 
						|
 | 
						|
  foreach ($fields as $field) {
 | 
						|
    if ($value = profile_view_field($user, $field)) {
 | 
						|
      $output .= "<p><strong>$field->title:</strong><br />$value</p>\n";
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  return $output;
 | 
						|
}
 | 
						|
 | 
						|
function theme_profile_listing($user, $fields = array()) {
 | 
						|
 | 
						|
  $output  = "<div class=\"profile\">\n";
 | 
						|
  $output .= theme('user_picture', $user);
 | 
						|
  $output .= ' <div class="name">'. format_name($user) ."</div>\n";
 | 
						|
 | 
						|
  foreach ($fields as $field) {
 | 
						|
    if ($value = profile_view_field($user, $field)) {
 | 
						|
      $output .= " <div class=\"field\">$value</div>\n";
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  $output .= "</div>\n";
 | 
						|
 | 
						|
  return $output;
 | 
						|
}
 | 
						|
 | 
						|
function _profile_field_types($type = NULL) {
 | 
						|
  $types = array('textfield' => t('single-line textfield'),
 | 
						|
                 'textarea' => t('multi-line textfield'),
 | 
						|
                 'checkbox' => t('checkbox'),
 | 
						|
                 'selection' => t('list selection'),
 | 
						|
                 'list' => t('freeform list'),
 | 
						|
                 'url' => t('URL'),
 | 
						|
                 'date' => t('date'));
 | 
						|
  return isset($type) ? $types[$type] : $types;
 | 
						|
}
 | 
						|
 | 
						|
function _profile_field_serialize($type = NULL) {
 | 
						|
  return $type == 'date';
 | 
						|
}
 | 
						|
 | 
						|
?>
 |