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