profile.pages.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the profile module.
  5. */
  6. /**
  7. * Menu callback; display a list of user information.
  8. */
  9. function profile_browse() {
  10. // Ensure that the path is converted to 3 levels always.
  11. list(, $name, $value) = array_pad(explode('/', $_GET['q'], 3), 3, '');
  12. $field = db_query("SELECT DISTINCT(fid), type, title, page, visibility FROM {profile_field} WHERE name = :name", array(':name' => $name))->fetchObject();
  13. if ($name && $field->fid) {
  14. // Only allow browsing of fields that have a page title set.
  15. if (empty($field->page)) {
  16. return MENU_NOT_FOUND;
  17. }
  18. // Do not allow browsing of private and hidden fields by non-admins.
  19. if (!user_access('administer users') && ($field->visibility == PROFILE_PRIVATE || $field->visibility == PROFILE_HIDDEN)) {
  20. return MENU_ACCESS_DENIED;
  21. }
  22. // Compile a list of fields to show.
  23. $fields = db_query('SELECT name, title, type, weight, page, visibility FROM {profile_field} WHERE fid <> :fid AND visibility = :visibility ORDER BY weight', array(
  24. ':fid' => $field->fid,
  25. ':visibility' => PROFILE_PUBLIC_LISTINGS,
  26. ))->fetchAll();
  27. $query = db_select('users', 'u')->extend('PagerDefault');
  28. $query->join('profile_value', 'v', 'u.uid = v.uid');
  29. $query
  30. ->fields('u', array('uid', 'access'))
  31. ->condition('v.fid', $field->fid)
  32. ->condition('u.status', 0, '<>')
  33. ->orderBy('u.access', 'DESC');
  34. // Determine what query to use:
  35. $arguments = array($field->fid);
  36. switch ($field->type) {
  37. case 'checkbox':
  38. $query->condition('v.value', 1);
  39. break;
  40. case 'textfield':
  41. case 'selection':
  42. $query->condition('v.value', $value);
  43. break;
  44. case 'list':
  45. $query->condition('v.value', '%' . db_like($value) . '%', 'LIKE');
  46. break;
  47. default:
  48. return MENU_NOT_FOUND;
  49. }
  50. $uids = $query
  51. ->limit(20)
  52. ->execute()
  53. ->fetchCol();
  54. // Load the users.
  55. $users = user_load_multiple($uids);
  56. $content = '';
  57. foreach ($users as $account) {
  58. $profile = _profile_update_user_fields($fields, $account);
  59. $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
  60. }
  61. $output = theme('profile_wrapper', array('content' => $content));
  62. $output .= theme('pager');
  63. if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield') {
  64. $title = strtr(check_plain($field->page), array('%value' => drupal_placeholder($value)));
  65. }
  66. else {
  67. $title = check_plain($field->page);
  68. }
  69. drupal_set_title($title, PASS_THROUGH);
  70. return $output;
  71. }
  72. elseif ($name && !$field->fid) {
  73. return MENU_NOT_FOUND;
  74. }
  75. else {
  76. // Compile a list of fields to show.
  77. $fields = db_query('SELECT name, title, type, weight, page, visibility FROM {profile_field} WHERE visibility = :visibility ORDER BY category, weight', array(':visibility' => PROFILE_PUBLIC_LISTINGS))->fetchAll();
  78. // Extract the affected users:
  79. $query = db_select('users', 'u')->extend('PagerDefault');
  80. $uids = $query
  81. ->fields('u', array('uid', 'access'))
  82. ->condition('u.uid', 0, '>')
  83. ->condition('u.status', 0, '>')
  84. ->orderBy('u.access', 'DESC')
  85. ->limit(20)
  86. ->execute()
  87. ->fetchCol();
  88. $users = user_load_multiple($uids);
  89. $content = '';
  90. foreach ($users as $account) {
  91. $profile = _profile_update_user_fields($fields, $account);
  92. $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
  93. }
  94. $output = theme('profile_wrapper', array('content' => $content));
  95. $output .= theme('pager');
  96. drupal_set_title(t('User list'));
  97. return $output;
  98. }
  99. }
  100. /**
  101. * Callback to allow autocomplete of profile text fields.
  102. */
  103. function profile_autocomplete($field, $string) {
  104. $matches = array();
  105. $autocomplete_field = (bool) db_query_range("SELECT 1 FROM {profile_field} WHERE fid = :fid AND autocomplete = 1", 0, 1, array(':fid' => $field))->fetchField();
  106. if ($autocomplete_field) {
  107. $values = db_select('profile_value')
  108. ->fields('profile_value', array('value'))
  109. ->condition('fid', $field)
  110. ->condition('value', db_like($string) . '%', 'LIKE')
  111. ->groupBy('value')
  112. ->orderBy('value')
  113. ->range(0, 10)
  114. ->execute()->fetchCol();
  115. foreach ($values as $value) {
  116. $matches[$value] = check_plain($value);
  117. }
  118. }
  119. drupal_json_output($matches);
  120. }