user.inc 4.5 KB

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