user.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a user context.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("User"),
  12. 'description' => t('A single user object.'),
  13. 'context' => 'ctools_context_create_user',
  14. 'edit form' => 'ctools_context_user_settings_form',
  15. 'defaults' => array('type' => 'select', 'uid' => ''),
  16. 'keyword' => 'user',
  17. 'context name' => 'user',
  18. 'convert list' => 'ctools_context_user_convert_list',
  19. 'convert' => 'ctools_context_user_convert',
  20. 'convert default' => 'name',
  21. // This context is deprecated and should not be usable in the UI.
  22. 'no ui' => TRUE,
  23. 'no required context ui' => TRUE,
  24. );
  25. /**
  26. * It's important to remember that $conf is optional here, because contexts
  27. * are not always created from the UI.
  28. */
  29. function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
  30. $context = new ctools_context(array('entity:user', 'entity', 'user'));
  31. $context->plugin = 'user';
  32. if ($empty) {
  33. return $context;
  34. }
  35. if ($conf) {
  36. if ($data['type'] == 'current') {
  37. global $user;
  38. $data = user_load($user->uid);
  39. if (user_is_logged_in()) {
  40. $data->logged_in_user = TRUE;
  41. }
  42. }
  43. else {
  44. $data = user_load($data['uid']);
  45. }
  46. }
  47. // Load entity if the data provided is a numeric value. This kind of data is
  48. // passed by some relationships.
  49. if (is_numeric($data)) {
  50. $data = user_load($data);
  51. }
  52. if (!empty($data)) {
  53. $context->data = $data;
  54. $context->title = isset($data->name) ? $data->name : t('Anonymous');
  55. $context->argument = $data->uid;
  56. return $context;
  57. }
  58. }
  59. function ctools_context_user_settings_form($form, &$form_state) {
  60. $conf = $form_state['conf'];
  61. ctools_include('dependent');
  62. $form['type'] = array(
  63. '#title' => t('Enter the context type'),
  64. '#type' => 'radios',
  65. '#options' => array(
  66. 'select' => t('Select a user'),
  67. 'current' => t('Logged in user'),
  68. ),
  69. '#default_value' => $conf['type'],
  70. );
  71. $form['user'] = array(
  72. '#title' => t('Enter a user name'),
  73. '#type' => 'textfield',
  74. '#maxlength' => 512,
  75. '#autocomplete_path' => 'user/autocomplete',
  76. '#dependency' => array('radio:type' => array('select')),
  77. );
  78. if (!empty($conf['uid'])) {
  79. $info = user_load($conf['uid']);
  80. if ($info) {
  81. $form['user']['#description'] = t('Currently set to !link', array('!link' => theme('username', array('account' => $info))));
  82. }
  83. }
  84. $form['uid'] = array(
  85. '#type' => 'value',
  86. '#value' => $conf['uid'],
  87. );
  88. $form['set_identifier'] = array(
  89. '#type' => 'checkbox',
  90. '#default_value' => FALSE,
  91. '#title' => t('Reset identifier to username'),
  92. '#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
  93. '#dependency' => array('radio:context[context_settings][type]' => array('select')),
  94. );
  95. return $form;
  96. }
  97. /**
  98. * Validate a user.
  99. */
  100. function ctools_context_user_settings_form_validate($form, &$form_state) {
  101. if ($form_state['values']['type'] != 'select') {
  102. return;
  103. }
  104. // Validate the autocomplete.
  105. if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
  106. form_error($form['user'], t('You must select a user.'));
  107. return;
  108. }
  109. if (empty($form_state['values']['user'])) {
  110. return;
  111. }
  112. $account = user_load_by_name($form_state['values']['user']);
  113. if (!$account) {
  114. form_error($form['user'], t('Invalid user selected.'));
  115. }
  116. else {
  117. form_set_value($form['uid'], $account->uid, $form_state);
  118. }
  119. }
  120. function ctools_context_user_settings_form_submit($form, &$form_state) {
  121. if ($form_state['values']['set_identifier']) {
  122. $account = user_load($form_state['values']['uid']);
  123. $form_state['values']['identifier'] = $account->name;
  124. }
  125. $form_state['conf']['type'] = $form_state['values']['type'];
  126. $form_state['conf']['uid'] = $form_state['values']['uid'];
  127. }
  128. /**
  129. * Provide a list of replacements.
  130. */
  131. function ctools_context_user_convert_list() {
  132. $tokens = token_info();
  133. $list = array();
  134. foreach ($tokens['tokens']['user'] as $id => $info) {
  135. if (!isset($list[$id])) {
  136. $list[$id] = $info['name'];
  137. }
  138. }
  139. return $list;
  140. }
  141. /**
  142. * Convert a context into a string.
  143. */
  144. function ctools_context_user_convert($context, $type) {
  145. $tokens = token_info();
  146. if (isset($tokens['tokens']['user'][$type])) {
  147. $values = token_generate('user', array($type => $type), array('user' => $context->data));
  148. if (isset($values[$type])) {
  149. return $values[$type];
  150. }
  151. }
  152. }