Projet

Général

Profil

Paste
Télécharger (4,48 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / user.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * @file
5
 *
6
 * Plugin to provide a user context
7
 */
8

    
9
/**
10
 * Plugins are described by creating a $plugin array which will be used
11
 * by the system that includes this file.
12
 */
13
$plugin = array(
14
  'title' => t("User"),
15
  'description' => t('A single user object.'),
16
  'context' => 'ctools_context_create_user',
17
  'edit form' => 'ctools_context_user_settings_form',
18
  'defaults' => array('type' => 'select', 'uid' => ''),
19
  'keyword' => 'user',
20
  'context name' => 'user',
21
  'convert list' => 'ctools_context_user_convert_list',
22
  'convert' => 'ctools_context_user_convert',
23
  'convert default' => 'name',
24

    
25
  // This context is deprecated and should not be usable in the UI.
26
  'no ui' => TRUE,
27
  'no required context ui' => TRUE,
28
);
29

    
30
/**
31
 * It's important to remember that $conf is optional here, because contexts
32
 * are not always created from the UI.
33
 */
34
function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
35
  $context = new ctools_context(array('entity:user', 'entity', 'user'));
36
  $context->plugin = 'user';
37

    
38
  if ($empty) {
39
    return $context;
40
  }
41

    
42
  if ($conf) {
43
    if ($data['type'] == 'current') {
44
      global $user;
45
      $data = user_load($user->uid);
46
      $data->logged_in_user = TRUE;
47
    }
48
    else {
49
      $data = user_load($data['uid']);
50
    }
51
  }
52
  // Load entity if the data provided is a numeric value. This kind of data is
53
  // passed by some relationships.
54
  if (is_numeric($data)) {
55
    $data = user_load($data);
56
  }
57

    
58
  if (!empty($data)) {
59
    $context->data     = $data;
60
    $context->title    = isset($data->name) ? $data->name : t('Anonymous');
61
    $context->argument = $data->uid;
62
    return $context;
63
  }
64
}
65

    
66
function ctools_context_user_settings_form($form, &$form_state) {
67
  $conf = $form_state['conf'];
68

    
69
  ctools_include('dependent');
70
  $form['type'] = array(
71
    '#title' => t('Enter the context type'),
72
    '#type' => 'radios',
73
    '#options' => array(
74
      'select' => t('Select a user'),
75
      'current' => t('Logged in user'),
76
    ),
77
    '#default_value' => $conf['type'],
78
  );
79

    
80
  $form['user'] = array(
81
    '#title' => t('Enter a user name'),
82
    '#type' => 'textfield',
83
    '#maxlength' => 512,
84
    '#autocomplete_path' => 'user/autocomplete',
85
    '#dependency' => array('radio:type' => array('select')),
86
  );
87

    
88
  if (!empty($conf['uid'])) {
89
    $info = user_load($conf['uid']);
90
    if ($info) {
91
      $form['user']['#description'] = t('Currently set to !link', array('!link' => theme('username', array('account' => $info))));
92
    }
93
  }
94

    
95
  $form['uid'] = array(
96
    '#type' => 'value',
97
    '#value' => $conf['uid'],
98
  );
99

    
100
  $form['set_identifier'] = array(
101
    '#type' => 'checkbox',
102
    '#default_value' => FALSE,
103
    '#title' => t('Reset identifier to username'),
104
    '#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
105
    '#dependency' => array('radio:context[context_settings][type]' => array('select')),
106
  );
107

    
108
  return $form;
109
}
110

    
111
/**
112
 * Validate a user.
113
 */
114
function ctools_context_user_settings_form_validate($form, &$form_state) {
115
  if ($form_state['values']['type'] != 'select') {
116
    return;
117
  }
118

    
119
  // Validate the autocomplete
120
  if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
121
    form_error($form['user'], t('You must select a user.'));
122
    return;
123
  }
124

    
125
  if (empty($form_state['values']['user'])) {
126
    return;
127
  }
128

    
129
  $account = user_load_by_name($form_state['values']['user']);
130

    
131
  if (!$account) {
132
    form_error($form['user'], t('Invalid user selected.'));
133
  }
134
  else {
135
    form_set_value($form['uid'], $account->uid, $form_state);
136
  }
137
}
138

    
139
function ctools_context_user_settings_form_submit($form, &$form_state) {
140
  if ($form_state['values']['set_identifier']) {
141
    $account = user_load($form_state['values']['uid']);
142
    $form_state['values']['identifier'] = $account->name;
143
  }
144

    
145
  $form_state['conf']['type'] = $form_state['values']['type'];
146
  $form_state['conf']['uid'] = $form_state['values']['uid'];
147
}
148

    
149
/**
150
 * Provide a list of replacements.
151
 */
152
function ctools_context_user_convert_list() {
153
  $tokens = token_info();
154
  foreach ($tokens['tokens']['user'] as $id => $info) {
155
    if (!isset($list[$id])) {
156
      $list[$id] = $info['name'];
157
    }
158
  }
159

    
160
  return $list;
161
}
162

    
163
/**
164
 * Convert a context into a string.
165
 */
166
function ctools_context_user_convert($context, $type) {
167
  $tokens = token_info();
168
  if (isset($tokens['tokens']['user'][$type])) {
169
    $values = token_generate('user', array($type => $type), array('user' => $context->data));
170
    if (isset($values[$type])) {
171
      return $values[$type];
172
    }
173
  }
174
}