summaryrefslogtreecommitdiff
path: root/modules/profile/profile.pages.inc
blob: 056015a31023d38824a5aa66b4be8a1e36359ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * @file
 * User page callbacks for the profile module.
 */

/**
 * Menu callback; display a list of user information.
 */
function profile_browse() {
  // Ensure that the path is converted to 3 levels always.
  list(, $name, $value) = array_pad(explode('/', $_GET['q'], 3), 3, '');

  $field = db_query("SELECT DISTINCT(fid), type, title, page, visibility FROM {profile_field} WHERE name = :name", array(':name' => $name))->fetchObject();

  if ($name && $field->fid) {
    // Only allow browsing of fields that have a page title set.
    if (empty($field->page)) {
      return MENU_NOT_FOUND;
    }
    // Do not allow browsing of private and hidden fields by non-admins.
    if (!user_access('administer users') && ($field->visibility == PROFILE_PRIVATE || $field->visibility == PROFILE_HIDDEN)) {
      return MENU_ACCESS_DENIED;
    }

    // Compile a list of fields to show.
    $fields = db_query('SELECT name, title, type, weight, page, visibility FROM {profile_field} WHERE fid <> :fid AND visibility = :visibility ORDER BY weight', array(
      ':fid' => $field->fid,
      ':visibility' => PROFILE_PUBLIC_LISTINGS,
    ))->fetchAll();

    $query = db_select('users', 'u')->extend('PagerDefault');
    $query->join('profile_value', 'v', 'u.uid = v.uid');
    $query
      ->fields('u', array('uid', 'access'))
      ->condition('v.fid', $field->fid)
      ->condition('u.status', 0, '<>')
      ->orderBy('u.access', 'DESC');

    // Determine what query to use:
    $arguments = array($field->fid);
    switch ($field->type) {
      case 'checkbox':
        $query->condition('v.value', 1);
        break;
      case 'textfield':
      case 'selection':
        $query->condition('v.value', $value);
        break;
      case 'list':
        $query->condition('v.value', '%' . db_like($value) . '%', 'LIKE');
        break;
      default:
        return MENU_NOT_FOUND;
    }

    $uids = $query
      ->limit(20)
      ->execute()
      ->fetchCol();

    // Load the users.
    $users = user_load_multiple($uids);

    $content = '';
    foreach ($users as $account) {
      $profile = _profile_update_user_fields($fields, $account);
      $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
    }
    $output = theme('profile_wrapper', array('content' => $content));
    $output .= theme('pager');

    if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield') {
      $title = strtr(check_plain($field->page), array('%value' => drupal_placeholder($value)));
    }
    else {
      $title = check_plain($field->page);
    }

    drupal_set_title($title, PASS_THROUGH);
    return $output;
  }
  elseif ($name && !$field->fid) {
    return MENU_NOT_FOUND;
  }
  else {
    // Compile a list of fields to show.
    $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();

    // Extract the affected users:
    $query = db_select('users', 'u')->extend('PagerDefault');
    $uids = $query
      ->fields('u', array('uid', 'access'))
      ->condition('u.uid', 0, '>')
      ->condition('u.status', 0, '>')
      ->orderBy('u.access', 'DESC')
      ->limit(20)
      ->execute()
      ->fetchCol();
    $users = user_load_multiple($uids);
    $content = '';
    foreach ($users as $account) {
      $profile = _profile_update_user_fields($fields, $account);
      $content .= theme('profile_listing', array('account' => $account, 'fields' => $profile));
    }
    $output = theme('profile_wrapper', array('content' => $content));
    $output .= theme('pager');

    drupal_set_title(t('User list'));
    return $output;
  }
}

/**
 * Callback to allow autocomplete of profile text fields.
 */
function profile_autocomplete($field, $string) {
  $matches = array();
  $autocomplete_field = (bool) db_query_range("SELECT 1 FROM {profile_field} WHERE fid = :fid AND autocomplete = 1", 0, 1, array(':fid' => $field))->fetchField();
  if ($autocomplete_field) {
    $values = db_select('profile_value')
      ->fields('profile_value', array('value'))
      ->condition('fid', $field)
      ->condition('value', db_like($string) . '%', 'LIKE')
      ->groupBy('value')
      ->orderBy('value')
      ->range(0, 10)
      ->execute()->fetchCol();
    foreach ($values as $value) {
      $matches[$value] = check_plain($value);
    }
  }

  drupal_json_output($matches);
}