Projet

Général

Profil

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

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

1
<?php
2

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

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

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

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

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

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

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

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

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

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

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

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

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

    
109
  return $form;
110
}
111

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

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

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

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

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

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

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

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

    
162
  return $list;
163
}
164

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