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
/**
2006-12-07 16:53:56 +00:00
* Private field, content only available to privileged users.
2004-06-27 19:10:52 +00:00
*/
define('PROFILE_PRIVATE', 1);
2006-12-07 16:53:56 +00:00
/**
* Public field, content shown on profile page but not used on member list pages.
*/
2004-06-27 19:10:52 +00:00
define('PROFILE_PUBLIC', 2);
2006-12-07 16:53:56 +00:00
/**
* Public field, content shown on profile page and on member list pages.
*/
2004-06-27 19:10:52 +00:00
define('PROFILE_PUBLIC_LISTINGS', 3);
2006-12-07 16:53:56 +00:00
/**
* Hidden profile field, only accessible by administrators, modules and themes.
*/
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
/**
2009-05-27 18:34:03 +00:00
* Implement hook_help().
2004-06-18 15:04:37 +00:00
*/
2007-06-30 19:46:58 +00:00
function profile_help($path, $arg) {
switch ($path) {
2005-11-01 10:17:34 +00:00
case 'admin/help#profile':
2008-04-14 17:48:46 +00:00
$output = '<p>' . t('The profile module allows custom fields (such as country, full name, or age) to be defined and displayed in the <em>My Account</em> section. This permits users of a site to share more information about themselves, and can help community-based sites organize users around specific information.') . '</p>';
$output .= '<p>' . t('The following types of fields can be added to a user profile:') . '</p>';
$output .= '<ul><li>' . t('single-line textfield') . '</li>';
$output .= '<li>' . t('multi-line textfield') . '</li>';
$output .= '<li>' . t('checkbox') . '</li>';
$output .= '<li>' . t('list selection') . '</li>';
$output .= '<li>' . t('freeform list') . '</li>';
$output .= '<li>' . t('URL') . '</li>';
$output .= '<li>' . t('date') . '</li></ul>';
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@profile">Profile module</a>.', array('@profile' => 'http://drupal.org/handbook/modules/profile/')) . '</p>';
2005-11-01 10:17:34 +00:00
return $output;
2009-08-21 14:27:47 +00:00
case 'admin/config/people/profile':
2009-08-02 15:44:08 +00:00
return '<p>' . t("This page displays a list of the existing custom profile fields to be displayed on a user's <em>My Account</em> page. To provide structure, similar or related fields may be placed inside a category. To add a new category (or edit an existing one), edit a profile field and provide a new category name. Remember that your changes will not be saved until you click the <em>Save configuration</em> button at the bottom of the page.") . '</p>';
2003-08-23 18:27:05 +00:00
}
}
2007-04-06 13:27:23 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_theme().
2007-04-06 13:27:23 +00:00
*/
function profile_theme() {
return array(
'profile_block' => array(
'arguments' => array('account' => NULL, 'fields' => array()),
2007-08-26 07:46:11 +00:00
'template' => 'profile-block',
2007-04-06 13:27:23 +00:00
),
'profile_listing' => array(
'arguments' => array('account' => NULL, 'fields' => array()),
2007-08-26 07:46:11 +00:00
'template' => 'profile-listing',
2007-08-02 10:36:42 +00:00
),
'profile_wrapper' => array(
'arguments' => array('content' => NULL),
2007-08-26 07:46:11 +00:00
'template' => 'profile-wrapper',
2007-11-30 09:02:51 +00:00
),
'profile_admin_overview' => array(
'arguments' => array('form' => NULL),
2009-05-21 23:07:16 +00:00
'file' => 'profile.admin.inc',
2007-08-02 10:36:42 +00:00
)
);
2007-04-06 13:27:23 +00:00
}
2006-03-12 14:08:55 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_menu().
2006-03-12 14:08:55 +00:00
*/
2007-01-24 14:48:36 +00:00
function profile_menu() {
$items['profile'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'User list',
2007-01-24 14:48:36 +00:00
'page callback' => 'profile_browse',
'access arguments' => array('access user profiles'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.pages.inc',
2007-01-24 14:48:36 +00:00
'type' => MENU_SUGGESTED_ITEM,
);
2009-08-21 14:27:47 +00:00
$items['admin/config/people/profile'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Profiles',
'description' => 'Create customizable fields for your users.',
2007-11-30 09:02:51 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('profile_admin_overview'),
2007-12-26 19:02:24 +00:00
'access arguments' => array('administer users'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-08-21 14:27:47 +00:00
$items['admin/config/people/profile/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add field',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('profile_field_form'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer users'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.admin.inc',
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2009-08-21 14:27:47 +00:00
$items['admin/config/people/profile/autocomplete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Profile category autocomplete',
2007-01-24 14:48:36 +00:00
'page callback' => 'profile_admin_settings_autocomplete',
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer users'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.admin.inc',
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2009-08-21 14:27:47 +00:00
$items['admin/config/people/profile/edit'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit field',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('profile_field_form'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer users'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.admin.inc',
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2009-08-21 14:27:47 +00:00
$items['admin/config/people/profile/delete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Delete field',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('profile_field_delete'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer users'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.admin.inc',
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
$items['profile/autocomplete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Profile autocomplete',
2007-01-24 14:48:36 +00:00
'page callback' => 'profile_autocomplete',
2007-02-02 15:25:25 +00:00
'access arguments' => array('access user profiles'),
2009-08-24 00:14:23 +00:00
'file' => 'profile.pages.inc',
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2006-03-12 14:08:55 +00:00
return $items;
}
2005-04-18 20:37:32 +00:00
/**
2009-08-29 05:46:04 +00:00
* Implement hook_block_info().
2005-04-18 20:37:32 +00:00
*/
2009-08-29 05:46:04 +00:00
function profile_block_info() {
2008-12-16 23:57:33 +00:00
$blocks['author-information']['info'] = t('Author information');
2009-08-31 17:06:10 +00:00
$blocks['author-information']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE;
2008-12-16 23:57:33 +00:00
return $blocks;
}
2005-04-18 20:37:32 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_configure().
2008-12-16 23:57:33 +00:00
*/
function profile_block_configure($delta = '') {
// Compile a list of fields to show
$fields = array();
2009-05-26 10:41:06 +00:00
$result = db_query('SELECT name, title, weight, visibility FROM {profile_field} WHERE visibility IN (:visibility) ORDER BY weight', array(':visibility' => array(PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS)));
foreach ($result as $record) {
2008-12-16 23:57:33 +00:00
$fields[$record->name] = check_plain($record->title);
2005-04-18 20:37:32 +00:00
}
2008-12-16 23:57:33 +00:00
$fields['user_profile'] = t('Link to full user profile');
$form['profile_block_author_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Profile fields to display'),
'#default_value' => variable_get('profile_block_author_fields', array()),
'#options' => $fields,
2009-08-21 14:27:47 +00:00
'#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/config/people/profile'))),
2008-12-16 23:57:33 +00:00
);
return $form;
}
2005-04-18 20:37:32 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_save().
2008-12-16 23:57:33 +00:00
*/
function profile_block_save($delta = '', $edit = array()) {
variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
}
2005-04-18 20:37:32 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_block_view().
2008-12-16 23:57:33 +00:00
*/
function profile_block_view($delta = '') {
if (user_access('access user profiles')) {
$output = '';
if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
$node = node_load(arg(1));
$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();
2009-05-26 10:41:06 +00:00
$result = db_query('SELECT name, title, weight, visibility FROM {profile_field} WHERE visibility IN (:visibility) ORDER BY weight', array(':visibility' => array(PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS)));
foreach ($result as $record) {
2008-12-16 23:57:33 +00:00
// 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 (isset($use_fields[$record->name]) && $use_fields[$record->name]) {
$fields[] = $record;
}
2005-04-18 20:37:32 +00:00
}
}
2008-12-16 23:57:33 +00:00
if (!empty($fields)) {
$profile = _profile_update_user_fields($fields, $account);
2009-10-09 01:00:08 +00:00
$output .= theme('profile_block', array('account' => $account, 'fields' => $profile));
2008-12-16 23:57:33 +00:00
}
if (isset($use_fields['user_profile']) && $use_fields['user_profile']) {
$output .= '<div>' . l(t('View full user profile'), 'user/' . $account->uid) . '</div>';
2005-04-18 20:37:32 +00:00
}
}
2008-12-16 23:57:33 +00:00
if ($output) {
$block['subject'] = t('About %name', array('%name' => $account->name));
$block['content'] = $output;
return $block;
}
2005-04-18 20:37:32 +00:00
}
}
2008-10-06 11:30:12 +00:00
/**
2009-10-10 16:48:39 +00:00
* Implement hook_user_presave().
2008-10-06 11:30:12 +00:00
*/
2009-10-10 16:48:39 +00:00
function profile_user_presave(&$edit, $account, $category) {
if ($account->uid) {
profile_save_profile($edit, $account, $category);
}
2008-10-06 11:30:12 +00:00
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_user_insert().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function profile_user_insert(&$edit, $account, $category) {
2009-10-10 16:48:39 +00:00
profile_save_profile($edit, $account, $category, TRUE);
2008-10-06 11:30:12 +00:00
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_user_cancel().
2008-10-06 11:30:12 +00:00
*/
2009-08-12 12:36:05 +00:00
function profile_user_cancel(&$edit, $account, $method) {
2009-01-08 08:42:13 +00:00
switch ($method) {
case 'user_cancel_reassign':
case 'user_cancel_delete':
2009-05-26 10:41:06 +00:00
db_delete('profile_value')
->condition('uid', $account->uid)
->execute();
2009-01-08 08:42:13 +00:00
break;
}
2006-03-12 14:08:55 +00:00
}
2004-09-16 07:17:56 +00:00
2009-03-14 23:01:38 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_user_load().
2009-03-14 23:01:38 +00:00
*/
function profile_user_load($users) {
$result = db_query('SELECT f.name, f.type, v.uid, v.value FROM {profile_field} f INNER JOIN {profile_value} v ON f.fid = v.fid WHERE uid IN (:uids)', array(':uids' => array_keys($users)));
foreach ($result as $record) {
if (empty($users[$record->uid]->{$record->name})) {
$users[$record->uid]->{$record->name} = _profile_field_serialize($record->type) ? unserialize($record->value) : $record->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
}
2009-08-12 12:36:05 +00:00
function profile_save_profile(&$edit, $account, $category, $register = FALSE) {
2007-03-30 09:38:13 +00:00
$result = _profile_get_fields($category, $register);
2009-05-26 10:41:06 +00:00
foreach ($result as $field) {
2004-08-14 11:54:31 +00:00
if (_profile_field_serialize($field->type)) {
2007-12-08 14:06:23 +00:00
$edit[$field->name] = serialize($edit[$field->name]);
2004-08-14 11:54:31 +00:00
}
2009-05-26 10:41:06 +00:00
db_merge('profile_value')
->key(array(
'fid' => $field->fid,
2009-08-12 12:36:05 +00:00
'uid' => $account->uid,
2009-05-26 10:41:06 +00:00
))
->fields(array('value' => $edit[$field->name]))
->execute();
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
}
2009-08-12 12:36:05 +00:00
function profile_view_field($account, $field) {
2006-04-04 06:56:34 +00:00
// Only allow browsing of private fields for admins, if browsing is enabled,
// and if a user has permission to view profiles. Note that this check is
// necessary because a user may always see their own profile.
$browse = user_access('access user profiles')
2009-05-24 17:39:35 +00:00
&& (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
&& !empty($field->page);
2004-08-14 11:54:31 +00:00
2009-08-12 12:36:05 +00:00
if (isset($account->{$field->name}) && $value = $account->{$field->name}) {
2004-03-21 10:28:10 +00:00
switch ($field->type) {
case 'textarea':
2009-10-10 21:39:03 +00:00
return check_markup($value, filter_default_format($account), '', TRUE);
2006-04-04 06:56:34 +00:00
case 'textfield':
2004-03-21 10:28:10 +00:00
case 'selection':
2008-04-14 17:48:46 +00:00
return $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
2004-03-21 10:28:10 +00:00
case 'checkbox':
2008-04-14 17:48:46 +00:00
return $browse ? l($field->title, 'profile/' . $field->name) : check_plain($field->title);
2004-03-21 10:28:10 +00:00
case 'url':
2008-04-14 17:48:46 +00:00
return '<a href="' . check_url($value) . '">' . check_plain($value) . '</a>';
2004-08-14 11:54:31 +00:00
case 'date':
2006-04-04 06:52:59 +00:00
$format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
2006-03-12 14:08:55 +00:00
// Note: Avoid PHP's date() because it does not handle dates before
2004-08-14 11:54:31 +00:00
// 1970 on Windows. This would make the date field useless for e.g.
// birthdays.
2007-12-23 13:17:20 +00:00
$replace = array(
'd' => sprintf('%02d', $value['day']),
'j' => $value['day'],
'm' => sprintf('%02d', $value['month']),
'M' => map_month($value['month']),
'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':
2008-09-05 09:25:52 +00:00
$values = preg_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)) {
2008-04-14 17:48:46 +00:00
$fields[] = $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
2004-03-21 12:46:06 +00:00
}
}
return implode(', ', $fields);
2004-03-21 10:28:10 +00:00
}
}
}
2009-08-12 12:36:05 +00:00
/**
* Implement hook_user_view().
*/
function profile_user_view($account) {
2004-08-14 11:54:31 +00:00
// Show private fields to administrators and people viewing their own account.
2009-08-12 12:36:05 +00:00
if (user_access('administer users') || $GLOBALS['user']->uid == $account->uid) {
2009-07-28 19:18:08 +00:00
$result = db_query('SELECT * FROM {profile_field} WHERE visibility <> :hidden ORDER BY category, weight', array(':hidden' => PROFILE_HIDDEN));
2004-08-14 11:54:31 +00:00
}
else {
2009-05-26 10:41:06 +00:00
$result = db_query('SELECT * FROM {profile_field} WHERE visibility <> :private AND visibility <> :hidden ORDER BY category, weight', array(':private' => PROFILE_PRIVATE, ':hidden' => PROFILE_HIDDEN));
2004-08-14 11:54:31 +00:00
}
2007-01-31 15:49:26 +00:00
$fields = array();
2009-05-26 10:41:06 +00:00
foreach ($result as $field) {
2009-08-12 12:36:05 +00:00
if ($value = profile_view_field($account, $field)) {
2006-02-16 15:29:46 +00:00
$title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
2007-06-21 04:38:41 +00:00
// Create a single fieldset for each category.
2009-08-12 12:36:05 +00:00
if (!isset($account->content[$field->category])) {
$account->content[$field->category] = array(
2007-06-21 04:38:41 +00:00
'#type' => 'user_profile_category',
2008-02-03 19:36:46 +00:00
'#title' => $field->category,
2007-06-21 04:38:41 +00:00
);
}
2009-08-12 12:36:05 +00:00
$account->content[$field->category][$field->name] = array(
2007-06-21 04:38:41 +00:00
'#type' => 'user_profile_item',
2007-11-20 11:39:48 +00:00
'#title' => $title,
2008-07-16 21:59:29 +00:00
'#markup' => $value,
2007-06-21 04:38:41 +00:00
'#weight' => $field->weight,
2009-08-22 14:34:23 +00:00
'#attributes' => array('class' => array('profile-' . $field->name)),
2006-01-26 13:43:04 +00:00
);
2002-08-08 18:52:55 +00:00
}
}
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') {
2008-04-14 17:48:46 +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) {
2008-04-14 17:48:46 +00:00
$output .= ' ' . t('The content of this field is kept private and will not be shown publicly.');
2004-06-27 19:10:52 +00:00
}
return $output;
}
2009-09-22 07:50:16 +00:00
/**
* Implement hook_form_alter().
*/
function profile_form_alter(&$form, &$form_state, $form_id) {
2009-10-10 16:48:39 +00:00
if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
return;
2009-09-22 07:50:16 +00:00
}
2009-10-10 16:48:39 +00:00
$form['#validate'][] = 'profile_user_form_validate';
$account = $form['#user'];
$result = _profile_get_fields($form['#user_category'], $form['#user_category'] == 'register');
2007-07-05 08:38:58 +00:00
$weight = 1;
2009-05-26 10:41:06 +00:00
foreach ($result as $field) {
2004-09-19 13:28:11 +00:00
$category = $field->category;
2009-10-10 16:48:39 +00:00
if (!isset($form[$category])) {
$form[$category] = array('#type' => 'fieldset', '#title' => check_plain($category), '#weight' => $weight++);
2005-11-13 08:33:44 +00:00
}
2004-03-11 20:33:59 +00:00
switch ($field->type) {
case 'textfield':
2005-10-07 06:51:43 +00:00
case 'url':
2009-10-10 16:48:39 +00:00
$form[$category][$field->name] = array(
'#type' => 'textfield',
2006-02-21 08:41:42 +00:00
'#title' => check_plain($field->title),
2009-09-22 07:50:16 +00:00
'#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
2006-02-21 08:41:42 +00:00
'#maxlength' => 255,
'#description' => _profile_form_explanation($field),
2006-03-12 14:08:55 +00:00
'#required' => $field->required,
2006-02-21 08:41:42 +00:00
);
2006-05-12 08:50:22 +00:00
if ($field->autocomplete) {
2009-10-10 16:48:39 +00:00
$form[$category][$field->name]['#autocomplete_path'] = "profile/autocomplete/" . $field->fid;
2006-05-12 08:50:22 +00:00
}
2004-03-11 20:33:59 +00:00
break;
2009-10-10 16:48:39 +00:00
2005-10-07 06:51:43 +00:00
case 'textarea':
2009-10-10 16:48:39 +00:00
$form[$category][$field->name] = array(
'#type' => 'textarea',
2006-02-21 08:41:42 +00:00
'#title' => check_plain($field->title),
2009-09-22 07:50:16 +00:00
'#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
2006-02-21 08:41:42 +00:00
'#description' => _profile_form_explanation($field),
2006-03-12 14:08:55 +00:00
'#required' => $field->required,
2006-02-21 08:41:42 +00:00
);
2004-03-21 12:46:06 +00:00
break;
2009-10-10 16:48:39 +00:00
2004-03-21 12:46:06 +00:00
case 'list':
2009-10-10 16:48:39 +00:00
$form[$category][$field->name] = array(
'#type' => 'textarea',
2006-02-21 08:41:42 +00:00
'#title' => check_plain($field->title),
2009-09-22 07:50:16 +00:00
'#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
2006-02-21 08:41:42 +00:00
'#description' => _profile_form_explanation($field),
2006-03-12 14:08:55 +00:00
'#required' => $field->required,
2006-02-21 08:41:42 +00:00
);
2004-03-11 20:33:59 +00:00
break;
2009-10-10 16:48:39 +00:00
2005-10-07 06:51:43 +00:00
case 'checkbox':
2009-10-10 16:48:39 +00:00
$form[$category][$field->name] = array(
'#type' => 'checkbox',
2006-02-21 08:41:42 +00:00
'#title' => check_plain($field->title),
2009-09-22 07:50:16 +00:00
'#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
2006-02-21 08:41:42 +00:00
'#description' => _profile_form_explanation($field),
2006-03-12 14:08:55 +00:00
'#required' => $field->required,
2006-02-21 08:41:42 +00:00
);
2004-03-11 20:33:59 +00:00
break;
2009-10-10 16:48:39 +00:00
2004-03-11 20:33:59 +00:00
case 'selection':
2006-04-13 13:21:15 +00:00
$options = $field->required ? array() : array('--');
2009-01-11 10:49:21 +00:00
$lines = preg_split("/[\n\r]/", $field->options);
2004-03-11 20:33:59 +00:00
foreach ($lines as $line) {
if ($line = trim($line)) {
$options[$line] = $line;
}
}
2009-10-10 16:48:39 +00:00
$form[$category][$field->name] = array(
'#type' => 'select',
2006-02-21 08:41:42 +00:00
'#title' => check_plain($field->title),
2009-09-22 07:50:16 +00:00
'#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
2006-02-21 08:41:42 +00:00
'#options' => $options,
'#description' => _profile_form_explanation($field),
2006-03-12 14:08:55 +00:00
'#required' => $field->required,
2006-02-21 08:41:42 +00:00
);
2004-03-11 20:33:59 +00:00
break;
2009-10-10 16:48:39 +00:00
2004-08-14 11:54:31 +00:00
case 'date':
2009-10-10 16:48:39 +00:00
$form[$category][$field->name] = array(
'#type' => 'date',
2006-02-21 08:41:42 +00:00
'#title' => check_plain($field->title),
2009-09-22 07:50:16 +00:00
'#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
2006-02-21 08:41:42 +00:00
'#description' => _profile_form_explanation($field),
2006-03-12 14:08:55 +00:00
'#required' => $field->required,
2006-02-21 08:41:42 +00:00
);
2004-08-14 11:54:31 +00:00
break;
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) {
2006-04-04 23:04:58 +00:00
$fields[$key]->value = profile_view_field($account, $field);
2005-09-06 20:39:10 +00:00
}
2005-12-03 17:12:56 +00:00
return $fields;
2005-09-06 20:39:10 +00:00
}
2009-08-12 12:36:05 +00:00
/**
2009-10-10 16:48:39 +00:00
* Form validation handler for the user register/profile form.
*
* @see profile_form_alter()
2009-08-12 12:36:05 +00:00
*/
2009-10-10 16:48:39 +00:00
function profile_user_form_validate($form, &$form_state) {
$result = _profile_get_fields($form['#user_category'], $form['#user_category'] == 'register');
2009-05-26 10:41:06 +00:00
foreach ($result as $field) {
2009-10-10 16:48:39 +00:00
if (!empty($form_state['values'][$field->name])) {
if ($field->type == 'url' && !valid_url($form_state['values'][$field->name], TRUE)) {
form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => $field->title)));
2004-03-21 10:28:10 +00:00
}
}
2008-10-12 04:30:09 +00:00
elseif ($field->required && !user_access('administer users')) {
2006-08-18 12:17:00 +00:00
form_set_error($field->name, t('The field %field is required.', array('%field' => $field->title)));
2004-03-27 14:50:56 +00:00
}
2004-03-21 10:28:10 +00:00
}
}
2009-10-10 16:48:39 +00:00
2009-06-03 07:28:28 +00:00
/**
* Implement hook_user_categories().
*/
function profile_user_categories() {
2008-12-05 12:50:28 +00:00
$result = db_query("SELECT DISTINCT(category) FROM {profile_field}");
2007-01-31 15:49:26 +00:00
$data = array();
2009-05-26 10:41:06 +00:00
foreach ($result as $category) {
2007-11-19 11:24:11 +00:00
$data[] = array(
'name' => $category->category,
'title' => $category->category,
'weight' => 3,
'access callback' => 'profile_category_access',
2008-04-23 20:01:56 +00:00
'access arguments' => array(1, $category->category)
2007-11-19 11:24:11 +00:00
);
2004-06-27 19:10:52 +00:00
}
return $data;
}
2007-12-31 08:54:37 +00:00
/**
2007-11-19 11:24:11 +00:00
* Menu item access callback - check if a user has access to a profile category.
*/
2008-04-23 20:01:56 +00:00
function profile_category_access($account, $category) {
if (user_access('administer users') && $account->uid > 0) {
2007-11-19 11:24:11 +00:00
return TRUE;
}
else {
2009-09-18 00:04:24 +00:00
$category_visible = (bool) db_query_range('SELECT 1 FROM {profile_field} WHERE category = :category AND visibility <> :visibility', 0, 1, array(
2009-05-26 10:41:06 +00:00
':category' => $category,
':visibility' => PROFILE_HIDDEN
2009-09-18 00:04:24 +00:00
))->fetchField();
2009-05-16 15:23:16 +00:00
return user_edit_access($account) && $category_visible;
2007-11-19 11:24:11 +00:00
}
}
2007-08-02 10:36:42 +00:00
/**
* Process variables for profile-block.tpl.php.
*
* The $variables array contains the following arguments:
* - $account
* - $fields
*
* @see profile-block.tpl.php
*/
function template_preprocess_profile_block(&$variables) {
2005-04-18 20:37:32 +00:00
2009-10-09 01:00:08 +00:00
$variables['user_picture'] = theme('user_picture', array('account' => $variables['account']));
2007-08-02 10:36:42 +00:00
$variables['profile'] = array();
// Supply filtered version of $fields that have values.
foreach ($variables['fields'] as $field) {
2005-09-06 20:39:10 +00:00
if ($field->value) {
2007-11-28 10:29:21 +00:00
$variables['profile'][$field->name]->title = check_plain($field->title);
2007-08-02 10:36:42 +00:00
$variables['profile'][$field->name]->value = $field->value;
$variables['profile'][$field->name]->type = $field->type;
2005-04-18 20:37:32 +00:00
}
}
}
2007-08-02 10:36:42 +00:00
/**
* Process variables for profile-listing.tpl.php.
*
* The $variables array contains the following arguments:
* - $account
* - $fields
*
* @see profile-listing.tpl.php
*/
function template_preprocess_profile_listing(&$variables) {
2004-03-11 20:33:59 +00:00
2009-10-09 01:00:08 +00:00
$variables['user_picture'] = theme('user_picture', array('account' => $variables['account']));
$variables['name'] = theme('username', array('account' => $variables['account']));
2007-08-02 10:36:42 +00:00
$variables['profile'] = array();
// Supply filtered version of $fields that have values.
foreach ($variables['fields'] as $field) {
2005-09-06 20:39:10 +00:00
if ($field->value) {
2007-08-02 10:36:42 +00:00
$variables['profile'][$field->name]->title = $field->title;
$variables['profile'][$field->name]->value = $field->value;
$variables['profile'][$field->name]->type = $field->type;
2002-08-08 18:52:55 +00:00
}
}
2004-03-11 20:33:59 +00:00
2007-08-02 10:36:42 +00:00
}
2004-03-11 20:33:59 +00:00
2007-08-02 10:36:42 +00:00
/**
* Process variables for profile-wrapper.tpl.php.
*
* The $variables array contains the following arguments:
* - $content
*
* @see profile-wrapper.tpl.php
*/
function template_preprocess_profile_wrapper(&$variables) {
$variables['current_field'] = '';
if ($field = arg(1)) {
$variables['current_field'] = $field;
// Supply an alternate template suggestion based on the browsable field.
2008-04-14 17:48:46 +00:00
$variables['template_files'][] = 'profile-wrapper-' . $field;
2007-08-02 10:36:42 +00:00
}
2004-03-11 20:33:59 +00:00
}
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';
}
2006-05-05 09:25:21 +00:00
2006-12-13 23:50:26 +00:00
function _profile_get_fields($category, $register = FALSE) {
2009-05-26 10:41:06 +00:00
$query = db_select('profile_field');
2006-12-13 23:50:26 +00:00
if ($register) {
2009-05-26 10:41:06 +00:00
$query->condition('register', 1);
2006-11-12 19:33:30 +00:00
}
else {
2009-05-26 10:41:06 +00:00
// Use LOWER(:category) instead of PHP's strtolower() to avoid UTF-8 conversion issues.
$query->where('LOWER(category) = LOWER(:category)', array(':category' => $category));
2006-11-12 19:33:30 +00:00
}
if (!user_access('administer users')) {
2009-06-02 00:23:42 +00:00
$query->condition('visibility', PROFILE_HIDDEN, '<>');
2006-11-12 19:33:30 +00:00
}
2009-05-26 10:41:06 +00:00
return $query
->fields('profile_field')
->orderBy('category', 'ASC')
->orderBy('weight', 'ASC')
->execute();
2006-11-12 19:33:30 +00:00
}