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-6.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. * Implementation of hook_help().
  24. */
  25. function profile_help($path, $arg) {
  26. switch ($path) {
  27. case 'admin/help#profile':
  28. $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>';
  29. $output .= '<p>'. t('The following types of fields can be added to a user profile:') .'</p>';
  30. $output .= '<ul><li>'. t('single-line textfield') .'</li>';
  31. $output .= '<li>'. t('multi-line textfield') .'</li>';
  32. $output .= '<li>'. t('checkbox') .'</li>';
  33. $output .= '<li>'. t('list selection') .'</li>';
  34. $output .= '<li>'. t('freeform list') .'</li>';
  35. $output .= '<li>'. t('URL') .'</li>';
  36. $output .= '<li>'. t('date') .'</li></ul>';
  37. $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>';
  38. return $output;
  39. case 'admin/user/profile':
  40. 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. To change the category of a field or the order of fields within a category, grab a drag-and-drop handle under the Title column and drag the field to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save configuration</em> button at the bottom of the page.") .'</p>';
  41. }
  42. }
  43. /**
  44. * Implementation of hook_theme()
  45. */
  46. function profile_theme() {
  47. return array(
  48. 'profile_block' => array(
  49. 'arguments' => array('account' => NULL, 'fields' => array()),
  50. 'template' => 'profile-block',
  51. ),
  52. 'profile_listing' => array(
  53. 'arguments' => array('account' => NULL, 'fields' => array()),
  54. 'template' => 'profile-listing',
  55. ),
  56. 'profile_wrapper' => array(
  57. 'arguments' => array('content' => NULL),
  58. 'template' => 'profile-wrapper',
  59. ),
  60. 'profile_admin_overview' => array(
  61. 'arguments' => array('form' => NULL),
  62. 'file' => 'profile.admin.inc',
  63. )
  64. );
  65. }
  66. /**
  67. * Implementation of hook_menu().
  68. */
  69. function profile_menu() {
  70. $items['profile'] = array(
  71. 'title' => 'User list',
  72. 'page callback' => 'profile_browse',
  73. 'access arguments' => array('access user profiles'),
  74. 'type' => MENU_SUGGESTED_ITEM,
  75. 'file' => 'profile.pages.inc',
  76. );
  77. $items['admin/user/profile'] = array(
  78. 'title' => 'Profiles',
  79. 'description' => 'Create customizable fields for your users.',
  80. 'page callback' => 'drupal_get_form',
  81. 'page arguments' => array('profile_admin_overview'),
  82. 'access arguments' => array('administer users'),
  83. 'file' => 'profile.admin.inc',
  84. );
  85. $items['admin/user/profile/add'] = array(
  86. 'title' => 'Add field',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('profile_field_form'),
  89. 'access arguments' => array('administer users'),
  90. 'type' => MENU_CALLBACK,
  91. 'file' => 'profile.admin.inc',
  92. );
  93. $items['admin/user/profile/autocomplete'] = array(
  94. 'title' => 'Profile category autocomplete',
  95. 'page callback' => 'profile_admin_settings_autocomplete',
  96. 'access arguments' => array('administer users'),
  97. 'type' => MENU_CALLBACK,
  98. 'file' => 'profile.admin.inc',
  99. );
  100. $items['admin/user/profile/edit'] = array(
  101. 'title' => 'Edit field',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('profile_field_form'),
  104. 'access arguments' => array('administer users'),
  105. 'type' => MENU_CALLBACK,
  106. 'file' => 'profile.admin.inc',
  107. );
  108. $items['admin/user/profile/delete'] = array(
  109. 'title' => 'Delete field',
  110. 'page callback' => 'drupal_get_form',
  111. 'page arguments' => array('profile_field_delete'),
  112. 'access arguments' => array('administer users'),
  113. 'type' => MENU_CALLBACK,
  114. 'file' => 'profile.admin.inc',
  115. );
  116. $items['profile/autocomplete'] = array(
  117. 'title' => 'Profile autocomplete',
  118. 'page callback' => 'profile_autocomplete',
  119. 'access arguments' => array('access user profiles'),
  120. 'type' => MENU_CALLBACK,
  121. 'file' => 'profile.pages.inc',
  122. );
  123. return $items;
  124. }
  125. /**
  126. * Implementation of hook_block().
  127. */
  128. function profile_block($op = 'list', $delta = 0, $edit = array()) {
  129. if ($op == 'list') {
  130. $blocks[0]['info'] = t('Author information');
  131. $blocks[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
  132. return $blocks;
  133. }
  134. else if ($op == 'configure' && $delta == 0) {
  135. // Compile a list of fields to show
  136. $fields = array();
  137. $result = db_query('SELECT name, title, weight, visibility FROM {profile_fields} WHERE visibility IN (%d, %d) ORDER BY weight', PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
  138. while ($record = db_fetch_object($result)) {
  139. $fields[$record->name] = check_plain($record->title);
  140. }
  141. $fields['user_profile'] = t('Link to full user profile');
  142. $form['profile_block_author_fields'] = array('#type' => 'checkboxes',
  143. '#title' => t('Profile fields to display'),
  144. '#default_value' => variable_get('profile_block_author_fields', array()),
  145. '#options' => $fields,
  146. '#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/user/profile'))),
  147. );
  148. return $form;
  149. }
  150. else if ($op == 'save' && $delta == 0) {
  151. variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
  152. }
  153. else if ($op == 'view') {
  154. if (user_access('access user profiles')) {
  155. $output = '';
  156. if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
  157. $node = node_load(arg(1));
  158. $account = user_load(array('uid' => $node->uid));
  159. if ($use_fields = variable_get('profile_block_author_fields', array())) {
  160. // Compile a list of fields to show.
  161. $fields = array();
  162. $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);
  163. while ($record = db_fetch_object($result)) {
  164. // Ensure that field is displayed only if it is among the defined block fields and, if it is private, the user has appropriate permissions.
  165. if (isset($use_fields[$record->name]) && $use_fields[$record->name]) {
  166. $fields[] = $record;
  167. }
  168. }
  169. }
  170. if (!empty($fields)) {
  171. $profile = _profile_update_user_fields($fields, $account);
  172. $output .= theme('profile_block', $account, $profile, TRUE);
  173. }
  174. if (isset($use_fields['user_profile']) && $use_fields['user_profile']) {
  175. $output .= '<div>'. l(t('View full user profile'), 'user/'. $account->uid) .'</div>';
  176. }
  177. }
  178. if ($output) {
  179. $block['subject'] = t('About %name', array('%name' => $account->name));
  180. $block['content'] = $output;
  181. return $block;
  182. }
  183. }
  184. }
  185. }
  186. /**
  187. * Implementation of hook_user().
  188. */
  189. function profile_user($type, &$edit, &$user, $category = NULL) {
  190. switch ($type) {
  191. case 'load':
  192. return profile_load_profile($user);
  193. case 'register':
  194. return profile_form_profile($edit, $user, $category, TRUE);
  195. case 'update':
  196. return profile_save_profile($edit, $user, $category);
  197. case 'insert':
  198. return profile_save_profile($edit, $user, $category, TRUE);
  199. case 'view':
  200. return profile_view_profile($user);
  201. case 'form':
  202. return profile_form_profile($edit, $user, $category);
  203. case 'validate':
  204. return profile_validate_profile($edit, $category);
  205. case 'categories':
  206. return profile_categories();
  207. case 'delete':
  208. db_query('DELETE FROM {profile_values} WHERE uid = %d', $user->uid);
  209. }
  210. }
  211. function profile_load_profile(&$user) {
  212. $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);
  213. while ($field = db_fetch_object($result)) {
  214. if (empty($user->{$field->name})) {
  215. $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
  216. }
  217. }
  218. }
  219. function profile_save_profile(&$edit, &$user, $category, $register = FALSE) {
  220. $result = _profile_get_fields($category, $register);
  221. while ($field = db_fetch_object($result)) {
  222. if (_profile_field_serialize($field->type)) {
  223. $edit[$field->name] = serialize($edit[$field->name]);
  224. }
  225. db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid);
  226. db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
  227. // Mark field as handled (prevents saving to user->data).
  228. $edit[$field->name] = NULL;
  229. }
  230. }
  231. function profile_view_field($user, $field) {
  232. // Only allow browsing of private fields for admins, if browsing is enabled,
  233. // and if a user has permission to view profiles. Note that this check is
  234. // necessary because a user may always see their own profile.
  235. $browse = user_access('access user profiles')
  236. && (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
  237. && !empty($field->page);
  238. if (isset($user->{$field->name}) && $value = $user->{$field->name}) {
  239. switch ($field->type) {
  240. case 'textarea':
  241. return check_markup($value);
  242. case 'textfield':
  243. case 'selection':
  244. return $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
  245. case 'checkbox':
  246. return $browse ? l($field->title, 'profile/'. $field->name) : check_plain($field->title);
  247. case 'url':
  248. return '<a href="'. check_url($value) .'">'. check_plain($value) .'</a>';
  249. case 'date':
  250. $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
  251. // Note: Avoid PHP's date() because it does not handle dates before
  252. // 1970 on Windows. This would make the date field useless for e.g.
  253. // birthdays.
  254. $replace = array(
  255. 'd' => sprintf('%02d', $value['day']),
  256. 'j' => $value['day'],
  257. 'm' => sprintf('%02d', $value['month']),
  258. 'M' => map_month($value['month']),
  259. 'Y' => $value['year'],
  260. 'H:i' => NULL,
  261. 'g:ia' => NULL,
  262. );
  263. return strtr($format, $replace);
  264. case 'list':
  265. $values = split("[,\n\r]", $value);
  266. $fields = array();
  267. foreach ($values as $value) {
  268. if ($value = trim($value)) {
  269. $fields[] = $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
  270. }
  271. }
  272. return implode(', ', $fields);
  273. }
  274. }
  275. }
  276. function profile_view_profile(&$user) {
  277. profile_load_profile($user);
  278. // Show private fields to administrators and people viewing their own account.
  279. if (user_access('administer users') || $GLOBALS['user']->uid == $user->uid) {
  280. $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
  281. }
  282. else {
  283. $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d AND visibility != %d ORDER BY category, weight', PROFILE_PRIVATE, PROFILE_HIDDEN);
  284. }
  285. $fields = array();
  286. while ($field = db_fetch_object($result)) {
  287. if ($value = profile_view_field($user, $field)) {
  288. $title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
  289. // Create a single fieldset for each category.
  290. if (!isset($user->content[$field->category])) {
  291. $user->content[$field->category] = array(
  292. '#type' => 'user_profile_category',
  293. '#title' => $field->category,
  294. );
  295. }
  296. $user->content[$field->category][$field->name] = array(
  297. '#type' => 'user_profile_item',
  298. '#title' => $title,
  299. '#value' => $value,
  300. '#weight' => $field->weight,
  301. '#attributes' => array('class' => 'profile-'. $field->name),
  302. );
  303. }
  304. }
  305. }
  306. function _profile_form_explanation($field) {
  307. $output = $field->explanation;
  308. if ($field->type == 'list') {
  309. $output .= ' '. t('Put each item on a separate line or separate them by commas. No HTML allowed.');
  310. }
  311. if ($field->visibility == PROFILE_PRIVATE) {
  312. $output .= ' '. t('The content of this field is kept private and will not be shown publicly.');
  313. }
  314. return $output;
  315. }
  316. function profile_form_profile($edit, $user, $category, $register = FALSE) {
  317. $result = _profile_get_fields($category, $register);
  318. $weight = 1;
  319. $fields = array();
  320. while ($field = db_fetch_object($result)) {
  321. $category = $field->category;
  322. if (!isset($fields[$category])) {
  323. $fields[$category] = array('#type' => 'fieldset', '#title' => check_plain($category), '#weight' => $weight++);
  324. }
  325. switch ($field->type) {
  326. case 'textfield':
  327. case 'url':
  328. $fields[$category][$field->name] = array('#type' => 'textfield',
  329. '#title' => check_plain($field->title),
  330. '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  331. '#maxlength' => 255,
  332. '#description' => _profile_form_explanation($field),
  333. '#required' => $field->required,
  334. );
  335. if ($field->autocomplete) {
  336. $fields[$category][$field->name]['#autocomplete_path'] = "profile/autocomplete/". $field->fid;
  337. }
  338. break;
  339. case 'textarea':
  340. $fields[$category][$field->name] = array('#type' => 'textarea',
  341. '#title' => check_plain($field->title),
  342. '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  343. '#description' => _profile_form_explanation($field),
  344. '#required' => $field->required,
  345. );
  346. break;
  347. case 'list':
  348. $fields[$category][$field->name] = array('#type' => 'textarea',
  349. '#title' => check_plain($field->title),
  350. '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  351. '#description' => _profile_form_explanation($field),
  352. '#required' => $field->required,
  353. );
  354. break;
  355. case 'checkbox':
  356. $fields[$category][$field->name] = array('#type' => 'checkbox',
  357. '#title' => check_plain($field->title),
  358. '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  359. '#description' => _profile_form_explanation($field),
  360. '#required' => $field->required,
  361. );
  362. break;
  363. case 'selection':
  364. $options = $field->required ? array() : array('--');
  365. $lines = split("[\n\r]", $field->options);
  366. foreach ($lines as $line) {
  367. if ($line = trim($line)) {
  368. $options[$line] = $line;
  369. }
  370. }
  371. $fields[$category][$field->name] = array('#type' => 'select',
  372. '#title' => check_plain($field->title),
  373. '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  374. '#options' => $options,
  375. '#description' => _profile_form_explanation($field),
  376. '#required' => $field->required,
  377. );
  378. break;
  379. case 'date':
  380. $fields[$category][$field->name] = array('#type' => 'date',
  381. '#title' => check_plain($field->title),
  382. '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  383. '#description' => _profile_form_explanation($field),
  384. '#required' => $field->required,
  385. );
  386. break;
  387. }
  388. }
  389. return $fields;
  390. }
  391. /**
  392. * Helper function: update an array of user fields by calling profile_view_field
  393. */
  394. function _profile_update_user_fields($fields, $account) {
  395. foreach ($fields as $key => $field) {
  396. $fields[$key]->value = profile_view_field($account, $field);
  397. }
  398. return $fields;
  399. }
  400. function profile_validate_profile($edit, $category) {
  401. $result = _profile_get_fields($category);
  402. while ($field = db_fetch_object($result)) {
  403. if ($edit[$field->name]) {
  404. if ($field->type == 'url') {
  405. if (!valid_url($edit[$field->name], TRUE)) {
  406. form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => $field->title)));
  407. }
  408. }
  409. }
  410. else if ($field->required && !user_access('administer users')) {
  411. form_set_error($field->name, t('The field %field is required.', array('%field' => $field->title)));
  412. }
  413. }
  414. return $edit;
  415. }
  416. function profile_categories() {
  417. $result = db_query("SELECT DISTINCT(category) FROM {profile_fields}");
  418. $data = array();
  419. while ($category = db_fetch_object($result)) {
  420. $data[] = array(
  421. 'name' => $category->category,
  422. 'title' => $category->category,
  423. 'weight' => 3,
  424. 'access callback' => 'profile_category_access',
  425. 'access arguments' => array(1, $category->category)
  426. );
  427. }
  428. return $data;
  429. }
  430. /**
  431. * Menu item access callback - check if a user has access to a profile category.
  432. */
  433. function profile_category_access($account, $category) {
  434. if (user_access('administer users') && $account->uid > 0) {
  435. return TRUE;
  436. }
  437. else {
  438. return user_edit_access($account) && db_result(db_query("SELECT COUNT(*) FROM {profile_fields} WHERE category = '%s' AND visibility <> %d", $category, PROFILE_HIDDEN));
  439. }
  440. }
  441. /**
  442. * Process variables for profile-block.tpl.php.
  443. *
  444. * The $variables array contains the following arguments:
  445. * - $account
  446. * - $fields
  447. *
  448. * @see profile-block.tpl.php
  449. */
  450. function template_preprocess_profile_block(&$variables) {
  451. $variables['picture'] = theme('user_picture', $variables['account']);
  452. $variables['profile'] = array();
  453. // Supply filtered version of $fields that have values.
  454. foreach ($variables['fields'] as $field) {
  455. if ($field->value) {
  456. $variables['profile'][$field->name]->title = check_plain($field->title);
  457. $variables['profile'][$field->name]->value = $field->value;
  458. $variables['profile'][$field->name]->type = $field->type;
  459. }
  460. }
  461. }
  462. /**
  463. * Process variables for profile-listing.tpl.php.
  464. *
  465. * The $variables array contains the following arguments:
  466. * - $account
  467. * - $fields
  468. *
  469. * @see profile-listing.tpl.php
  470. */
  471. function template_preprocess_profile_listing(&$variables) {
  472. $variables['picture'] = theme('user_picture', $variables['account']);
  473. $variables['name'] = theme('username', $variables['account']);
  474. $variables['profile'] = array();
  475. // Supply filtered version of $fields that have values.
  476. foreach ($variables['fields'] as $field) {
  477. if ($field->value) {
  478. $variables['profile'][$field->name]->title = $field->title;
  479. $variables['profile'][$field->name]->value = $field->value;
  480. $variables['profile'][$field->name]->type = $field->type;
  481. }
  482. }
  483. }
  484. /**
  485. * Process variables for profile-wrapper.tpl.php.
  486. *
  487. * The $variables array contains the following arguments:
  488. * - $content
  489. *
  490. * @see profile-wrapper.tpl.php
  491. */
  492. function template_preprocess_profile_wrapper(&$variables) {
  493. $variables['current_field'] = '';
  494. if ($field = arg(1)) {
  495. $variables['current_field'] = $field;
  496. // Supply an alternate template suggestion based on the browsable field.
  497. $variables['template_files'][] = 'profile-wrapper-'. $field;
  498. }
  499. }
  500. function _profile_field_types($type = NULL) {
  501. $types = array('textfield' => t('single-line textfield'),
  502. 'textarea' => t('multi-line textfield'),
  503. 'checkbox' => t('checkbox'),
  504. 'selection' => t('list selection'),
  505. 'list' => t('freeform list'),
  506. 'url' => t('URL'),
  507. 'date' => t('date'));
  508. return isset($type) ? $types[$type] : $types;
  509. }
  510. function _profile_field_serialize($type = NULL) {
  511. return $type == 'date';
  512. }
  513. function _profile_get_fields($category, $register = FALSE) {
  514. $args = array();
  515. $sql = 'SELECT * FROM {profile_fields} WHERE ';
  516. $filters = array();
  517. if ($register) {
  518. $filters[] = 'register = 1';
  519. }
  520. else {
  521. // Use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
  522. $filters[] = "LOWER(category) = LOWER('%s')";
  523. $args[] = $category;
  524. }
  525. if (!user_access('administer users')) {
  526. $filters[] = 'visibility != %d';
  527. $args[] = PROFILE_HIDDEN;
  528. }
  529. $sql .= implode(' AND ', $filters);
  530. $sql .= ' ORDER BY category, weight';
  531. return db_query($sql, $args);
  532. }