profile.pages.inc 4.4 KB

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