2002-08-08 18:52:55 +00:00
<?php
2004-03-13 06:10:20 +00:00
// $Id$
2002-08-08 18:52:55 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Support for configurable user profiles.
*/
2004-06-27 19:10:52 +00:00
/**
* Flags to define the visibility of a profile field.
*/
define('PROFILE_PRIVATE', 1);
define('PROFILE_PUBLIC', 2);
define('PROFILE_PUBLIC_LISTINGS', 3);
2005-04-18 20:58:39 +00:00
define('PROFILE_HIDDEN', 4);
2004-06-27 19:10:52 +00:00
2004-06-18 15:04:37 +00:00
/**
* Implementation of hook_help().
*/
2004-03-11 20:33:59 +00:00
function profile_help($section) {
2003-08-23 18:27:05 +00:00
switch ($section) {
2005-11-01 10:17:34 +00:00
case 'admin/help#profile':
$output = '<p>'. t('The profile module allows you to define custom fields (such as country, real name, age, ...) in the user profile. This permits users of a site to share more information about themselves, and can help community-based sites to organize users around profile fields.') .'</p>';
$output .= t('<p>The following types of fields can be added to the user profile:</p>
<ul>
<li>single-line textfield</li>
<li>multi-line textfield</li>
<li>checkbox</li>
<li>list selection</li>
<li>freeform list</li>
<li>URL</li>
<li>date</li>
</ul>
');
$output .= t('<p>You can</p>
<ul>
<li>view user <a href="%profile">profiles</a>.</li>
<li>administer profile settings: <a href="%admin-settings-profile">administer >> settings >> profiles</a>.</li>
</ul>
', array('%profile' => url('profile'), '%admin-settings-profile' => url('admin/settings/profile')));
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%profile">Profile page</a>.', array('%profile' => 'http://www.drupal.org/handbook/modules/profile/')) .'</p>';
return $output;
2004-06-18 15:04:37 +00:00
case 'admin/modules#description':
2005-04-01 15:55:02 +00:00
return t('Supports configurable user profiles.');
2005-01-28 20:49:00 +00:00
case 'admin/settings/profile':
2005-03-18 07:07:04 +00:00
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>');
2003-08-23 18:27:05 +00:00
}
}
2005-04-18 20:37:32 +00:00
/**
* 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();
2005-11-24 20:54:22 +00:00
$result = db_query('SELECT name, title, weight FROM {profile_fields} ORDER BY weight');
2005-04-18 20:37:32 +00:00
while ($record = db_fetch_object($result)) {
$fields[$record->name] = $record->title;
}
$fields['user_profile'] = t('Link to full user profile');
2005-10-11 19:44:35 +00:00
$form['profile_block_author_fields'] = array('#type' => 'checkboxes', '#title' => t('Profile fields to display'), '#default_value' => variable_get('profile_block_author_fields', NULL), '#options' => $fields, '#description' => 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'))));
2005-10-07 06:11:12 +00:00
return $form;
2005-04-18 20:37:32 +00:00
}
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)) {
2005-08-10 20:52:00 +00:00
$node = node_load(arg(1));
2005-04-18 20:37:32 +00:00
$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();
2005-11-24 20:54:22 +00:00
$result = db_query('SELECT name, title, type, visibility, weight FROM {profile_fields} WHERE visibility IN (%d, %d) ORDER BY weight', PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
while ($record = db_fetch_object($result)) {
// Ensure 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)) {
2005-04-18 20:37:32 +00:00
$fields[] = $record;
}
}
}
if ($fields) {
2005-12-03 17:12:56 +00:00
$fields = _profile_update_user_fields($fields, $account);
2005-04-18 20:37:32 +00:00
$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;
}
}
}
}
2004-04-21 13:56:38 +00:00
/**
2004-06-18 15:04:37 +00:00
* Implementation of hook_menu().
2004-04-21 13:56:38 +00:00
*/
2004-09-16 07:17:56 +00:00
function profile_menu($may_cache) {
2004-06-27 19:10:52 +00:00
global $user;
2004-06-18 15:04:37 +00:00
$items = array();
2004-09-16 07:17:56 +00:00
if ($may_cache) {
$items[] = array('path' => 'profile', 'title' => t('user list'),
'callback' => 'profile_browse',
2005-04-11 22:48:27 +00:00
'access' => user_access('access user profiles'),
2004-09-16 07:17:56 +00:00
'type' => MENU_SUGGESTED_ITEM);
2005-01-28 20:49:00 +00:00
$items[] = array('path' => 'admin/settings/profile', 'title' => t('profiles'),
2006-01-06 07:42:31 +00:00
'callback' => 'profile_admin_overview');
2005-01-28 20:49:00 +00:00
$items[] = array('path' => 'admin/settings/profile/add', 'title' => t('add field'),
2004-09-16 07:17:56 +00:00
'callback' => 'profile_admin_add',
'type' => MENU_CALLBACK);
2005-01-28 20:49:00 +00:00
$items[] = array('path' => 'admin/settings/profile/edit', 'title' => t('edit field'),
2004-09-16 07:17:56 +00:00
'callback' => 'profile_admin_edit',
'type' => MENU_CALLBACK);
2005-01-28 20:49:00 +00:00
$items[] = array('path' => 'admin/settings/profile/delete', 'title' => t('delete field'),
2004-09-16 07:17:56 +00:00
'callback' => 'profile_admin_delete',
'type' => MENU_CALLBACK);
}
2004-06-27 19:10:52 +00:00
2004-06-18 15:04:37 +00:00
return $items;
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
2004-06-18 15:04:37 +00:00
/**
* Menu callback; display a list of user information.
*/
2004-03-11 20:33:59 +00:00
function profile_browse() {
2003-12-26 23:03:21 +00:00
2004-08-14 11:54:31 +00:00
$name = arg(1);
2005-12-22 22:58:12 +00:00
list(,,$value) = explode('/', $_GET['q'], 3);
2004-02-07 16:59:34 +00:00
2004-08-14 11:54:31 +00:00
$field = db_fetch_object(db_query("SELECT DISTINCT(fid), type, title, page, visibility FROM {profile_fields} WHERE name = '%s'", $name));
2002-08-08 18:52:55 +00:00
2004-08-12 22:03:31 +00:00
if ($name && $field->fid) {
2004-08-14 11:54:31 +00:00
// Do not allow browsing of private fields by non-admins
if (!user_access('administer users') && $field->visibility == PROFILE_PRIVATE) {
drupal_access_denied();
return;
}
2004-06-01 21:58:46 +00:00
// Compile a list of fields to show
2004-03-11 20:33:59 +00:00
$fields = array();
2005-11-24 20:54:22 +00:00
$result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight', $field->fid, PROFILE_PUBLIC_LISTINGS);
2004-03-11 20:33:59 +00:00
while ($record = db_fetch_object($result)) {
$fields[] = $record;
}
2002-08-08 18:52:55 +00:00
2004-03-21 12:46:06 +00:00
// Determine what query to use:
2005-11-24 20:54:22 +00:00
$arguments = array($field->fid);
2004-03-21 12:46:06 +00:00
switch ($field->type) {
case 'checkbox':
$query = 'v.value = 1';
break;
case 'selection':
2005-11-24 20:54:22 +00:00
$query = "v.value = '%s'";
$arguments[] = $value;
2004-03-21 12:46:06 +00:00
break;
case 'list':
2005-11-24 20:54:22 +00:00
$query = "v.value LIKE '%%%s%%'";
$arguments[] = $value;
2004-03-21 12:46:06 +00:00
break;
2004-08-14 11:54:31 +00:00
default:
drupal_not_found();
return;
2004-03-21 12:46:06 +00:00
}
2004-03-11 20:33:59 +00:00
// Extract the affected users:
2005-11-24 20:54:22 +00:00
$result = pager_query("SELECT u.uid, u.access 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, $arguments);
2002-08-08 18:52:55 +00:00
2004-03-20 19:41:42 +00:00
$output = '<div id="profile">';
2004-03-11 20:33:59 +00:00
while ($account = db_fetch_object($result)) {
2005-09-06 20:39:10 +00:00
$account = user_load(array('uid' => $account->uid));
2005-12-03 17:12:56 +00:00
$fields = _profile_update_user_fields($fields, $account);
2005-09-06 20:39:10 +00:00
$output .= theme('profile_listing', $account, $fields);
2004-03-11 20:33:59 +00:00
}
$output .= theme('pager', NULL, 20);
2004-03-21 12:46:06 +00:00
if ($field->type == 'selection' || $field->type == 'list') {
2005-03-31 09:25:33 +00:00
$title = strtr($field->page, array('%value' => theme('placeholder', $value)));
2004-03-11 20:33:59 +00:00
}
else {
2004-03-20 19:41:42 +00:00
$title = $field->page;
2004-03-11 20:33:59 +00:00
}
2004-06-18 15:04:37 +00:00
$output .= '</div>';
2004-03-11 20:33:59 +00:00
2004-12-15 21:19:42 +00:00
drupal_set_title($title);
2005-04-24 16:34:36 +00:00
return $output;
2004-03-11 20:33:59 +00:00
}
2004-08-12 22:03:31 +00:00
else if ($name && !$field->id) {
2004-03-11 20:33:59 +00:00
drupal_not_found();
2002-08-08 18:52:55 +00:00
}
2004-08-12 22:03:31 +00:00
else {
// Compile a list of fields to show
$fields = array();
2006-01-10 12:22:18 +00:00
$result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE visibility = %d ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);
2004-08-12 22:03:31 +00:00
while ($record = db_fetch_object($result)) {
$fields[] = $record;
}
// Extract the affected users:
2006-01-13 15:54:05 +00:00
$result = pager_query("SELECT uid, access FROM {users} WHERE uid > 0 AND status != 0 ORDER BY access DESC", 20, 0, NULL);
2004-08-12 22:03:31 +00:00
$output = '<div id="profile">';
while ($account = db_fetch_object($result)) {
2005-09-06 20:39:10 +00:00
$account = user_load(array('uid' => $account->uid));
2005-12-03 17:12:56 +00:00
$fields = _profile_update_user_fields($fields, $account);
2005-09-06 20:39:10 +00:00
$output .= theme('profile_listing', $account, $fields);
2004-08-12 22:03:31 +00:00
}
$output .= '</div>';
$output .= theme('pager', NULL, 20);
2004-12-15 21:19:42 +00:00
drupal_set_title(t('user list'));
2005-04-24 16:34:36 +00:00
return $output;
2004-08-12 22:03:31 +00:00
}
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
function profile_load_profile(&$user) {
2004-08-14 11:54:31 +00:00
$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);
2004-03-11 20:33:59 +00:00
while ($field = db_fetch_object($result)) {
if (empty($user->{$field->name})) {
2004-08-14 11:54:31 +00:00
$user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
2004-03-11 20:33:59 +00:00
}
2002-10-22 18:46:43 +00:00
}
2002-08-08 18:52:55 +00:00
}
2004-06-27 19:10:52 +00:00
function profile_save_profile(&$edit, &$user, $category) {
2005-09-08 19:32:31 +00:00
if ($_GET['q'] == 'user/register' || $_GET['q'] == 'admin/user/create') {
2005-04-18 20:58:39 +00:00
$result = db_query('SELECT fid, name, type FROM {profile_fields} WHERE register = 1 AND visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
2004-09-19 13:28:11 +00:00
}
else {
2005-04-18 20:58:39 +00:00
$result = db_query("SELECT fid, name, type FROM {profile_fields} WHERE LOWER(category) = LOWER('%s') AND visibility != %d", $category, PROFILE_HIDDEN);
2004-12-07 17:42:35 +00:00
// We use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
2004-09-19 13:28:11 +00:00
}
2004-03-11 20:33:59 +00:00
while ($field = db_fetch_object($result)) {
2004-08-14 11:54:31 +00:00
if (_profile_field_serialize($field->type)) {
$edit[$field->name] = serialize($edit[$field->name]);
}
2004-06-27 19:10:52 +00:00
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]);
2004-10-16 16:59:59 +00:00
// Mark field as handled (prevents saving to user->data).
2005-04-18 20:37:32 +00:00
$edit[$field->name] = NULL;
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
}
2004-03-21 10:28:10 +00:00
function profile_view_field($user, $field) {
2004-08-14 11:54:31 +00:00
// Only allow browsing of private fields for admins
$browse = user_access('administer users') || $field->visibility != PROFILE_PRIVATE;
2004-03-21 10:28:10 +00:00
if ($value = $user->{$field->name}) {
switch ($field->type) {
case 'textfield':
2005-03-31 09:25:33 +00:00
return check_plain($value);
2004-03-21 10:28:10 +00:00
case 'textarea':
2005-07-29 21:06:33 +00:00
return check_markup($value);
2004-03-21 10:28:10 +00:00
case 'selection':
2005-12-22 22:58:12 +00:00
return $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
2004-03-21 10:28:10 +00:00
case 'checkbox':
2005-12-22 22:58:12 +00:00
return $browse ? l($field->title, 'profile/'. $field->name) : check_plain($field->title);
2004-03-21 10:28:10 +00:00
case 'url':
2005-03-31 09:25:33 +00:00
return '<a href="'. check_url($value) .'">'. check_plain($value) .'</a>';
2004-08-14 11:54:31 +00:00
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']),
2005-11-21 09:42:14 +00:00
'M' => map_month($value['month']),
2006-01-22 07:37:17 +00:00
'Y' => $value['year'],
'H:i' => null,
'g:ia' => null);
2004-08-14 11:54:31 +00:00
return strtr($format, $replace);
2004-06-27 19:10:52 +00:00
case 'list':
2004-07-08 15:17:21 +00:00
$values = split("[,\n\r]", $value);
2004-03-21 12:46:06 +00:00
$fields = array();
foreach ($values as $value) {
2004-08-14 11:54:31 +00:00
if ($value = trim($value)) {
2005-10-21 11:14:55 +00:00
$fields[] = $browse ? l($value, "profile/". drupal_urlencode($field->name) ."/". drupal_urlencode($value)) : check_plain($value);
2004-03-21 12:46:06 +00:00
}
}
return implode(', ', $fields);
2004-03-21 10:28:10 +00:00
}
}
}
2004-03-11 20:33:59 +00:00
function profile_view_profile($user) {
2002-08-08 18:52:55 +00:00
2004-03-29 18:19:10 +00:00
profile_load_profile($user);
2002-08-08 18:52:55 +00:00
2004-08-14 11:54:31 +00:00
// Show private fields to administrators and people viewing their own account.
if (user_access('administer users') || $GLOBALS['user']->uid == $user->uid) {
2005-04-18 20:58:39 +00:00
$result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
2004-08-14 11:54:31 +00:00
}
else {
2005-04-18 20:58:39 +00:00
$result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d AND visibility != %d ORDER BY category, weight', PROFILE_PRIVATE, PROFILE_HIDDEN);
2004-08-14 11:54:31 +00:00
}
2004-03-11 20:33:59 +00:00
while ($field = db_fetch_object($result)) {
2004-03-21 10:28:10 +00:00
if ($value = profile_view_field($user, $field)) {
2004-08-14 11:54:31 +00:00
$description = ($field->visibility == PROFILE_PRIVATE) ? t('The content of this field is private and only visible to yourself.') : '';
2005-03-31 09:25:33 +00:00
$title = ($field->type != 'checkbox') ? check_plain($field->title) : '';
2005-11-23 10:43:30 +00:00
$form = array('#title' => $title, '#value' => $value, '#description' => $description);
$fields[$field->category][$field->name] = theme('item', $form);
2002-08-08 18:52:55 +00:00
}
}
2004-03-21 14:28:15 +00:00
return $fields;
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
2004-06-27 19:10:52 +00:00
function _profile_form_explanation($field) {
$output = $field->explanation;
2004-03-11 20:33:59 +00:00
2004-06-27 19:10:52 +00:00
if ($field->type == 'list') {
2004-07-08 15:17:21 +00:00
$output .= ' '. t('Put each item on a separate line or separate them by commas. No HTML allowed.');
2004-06-27 19:10:52 +00:00
}
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) {
2005-09-08 19:32:31 +00:00
if ($_GET['q'] == 'user/register' || $_GET['q'] == 'admin/user/create') {
2004-09-19 13:28:11 +00:00
$result = db_query('SELECT * FROM {profile_fields} WHERE register = 1 ORDER BY category, weight');
}
else {
2004-12-07 17:42:35 +00:00
$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.
2004-09-19 13:28:11 +00:00
}
2005-11-13 08:33:44 +00:00
2004-03-11 20:33:59 +00:00
while ($field = db_fetch_object($result)) {
2004-09-19 13:28:11 +00:00
$category = $field->category;
2005-11-13 08:33:44 +00:00
if (!isset($fields[$category])) {
$fields[$category] = array('#type' => 'fieldset', '#title' => $category, '#weight' => $w++);
}
2004-03-11 20:33:59 +00:00
switch ($field->type) {
case 'textfield':
2005-10-07 06:51:43 +00:00
case 'url':
2005-11-12 11:26:16 +00:00
$fields[$category][$field->name] = array('#type' => 'textfield', '#title' => check_plain($field->title), '#default_value' => $edit[$field->name], '#maxlength' => 255, '#description' => _profile_form_explanation($field), '#required' => $field->required);
2004-03-11 20:33:59 +00:00
break;
2005-10-07 06:51:43 +00:00
case 'textarea':
2005-11-12 11:26:16 +00:00
$fields[$category][$field->name] = array('#type' => 'textarea', '#title' => check_plain($field->title), '#default_value' => $edit[$field->name], '#description' => _profile_form_explanation($field), '#required' => $field->required);
2004-03-21 12:46:06 +00:00
break;
case 'list':
2005-11-12 11:26:16 +00:00
$fields[$category][$field->name] = array('#type' => 'textarea', '#title' => check_plain($field->title), '#default_value' => $edit[$field->name], '#description' => _profile_form_explanation($field), '#required' => $field->required);
2004-03-11 20:33:59 +00:00
break;
2005-10-07 06:51:43 +00:00
case 'checkbox':
2005-11-12 11:26:16 +00:00
$fields[$category][$field->name] = array('#type' => 'checkbox', '#title' => check_plain($field->title), '#default_value' => $edit[$field->name], '#description' => _profile_form_explanation($field), '#required' => $field->required);
2004-03-11 20:33:59 +00:00
break;
case 'selection':
$options = array('--');
2004-07-08 15:17:21 +00:00
$lines = split("[,\n\r]", $field->options);
2004-03-11 20:33:59 +00:00
foreach ($lines as $line) {
if ($line = trim($line)) {
$options[$line] = $line;
}
}
2005-10-11 19:44:35 +00:00
$fields[$category][$field->name] = array('#type' => 'select', '#title' => check_plain($field->title), '#default_value' => $edit[$field->name], '#options' => $options, '#description' => _profile_form_explanation($field), '#required' => $field->required);
2004-03-11 20:33:59 +00:00
break;
2004-08-14 11:54:31 +00:00
case 'date':
2005-11-13 08:33:44 +00:00
$fields[$category][$field->name] = array('#type' => 'date', '#title' => check_plain($field->title), '#default_value' => $edit[$field->name], '#description' => _profile_form_explanation($field), '#required' => $field->required);
2004-08-14 11:54:31 +00:00
break;
2002-08-08 18:52:55 +00:00
}
}
2005-10-07 06:11:12 +00:00
return $fields;
2002-08-08 18:52:55 +00:00
}
2005-09-06 20:39:10 +00:00
/**
* Helper function: update an array of user fields by calling profile_view_field
*/
2005-12-03 17:12:56 +00:00
function _profile_update_user_fields($fields, $account) {
2005-09-06 20:39:10 +00:00
foreach ($fields as $key => $field) {
if ($value = profile_view_field($account, $field)) {
$fields[$key]->value = $value;
}
}
2005-12-03 17:12:56 +00:00
return $fields;
2005-09-06 20:39:10 +00:00
}
2004-06-27 19:10:52 +00:00
function profile_validate_profile($edit, $category) {
2004-09-19 13:28:11 +00:00
2005-09-08 19:32:31 +00:00
if ($_GET['q'] == 'user/register' || $_GET['q'] == 'admin/user/create') {
2004-09-19 13:28:11 +00:00
$result = db_query('SELECT * FROM {profile_fields} WHERE register = 1 ORDER BY category, weight');
}
else {
2004-12-07 17:42:35 +00:00
$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.
2004-09-19 13:28:11 +00:00
}
2004-03-21 10:28:10 +00:00
while ($field = db_fetch_object($result)) {
2004-03-27 11:56:01 +00:00
if ($edit[$field->name]) {
2004-07-08 15:17:21 +00:00
if ($field->type == 'url') {
if (!valid_url($edit[$field->name], true)) {
2005-03-31 09:25:33 +00:00
form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => theme('placeholder', $field->title))));
}
2004-03-21 10:28:10 +00:00
}
}
2004-06-27 19:10:52 +00:00
else if ($field->required && !user_access('administer users')) {
2005-03-31 09:25:33 +00:00
form_set_error($field->name, t('The field %field is required.', array('%field' => theme('placeholder', $field->title))));
2004-03-27 14:50:56 +00:00
}
2004-03-21 10:28:10 +00:00
}
return $edit;
}
2004-06-27 19:10:52 +00:00
function profile_categories() {
$result = db_query("SELECT DISTINCT(category) FROM {profile_fields}");
while ($category = db_fetch_object($result)) {
2005-03-31 09:25:33 +00:00
$data[] = array('name' => check_plain($category->category), 'title' => $category->category, 'weight' => 3);
2004-06-27 19:10:52 +00:00
}
return $data;
}
2004-06-18 15:04:37 +00:00
/**
* Implementation of hook_user().
*/
2004-06-27 19:10:52 +00:00
function profile_user($type, &$edit, &$user, $category = NULL) {
2004-03-11 20:33:59 +00:00
switch ($type) {
case 'load':
return profile_load_profile($user);
2004-09-19 13:28:11 +00:00
case 'register':
return profile_form_profile($edit, $user, $category);
2004-03-11 20:33:59 +00:00
case 'update':
2004-04-29 23:27:15 +00:00
case 'insert':
2004-06-27 19:10:52 +00:00
return profile_save_profile($edit, $user, $category);
2004-03-11 20:33:59 +00:00
case 'view':
return profile_view_profile($user);
2004-04-29 22:39:55 +00:00
case 'form':
2004-06-27 19:10:52 +00:00
return profile_form_profile($edit, $user, $category);
2004-03-11 20:33:59 +00:00
case 'validate':
2004-06-27 19:10:52 +00:00
return profile_validate_profile($edit, $category);
case 'categories':
return profile_categories();
2004-03-11 20:33:59 +00:00
}
}
2003-04-29 20:31:21 +00:00
2004-03-11 20:33:59 +00:00
function profile_validate_form($edit) {
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
// Validate the title:
if (!$edit['title']) {
2004-05-31 09:40:56 +00:00
form_set_error('title', t('You must enter a title.'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
// Validate the 'form name':
if (eregi('[^a-z0-9_-]', $edit['name'])) {
2004-05-31 09:40:56 +00:00
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.'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
if (in_array($edit['name'], user_fields())) {
2004-05-31 09:40:56 +00:00
form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
// Validate the category:
if (!$edit['category']) {
2004-05-31 09:40:56 +00:00
form_set_error('category', t('You must enter a category.'));
2004-03-11 20:33:59 +00:00
}
2005-12-15 16:34:48 +00:00
if ($edit['category'] == 'account') {
form_set_error('category', t('The specified category name is reserved for use by Drupal.'));
}
2002-08-08 18:52:55 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; adds a new field to all user profiles.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_add($type) {
if ($_POST['op']) {
$data = $_POST['edit'];
2002-08-08 18:52:55 +00:00
2004-05-31 09:40:56 +00:00
// Validate the form:
profile_validate_form($data);
2005-01-24 21:39:58 +00:00
if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s' AND category = '%s'", $data['title'], $data['category']))) {
2004-08-14 11:54:31 +00:00
form_set_error('title', t('The specified title is already in use.'));
2004-05-28 20:02:11 +00:00
}
2004-05-31 09:40:56 +00:00
if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) {
2004-08-14 11:54:31 +00:00
form_set_error('name', t('The specified name is already in use.'));
2004-05-28 20:02:11 +00:00
}
2004-05-31 09:40:56 +00:00
2004-07-04 16:50:02 +00:00
if (!form_get_errors()) {
2004-09-19 13:28:11 +00:00
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']);
2002-08-08 18:52:55 +00:00
2004-09-19 13:33:08 +00:00
cache_clear_all();
2004-08-14 11:54:31 +00:00
drupal_set_message(t('The field has been created.'));
2005-01-28 20:49:00 +00:00
drupal_goto('admin/settings/profile');
2004-03-11 20:33:59 +00:00
}
2003-12-26 23:03:21 +00:00
}
2004-03-11 20:33:59 +00:00
else {
$data = array('name' => 'profile_');
}
2004-12-15 21:19:42 +00:00
drupal_set_title(t('Add new %type', array('%type' => _profile_field_types($type))));
2005-04-24 16:34:36 +00:00
return _profile_field_form($type, $data);
2003-12-26 23:03:21 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; displays the profile field editing form.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_edit($fid) {
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
if ($_POST['op']) {
$data = $_POST['edit'];
2002-08-08 18:52:55 +00:00
2004-05-31 09:40:56 +00:00
// Validate form:
profile_validate_form($data);
2002-08-08 18:52:55 +00:00
2004-07-04 16:50:02 +00:00
if (!form_get_errors()) {
2004-09-19 13:28:11 +00:00
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);
2004-03-11 20:33:59 +00:00
2004-09-19 13:33:08 +00:00
cache_clear_all();
2004-08-14 11:54:31 +00:00
drupal_set_message(t('The field has been updated.'));
2005-01-28 20:49:00 +00:00
drupal_goto('admin/settings/profile');
2004-03-11 20:33:59 +00:00
}
2003-12-26 23:03:21 +00:00
}
else {
2004-03-11 20:33:59 +00:00
$data = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
2002-08-08 18:52:55 +00:00
}
2004-12-15 21:19:42 +00:00
drupal_set_title(t('Edit %type', array('%type' => $data['type'])));
2005-04-24 16:34:36 +00:00
return _profile_field_form($data['type'], $data);
2002-08-08 18:52:55 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; deletes a field from all user profiles.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_delete($fid) {
2005-05-01 09:41:23 +00:00
$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);
2006-01-08 12:06:13 +00:00
db_query('DELETE FROM {profile_values} WHERE fid = %d', $fid);
2005-05-01 09:41:23 +00:00
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 {
2006-01-08 12:06:13 +00:00
return confirm_form('profile_confirm_delete', $form,
t('Are you sure you want to delete the field %field?', array('%field' => theme('placeholder', $field->title))),
'admin/settings/profile',
t('This action cannot be undone. If users have entered values into this field in their profile, these entries will also be deleted. If you want to keep the user-entered data, instead of deleting the field you may wish to <a href="%edit-field">edit this field</a> and change it to a \'hidden profile field\' so that it may only be accessed by administrators.', array('%edit-field' => url('admin/settings/profile/edit/' . $fid))),
t('Delete'),
t('Cancel'));
2005-05-01 09:41:23 +00:00
}
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
function _profile_field_form($type, $edit = array()) {
2005-10-07 06:51:43 +00:00
2005-10-11 19:44:35 +00:00
$form['fields'] = array('#type' => 'fieldset', '#title' => t('Field settings'));
2005-11-12 11:26:16 +00:00
$form['fields']['category'] = array('#type' => 'textfield', '#title' => t('Category'), '#default_value' => $edit['category'], '#description' => t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'));
$form['fields']['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $edit['title'], '#description' => t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'));
$form['fields']['name'] = array('#type' => 'textfield', '#title' => t('Form name'), '#default_value' => $edit['name'], '#description' => t('The name of the field. The form name is not shown to the user but used internally in the HTML code and URLs.
2004-06-18 15:04:37 +00:00
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".'));
2005-11-12 11:26:16 +00:00
$form['fields']['explanation'] = array('#type' => 'textarea', '#title' => t('Explanation'), '#default_value' => $edit['explanation'], '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'));
2004-03-20 19:41:42 +00:00
if ($type == 'selection') {
2005-11-12 11:26:16 +00:00
$form['fields']['options'] = array('#type' => 'textarea', '#title' => t('Selection options'), '#default_value' => $edit['options'], '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'));
2004-03-20 19:41:42 +00:00
}
2005-10-11 19:44:35 +00:00
$form['fields']['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#default_value' => $edit['weight'], '#delta' => 5, '#description' => t('The weights define the order in which the form fields are shown. Lighter fields "float up" towards the top of the category.'));
$form['fields']['visibility'] = array('#type' => 'radios', '#title' => t('Visibility'), '#default_value' => $edit['visibility'], '#options' => 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.')));
2004-03-21 12:46:06 +00:00
if ($type == 'selection' || $type == 'list') {
2005-11-12 11:26:16 +00:00
$form['fields']['page'] = array('#type' => 'textfield', '#title' => t('Page title'), '#default_value' => $edit['page'], '#description' => 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.'));
2004-03-20 19:41:42 +00:00
}
else {
2005-11-12 11:26:16 +00:00
$form['fields']['page'] = array('#type' => 'textfield', '#title' => t('Page title'), '#default_value' => $edit['page'], '#description' => 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.'));
2002-08-08 18:52:55 +00:00
}
2005-11-12 11:26:16 +00:00
$form['fields']['required'] = array('#type' => 'checkbox', '#title' => t('The user must enter a value.'), '#default_value' => $edit['required']);
$form['fields']['register'] = array('#type' => 'checkbox', '#title' => t('Visible in user registration form.'), '#default_value' => $edit['register']);
2005-10-11 19:44:35 +00:00
$form['submit'] = array('#type' => 'submit', '#value' => t('Save field'));
2004-03-11 20:33:59 +00:00
2005-10-07 06:11:12 +00:00
return drupal_get_form('_profile_field_form', $form);
2004-03-11 20:33:59 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; display a listing of all editable profile fields.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_overview() {
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
2004-08-14 11:54:31 +00:00
$rows = array();
2004-03-11 20:33:59 +00:00
while ($field = db_fetch_object($result)) {
2005-03-31 09:25:33 +00:00
$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"));
2004-08-14 11:54:31 +00:00
}
if (count($rows) == 0) {
$rows[] = array(array('data' => t('No fields defined.'), 'colspan' => '6'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
2004-08-19 15:41:57 +00:00
$header = array(t('Title'), t('Name'), t('Type'), t('Category'), array('data' => t('Operations'), 'colspan' => '2'));
2004-03-11 20:33:59 +00:00
$output = theme('table', $header, $rows);
2004-08-14 11:54:31 +00:00
$output .= '<h2>'. t('Add new field') .'</h2>';
2004-03-11 20:33:59 +00:00
$output .= '<ul>';
foreach (_profile_field_types() as $key => $value) {
2005-01-28 20:49:00 +00:00
$output .= '<li>'. l($value, "admin/settings/profile/add/$key") .'</li>';
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
$output .= '</ul>';
2005-04-24 16:34:36 +00:00
return $output;
2002-08-08 18:52:55 +00:00
}
2005-09-06 20:39:10 +00:00
function theme_profile_block($account, $fields = array()) {
2005-04-18 20:37:32 +00:00
2005-09-06 20:39:10 +00:00
$output .= theme('user_picture', $account);
2005-04-18 20:37:32 +00:00
foreach ($fields as $field) {
2005-09-06 20:39:10 +00:00
if ($field->value) {
2005-09-19 14:59:44 +00:00
$output .= "<p><strong>$field->title:</strong><br />$field->value</p>\n";
2005-04-18 20:37:32 +00:00
}
}
return $output;
}
2005-09-06 20:39:10 +00:00
function theme_profile_listing($account, $fields = array()) {
2004-03-11 20:33:59 +00:00
$output = "<div class=\"profile\">\n";
2005-09-06 20:39:10 +00:00
$output .= theme('user_picture', $account);
$output .= ' <div class="name">'. theme('username', $account) ."</div>\n";
2004-03-11 20:33:59 +00:00
foreach ($fields as $field) {
2005-09-06 20:39:10 +00:00
if ($field->value) {
2005-11-24 20:54:22 +00:00
$output .= " <div class=\"field\">$field->value</div>\n";
2002-08-08 18:52:55 +00:00
}
}
2004-03-11 20:33:59 +00:00
$output .= "</div>\n";
return $output;
}
function _profile_field_types($type = NULL) {
2004-08-14 11:54:31 +00:00
$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'));
2004-03-11 20:33:59 +00:00
return isset($type) ? $types[$type] : $types;
2002-08-08 18:52:55 +00:00
}
2004-08-14 11:54:31 +00:00
function _profile_field_serialize($type = NULL) {
return $type == 'date';
}
2005-08-25 21:14:17 +00:00