Projet

Général

Profil

Paste
Télécharger (5,21 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / user / views_plugin_argument_validate_user.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_argument_validate_user.
6
 */
7

    
8
/**
9
 * Validate whether an argument is a valid user.
10
 *
11
 * This supports either numeric arguments (UID) or strings (username) and
12
 * converts either one into the user's UID.  This validator also sets the
13
 * argument's title to the username.
14
 */
15
class views_plugin_argument_validate_user extends views_plugin_argument_validate {
16

    
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public function option_definition() {
21
    $options = parent::option_definition();
22
    $options['type'] = array('default' => 'uid');
23
    $options['restrict_roles'] = array('default' => FALSE, 'bool' => TRUE);
24
    $options['roles'] = array('default' => array());
25

    
26
    return $options;
27
  }
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function options_form(&$form, &$form_state) {
33
    $form['type'] = array(
34
      '#type' => 'radios',
35
      '#title' => t('Type of user filter value to allow'),
36
      '#options' => array(
37
        'uid' => t('Only allow numeric UIDs'),
38
        'name' => t('Only allow string usernames'),
39
        'either' => t('Allow both numeric UIDs and string usernames'),
40
      ),
41
      '#default_value' => $this->options['type'],
42
    );
43

    
44
    $form['restrict_roles'] = array(
45
      '#type' => 'checkbox',
46
      '#title' => t('Restrict user based on role'),
47
      '#default_value' => $this->options['restrict_roles'],
48
    );
49

    
50
    $form['roles'] = array(
51
      '#type' => 'checkboxes',
52
      '#prefix' => '<div id="edit-options-validate-options-user-roles-wrapper">',
53
      '#suffix' => '</div>',
54
      '#title' => t('Restrict to the selected roles'),
55
      '#options' => array_map('check_plain', user_roles(TRUE)),
56
      '#default_value' => $this->options['roles'],
57
      '#description' => t('If no roles are selected, users from any role will be allowed.'),
58
      '#dependency' => array(
59
        'edit-options-validate-options-user-restrict-roles' => array(1),
60
      ),
61
    );
62
  }
63

    
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function options_submit(&$form, &$form_state, &$options = array()) {
68
    // Filter trash out of the options so we don't store giant unnecessary
69
    // arrays.
70
    $options['roles'] = array_filter($options['roles']);
71
  }
72

    
73
  /**
74
   * {@inheritdoc}
75
   */
76
  public function convert_options(&$options) {
77
    if (!isset($options['type']) && isset($this->argument->options['validate_user_argument_type'])) {
78
      $options['type'] = $this->argument->options['validate_user_argument_type'];
79
      $options['restrict_roles'] = $this->argument->options['validate_user_restrict_roles'];
80
      $options['roles'] = $this->argument->options['validate_user_roles'];
81
    }
82
  }
83

    
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function validate_argument($argument) {
88
    $type = $this->options['type'];
89
    // is_numeric() can return false positives, so we ensure it's an integer.
90
    // However, is_integer() will always fail, since $argument is a string.
91
    if (is_numeric($argument) && $argument == (int) $argument) {
92
      if ($type == 'uid' || $type == 'either') {
93
        if ($argument == $GLOBALS['user']->uid) {
94
          // If you assign an object to a variable in PHP, the variable
95
          // automatically acts as a reference, not a copy, so we use
96
          // clone to ensure that we don't actually mess with the
97
          // real global $user object.
98
          $account = clone $GLOBALS['user'];
99
        }
100
        $where = 'uid = :argument';
101
      }
102
    }
103
    else {
104
      if ($type == 'name' || $type == 'either') {
105
        $name = !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : variable_get('anonymous', t('Anonymous'));
106
        if ($argument == $name) {
107
          $account = clone $GLOBALS['user'];
108
        }
109
        $where = "name = :argument";
110
      }
111
    }
112

    
113
    // If we don't have a WHERE clause, the argument is invalid.
114
    if (empty($where)) {
115
      return FALSE;
116
    }
117

    
118
    if (!isset($account)) {
119
      $query = "SELECT uid, name FROM {users} WHERE $where";
120
      $account = db_query($query, array(':argument' => $argument))->fetchObject();
121
    }
122
    if (empty($account)) {
123
      // User not found.
124
      return FALSE;
125
    }
126

    
127
    // See if we're filtering users based on roles.
128
    if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
129
      $roles = $this->options['roles'];
130
      $account->roles = array();
131
      $account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
132
      $result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
133
      foreach ($result as $role) {
134
        $account->roles[] = $role->rid;
135
      }
136
      if (!(bool) array_intersect($account->roles, $roles)) {
137
        return FALSE;
138
      }
139
    }
140

    
141
    $this->argument->argument = $account->uid;
142
    $this->argument->validated_title = check_plain(format_username($account));
143
    return TRUE;
144
  }
145

    
146
  /**
147
   * {@inheritdoc}
148
   */
149
  public function process_summary_arguments(&$args) {
150
    // If the validation says the input is an username, we should reverse the
151
    // argument so it works for example for generation summary urls.
152
    $uids_arg_keys = array_flip($args);
153
    if ($this->options['type'] == 'name') {
154
      $users = user_load_multiple($args);
155
      foreach ($users as $uid => $account) {
156
        $args[$uids_arg_keys[$uid]] = $account->name;
157
      }
158
    }
159
  }
160

    
161
}