profile.module

  1. 7.x drupal-7.x/modules/profile/profile.module
  2. 6.x drupal-6.x/modules/profile/profile.module

Support for configurable user profiles.

File

drupal-7.x/modules/profile/profile.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Support for configurable user profiles.
  5. */
  6. /**
  7. * Private field, content only available to privileged users.
  8. */
  9. define('PROFILE_PRIVATE', 1);
  10. /**
  11. * Public field, content shown on profile page but not used on member list pages.
  12. */
  13. define('PROFILE_PUBLIC', 2);
  14. /**
  15. * Public field, content shown on profile page and on member list pages.
  16. */
  17. define('PROFILE_PUBLIC_LISTINGS', 3);
  18. /**
  19. * Hidden profile field, only accessible by administrators, modules and themes.
  20. */
  21. define('PROFILE_HIDDEN', 4);
  22. /**
  23. * Implements hook_help().
  24. */
  25. function profile_help($path, $arg) {
  26. switch ($path) {
  27. case 'admin/help#profile':
  28. $output = '';
  29. $output .= '<h3>' . t('About') . '</h3>';
  30. $output .= '<p>' . t('The Profile module allows site administrators to define custom fields (such as country, full name, or age) for user profiles, which are then displayed in the <a href="@user">My Account</a> section. This permits users of a site to share more information about themselves, and can help community-based sites organize users around specific information. For more information, see the online handbook entry for <a href="@profile">Profile module</a>.', array('@user' => url('user'), '@profile' => 'http://drupal.org/documentation/modules/profile/')) . '</p>';
  31. $output .= '<h3>' . t('Uses') . '</h3>';
  32. $output .= '<dl>';
  33. $output .= '<dt>' . t('Adding fields to the default profile') . '</dt>';
  34. $output .= '<dd>' . t('To provide the ability for users to enter more information about themselves, the Profile module allows additional fields to be added to the default user profile. Examples of common additions would be <em>Location</em>, <em>Company</em>, <em>Age</em>, or <em>About me</em>.') . '</dd>';
  35. $output .= '<dt>' . t('User information pages') . '</dt>';
  36. $output .= '<dd>' . t('The Profile module enables links to see further information about site users. You can view both a main <a href="@profile">User list page</a>, and more specified pages by clicking on linked fields in any profile. For example, the <a href="@profile-country">People who live in Canada</a> listing on Drupal.org displays all users who have entered <em>Canada</em> in the <em>Country</em> field on their user profile.', array('@profile' => url('profile'), '@profile-country' => 'http://drupal.org/profile/country/Canada')) . '</dd>';
  37. $output .= '<dt>' . t('Author information block') . '</dt>';
  38. $output .= '<dd>' . t('The <em>Author information block</em> is a default block created by the Profile module that can be enabled on the <a href="@blocks">Blocks administration page</a>. It shows visitors of your website information about the author of the page they are reading.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
  39. $output .= '</dl>';
  40. return $output;
  41. case 'admin/config/people/profile':
  42. 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.") . '</p>';
  43. }
  44. }
  45. /**
  46. * Implements hook_theme().
  47. */
  48. function profile_theme() {
  49. return array(
  50. 'profile_block' => array(
  51. 'variables' => array('account' => NULL, 'fields' => array()),
  52. 'template' => 'profile-block',
  53. ),
  54. 'profile_listing' => array(
  55. 'variables' => array('account' => NULL, 'fields' => array()),
  56. 'template' => 'profile-listing',
  57. ),
  58. 'profile_wrapper' => array(
  59. 'variables' => array('content' => NULL),
  60. 'template' => 'profile-wrapper',
  61. ),
  62. 'profile_admin_overview' => array(
  63. 'render element' => 'form',
  64. 'file' => 'profile.admin.inc',
  65. )
  66. );
  67. }
  68. /**
  69. * Implements hook_menu().
  70. */
  71. function profile_menu() {
  72. $items['profile'] = array(
  73. 'title' => 'User list',
  74. 'page callback' => 'profile_browse',
  75. 'access arguments' => array('access user profiles'),
  76. 'file' => 'profile.pages.inc',
  77. 'type' => MENU_SUGGESTED_ITEM,
  78. );
  79. $items['admin/config/people/profile'] = array(
  80. 'title' => 'Profiles',
  81. 'description' => 'Create customizable fields for your users.',
  82. 'page callback' => 'drupal_get_form',
  83. 'page arguments' => array('profile_admin_overview'),
  84. 'access arguments' => array('administer users'),
  85. 'file' => 'profile.admin.inc',
  86. );
  87. $items['admin/config/people/profile/add'] = array(
  88. 'title' => 'Add field',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('profile_field_form'),
  91. 'access arguments' => array('administer users'),
  92. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  93. 'file' => 'profile.admin.inc',
  94. );
  95. $items['admin/config/people/profile/autocomplete'] = array(
  96. 'title' => 'Profile category autocomplete',
  97. 'page callback' => 'profile_admin_settings_autocomplete',
  98. 'access arguments' => array('administer users'),
  99. 'file' => 'profile.admin.inc',
  100. 'type' => MENU_CALLBACK,
  101. );
  102. $items['admin/config/people/profile/edit'] = array(
  103. 'title' => 'Edit field',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('profile_field_form'),
  106. 'access arguments' => array('administer users'),
  107. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  108. 'file' => 'profile.admin.inc',
  109. );
  110. $items['admin/config/people/profile/delete'] = array(
  111. 'title' => 'Delete field',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('profile_field_delete'),
  114. 'access arguments' => array('administer users'),
  115. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  116. 'file' => 'profile.admin.inc',
  117. );
  118. $items['profile/autocomplete'] = array(
  119. 'title' => 'Profile autocomplete',
  120. 'page callback' => 'profile_autocomplete',
  121. 'access arguments' => array('access user profiles'),
  122. 'file' => 'profile.pages.inc',
  123. 'type' => MENU_CALLBACK,
  124. );
  125. return $items;
  126. }
  127. /**
  128. * Implements hook_block_info().
  129. */
  130. function profile_block_info() {
  131. $blocks['author-information']['info'] = t('Author information');
  132. $blocks['author-information']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE;
  133. return $blocks;
  134. }
  135. /**
  136. * Implements hook_block_configure().
  137. */
  138. function profile_block_configure($delta = '') {
  139. // Compile a list of fields to show
  140. $fields = array();
  141. $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)));
  142. foreach ($result as $record) {
  143. $fields[$record->name] = check_plain($record->title);
  144. }
  145. $fields['user_profile'] = t('Link to full user profile');
  146. $form['profile_block_author_fields'] = array(
  147. '#type' => 'checkboxes',
  148. '#title' => t('Profile fields to display'),
  149. '#default_value' => variable_get('profile_block_author_fields', array()),
  150. '#options' => $fields,
  151. '#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'))),
  152. );
  153. return $form;
  154. }
  155. /**
  156. * Implements hook_block_save().
  157. */
  158. function profile_block_save($delta = '', $edit = array()) {
  159. variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
  160. }
  161. /**
  162. * Implements hook_block_view().
  163. */
  164. function profile_block_view($delta = '') {
  165. if (user_access('access user profiles')) {
  166. $output = '';
  167. if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) {
  168. $node = node_load(arg(1));
  169. $account = user_load($node->uid);
  170. if ($use_fields = variable_get('profile_block_author_fields', array())) {
  171. // Compile a list of fields to show.
  172. $fields = array();
  173. $result = db_query('SELECT * FROM {profile_field} WHERE visibility IN (:visibility) ORDER BY weight', array(':visibility' => array(PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS)));
  174. foreach ($result as $record) {
  175. // Ensure that field is displayed only if it is among the defined block fields and, if it is private, the user has appropriate permissions.
  176. if (isset($use_fields[$record->name]) && $use_fields[$record->name]) {
  177. $fields[] = $record;
  178. }
  179. }
  180. }
  181. if (!empty($fields)) {
  182. $profile = _profile_update_user_fields($fields, $account);
  183. $output .= theme('profile_block', array('account' => $account, 'fields' => $profile));
  184. }
  185. if (isset($use_fields['user_profile']) && $use_fields['user_profile']) {
  186. $output .= '<div>' . l(t('View full user profile'), 'user/' . $account->uid) . '</div>';
  187. }
  188. }
  189. if ($output) {
  190. $block['subject'] = t('About %name', array('%name' => format_username($account)));
  191. $block['content'] = $output;
  192. return $block;
  193. }
  194. }
  195. }
  196. /**
  197. * Implements hook_user_presave().
  198. */
  199. function profile_user_presave(&$edit, $account, $category) {
  200. if (!empty($account->uid)) {
  201. profile_save_profile($edit, $account, $category);
  202. }
  203. }
  204. /**
  205. * Implements hook_user_insert().
  206. */
  207. function profile_user_insert(&$edit, $account, $category) {
  208. profile_save_profile($edit, $account, $category, TRUE);
  209. }
  210. /**
  211. * Implements hook_user_cancel().
  212. */
  213. function profile_user_cancel($edit, $account, $method) {
  214. switch ($method) {
  215. case 'user_cancel_reassign':
  216. db_delete('profile_value')
  217. ->condition('uid', $account->uid)
  218. ->execute();
  219. }
  220. }
  221. /**
  222. * Implements hook_user_delete().
  223. */
  224. function profile_user_delete($account) {
  225. db_delete('profile_value')
  226. ->condition('uid', $account->uid)
  227. ->execute();
  228. }
  229. /**
  230. * Implements hook_user_load().
  231. */
  232. function profile_user_load($users) {
  233. $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)));
  234. foreach ($result as $record) {
  235. if (empty($users[$record->uid]->{$record->name})) {
  236. $users[$record->uid]->{$record->name} = _profile_field_serialize($record->type) ? unserialize($record->value) : $record->value;
  237. }
  238. }
  239. }
  240. function profile_save_profile(&$edit, $account, $category, $register = FALSE) {
  241. $result = _profile_get_fields($category, $register);
  242. foreach ($result as $field) {
  243. if (_profile_field_serialize($field->type)) {
  244. $edit[$field->name] = serialize($edit[$field->name]);
  245. }
  246. db_merge('profile_value')
  247. ->key(array(
  248. 'fid' => $field->fid,
  249. 'uid' => $account->uid,
  250. ))
  251. ->fields(array('value' => $edit[$field->name]))
  252. ->execute();
  253. }
  254. }
  255. function profile_view_field($account, $field) {
  256. // Only allow browsing of private fields for admins, if browsing is enabled,
  257. // and if a user has permission to view profiles. Note that this check is
  258. // necessary because a user may always see their own profile.
  259. $browse = user_access('access user profiles')
  260. && (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
  261. && !empty($field->page);
  262. if (isset($account->{$field->name}) && $value = $account->{$field->name}) {
  263. switch ($field->type) {
  264. case 'textarea':
  265. return check_markup($value, filter_default_format($account), '', TRUE);
  266. case 'textfield':
  267. case 'selection':
  268. return $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
  269. case 'checkbox':
  270. return $browse ? l($field->title, 'profile/' . $field->name) : check_plain($field->title);
  271. case 'url':
  272. return '<a href="' . check_url($value) . '">' . check_plain($value) . '</a>';
  273. case 'date':
  274. $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
  275. // Note: Avoid PHP's date() because it does not handle dates before
  276. // 1970 on Windows. This would make the date field useless for e.g.
  277. // birthdays.
  278. $replace = array(
  279. 'd' => sprintf('%02d', $value['day']),
  280. 'j' => $value['day'],
  281. 'm' => sprintf('%02d', $value['month']),
  282. 'M' => map_month($value['month']),
  283. 'Y' => $value['year'],
  284. 'H:i' => NULL,
  285. 'g:ia' => NULL,
  286. );
  287. return strtr($format, $replace);
  288. case 'list':
  289. $values = preg_split("/[,\n\r]/", $value);
  290. $fields = array();
  291. foreach ($values as $value) {
  292. if ($value = trim($value)) {
  293. $fields[] = $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
  294. }
  295. }
  296. return implode(', ', $fields);
  297. }
  298. }
  299. }
  300. /**
  301. * Implements hook_user_view().
  302. */
  303. function profile_user_view($account) {
  304. // Show private fields to administrators and people viewing their own account.
  305. if (user_access('administer users') || $GLOBALS['user']->uid == $account->uid) {
  306. $result = db_query('SELECT * FROM {profile_field} WHERE visibility <> :hidden ORDER BY category, weight', array(':hidden' => PROFILE_HIDDEN));
  307. }
  308. else {
  309. $result = db_query('SELECT * FROM {profile_field} WHERE visibility <> :private AND visibility <> :hidden ORDER BY category, weight', array(':private' => PROFILE_PRIVATE, ':hidden' => PROFILE_HIDDEN));
  310. }
  311. $fields = array();
  312. foreach ($result as $field) {
  313. if ($value = profile_view_field($account, $field)) {
  314. $title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
  315. // Create a single fieldset for each category.
  316. if (!isset($account->content[$field->category])) {
  317. $account->content[$field->category] = array(
  318. '#type' => 'user_profile_category',
  319. '#title' => $field->category,
  320. );
  321. }
  322. $account->content[$field->category][$field->name] = array(
  323. '#type' => 'user_profile_item',
  324. '#title' => $title,
  325. '#markup' => $value,
  326. '#weight' => $field->weight,
  327. '#attributes' => array('class' => array('profile-' . $field->name)),
  328. );
  329. }
  330. }
  331. }
  332. function _profile_form_explanation($field) {
  333. $output = filter_xss_admin($field->explanation);
  334. if ($field->type == 'list') {
  335. $output .= ' ' . t('Put each item on a separate line or separate them by commas. No HTML allowed.');
  336. }
  337. if ($field->visibility == PROFILE_PRIVATE) {
  338. $output .= ' ' . t('The content of this field is kept private and will not be shown publicly.');
  339. }
  340. return $output;
  341. }
  342. /**
  343. * Implements hook_form_alter().
  344. */
  345. function profile_form_alter(&$form, &$form_state, $form_id) {
  346. if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
  347. return;
  348. }
  349. $form['#validate'][] = 'profile_user_form_validate';
  350. $account = $form['#user'];
  351. $result = _profile_get_fields($form['#user_category'], $form['#user_category'] == 'register');
  352. $weight = 1;
  353. foreach ($result as $field) {
  354. $category = $field->category;
  355. if (!isset($form[$category])) {
  356. $form[$category] = array('#type' => 'fieldset', '#title' => check_plain($category), '#weight' => $weight++);
  357. }
  358. switch ($field->type) {
  359. case 'textfield':
  360. case 'url':
  361. $form[$category][$field->name] = array(
  362. '#type' => 'textfield',
  363. '#title' => check_plain($field->title),
  364. '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
  365. '#maxlength' => 255,
  366. '#description' => _profile_form_explanation($field),
  367. '#required' => $field->required,
  368. );
  369. if ($field->autocomplete) {
  370. $form[$category][$field->name]['#autocomplete_path'] = "profile/autocomplete/" . $field->fid;
  371. }
  372. break;
  373. case 'textarea':
  374. $form[$category][$field->name] = array(
  375. '#type' => 'textarea',
  376. '#title' => check_plain($field->title),
  377. '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
  378. '#description' => _profile_form_explanation($field),
  379. '#required' => $field->required,
  380. );
  381. break;
  382. case 'list':
  383. $form[$category][$field->name] = array(
  384. '#type' => 'textarea',
  385. '#title' => check_plain($field->title),
  386. '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
  387. '#description' => _profile_form_explanation($field),
  388. '#required' => $field->required,
  389. );
  390. break;
  391. case 'checkbox':
  392. $form[$category][$field->name] = array(
  393. '#type' => 'checkbox',
  394. '#title' => check_plain($field->title),
  395. '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
  396. '#description' => _profile_form_explanation($field),
  397. '#required' => $field->required,
  398. );
  399. break;
  400. case 'selection':
  401. $options = array();
  402. $lines = preg_split("/[\n\r]/", $field->options);
  403. foreach ($lines as $line) {
  404. if ($line = trim($line)) {
  405. $options[$line] = $line;
  406. }
  407. }
  408. $form[$category][$field->name] = array(
  409. '#type' => 'select',
  410. '#title' => check_plain($field->title),
  411. '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
  412. '#options' => $options,
  413. '#description' => _profile_form_explanation($field),
  414. '#required' => $field->required,
  415. '#empty_value' => 0,
  416. );
  417. break;
  418. case 'date':
  419. $form[$category][$field->name] = array(
  420. '#type' => 'date',
  421. '#title' => check_plain($field->title),
  422. '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
  423. '#description' => _profile_form_explanation($field),
  424. '#required' => $field->required,
  425. );
  426. break;
  427. }
  428. }
  429. }
  430. /**
  431. * Helper function: update an array of user fields by calling profile_view_field
  432. */
  433. function _profile_update_user_fields($fields, $account) {
  434. foreach ($fields as $key => $field) {
  435. $fields[$key]->value = profile_view_field($account, $field);
  436. }
  437. return $fields;
  438. }
  439. /**
  440. * Form validation handler for the user register/profile form.
  441. *
  442. * @see profile_form_alter()
  443. */
  444. function profile_user_form_validate($form, &$form_state) {
  445. $result = _profile_get_fields($form['#user_category'], $form['#user_category'] == 'register');
  446. foreach ($result as $field) {
  447. if (!empty($form_state['values'][$field->name])) {
  448. if ($field->type == 'url' && !valid_url($form_state['values'][$field->name], TRUE)) {
  449. form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => $field->title)));
  450. }
  451. }
  452. elseif ($field->required && !user_access('administer users')) {
  453. form_set_error($field->name, t('The field %field is required.', array('%field' => $field->title)));
  454. }
  455. }
  456. }
  457. /**
  458. * Implements hook_user_categories().
  459. */
  460. function profile_user_categories() {
  461. $result = db_query("SELECT DISTINCT(category) FROM {profile_field}");
  462. $data = array();
  463. foreach ($result as $category) {
  464. $data[] = array(
  465. 'name' => $category->category,
  466. 'title' => $category->category,
  467. 'weight' => 3,
  468. 'access callback' => 'profile_category_access',
  469. 'access arguments' => array(1, $category->category)
  470. );
  471. }
  472. return $data;
  473. }
  474. /**
  475. * Menu item access callback - check if a user has access to a profile category.
  476. */
  477. function profile_category_access($account, $category) {
  478. if (user_access('administer users') && $account->uid > 0) {
  479. return TRUE;
  480. }
  481. else {
  482. $category_visible = (bool) db_query_range('SELECT 1 FROM {profile_field} WHERE category = :category AND visibility <> :visibility', 0, 1, array(
  483. ':category' => $category,
  484. ':visibility' => PROFILE_HIDDEN
  485. ))->fetchField();
  486. return user_edit_access($account) && $category_visible;
  487. }
  488. }
  489. /**
  490. * Process variables for profile-block.tpl.php.
  491. *
  492. * The $variables array contains the following arguments:
  493. * - $account
  494. * - $fields
  495. *
  496. * @see profile-block.tpl.php
  497. */
  498. function template_preprocess_profile_block(&$variables) {
  499. $variables['user_picture'] = theme('user_picture', array('account' => $variables['account']));
  500. $variables['profile'] = array();
  501. // Supply filtered version of $fields that have values.
  502. foreach ($variables['fields'] as $field) {
  503. if ($field->value) {
  504. $variables['profile'][$field->name] = new stdClass();
  505. $variables['profile'][$field->name]->title = check_plain($field->title);
  506. $variables['profile'][$field->name]->value = $field->value;
  507. $variables['profile'][$field->name]->type = $field->type;
  508. }
  509. }
  510. }
  511. /**
  512. * Process variables for profile-listing.tpl.php.
  513. *
  514. * The $variables array contains the following arguments:
  515. * - $account
  516. * - $fields
  517. *
  518. * @see profile-listing.tpl.php
  519. */
  520. function template_preprocess_profile_listing(&$variables) {
  521. $variables['user_picture'] = theme('user_picture', array('account' => $variables['account']));
  522. $variables['name'] = theme('username', array('account' => $variables['account']));
  523. $variables['profile'] = array();
  524. // Supply filtered version of $fields that have values.
  525. foreach ($variables['fields'] as $field) {
  526. if ($field->value) {
  527. $variables['profile'][$field->name] = new stdClass();
  528. $variables['profile'][$field->name]->title = $field->title;
  529. $variables['profile'][$field->name]->value = $field->value;
  530. $variables['profile'][$field->name]->type = $field->type;
  531. }
  532. }
  533. }
  534. /**
  535. * Process variables for profile-wrapper.tpl.php.
  536. *
  537. * The $variables array contains the following arguments:
  538. * - $content
  539. *
  540. * @see profile-wrapper.tpl.php
  541. */
  542. function template_preprocess_profile_wrapper(&$variables) {
  543. $variables['current_field'] = '';
  544. if ($field = arg(1)) {
  545. $variables['current_field'] = $field;
  546. $variables['theme_hook_suggestions'][] = 'profile_wrapper__' . $field;
  547. }
  548. }
  549. function _profile_field_types($type = NULL) {
  550. $types = array('textfield' => t('single-line textfield'),
  551. 'textarea' => t('multi-line textfield'),
  552. 'checkbox' => t('checkbox'),
  553. 'selection' => t('list selection'),
  554. 'list' => t('freeform list'),
  555. 'url' => t('URL'),
  556. 'date' => t('date'));
  557. return isset($type) ? $types[$type] : $types;
  558. }
  559. function _profile_field_serialize($type = NULL) {
  560. return $type == 'date';
  561. }
  562. function _profile_get_fields($category, $register = FALSE) {
  563. $query = db_select('profile_field');
  564. if ($register) {
  565. $query->condition('register', 1);
  566. }
  567. else {
  568. $query->condition('category', db_like($category), 'LIKE');
  569. }
  570. if (!user_access('administer users')) {
  571. $query->condition('visibility', PROFILE_HIDDEN, '<>');
  572. }
  573. return $query
  574. ->fields('profile_field')
  575. ->orderBy('category', 'ASC')
  576. ->orderBy('weight', 'ASC')
  577. ->execute();
  578. }