user.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (user_is_logged_in()) {
  41. $data->logged_in_user = TRUE;
  42. }
  43. }
  44. else {
  45. $data = user_load($data['uid']);
  46. }
  47. }
  48. // Load entity if the data provided is a numeric value. This kind of data is
  49. // passed by some relationships.
  50. if (is_numeric($data)) {
  51. $data = user_load($data);
  52. }
  53. if (!empty($data)) {
  54. $context->data = $data;
  55. $context->title = isset($data->name) ? $data->name : t('Anonymous');
  56. $context->argument = $data->uid;
  57. return $context;
  58. }
  59. }
  60. function ctools_context_user_settings_form($form, &$form_state) {
  61. $conf = $form_state['conf'];
  62. ctools_include('dependent');
  63. $form['type'] = array(
  64. '#title' => t('Enter the context type'),
  65. '#type' => 'radios',
  66. '#options' => array(
  67. 'select' => t('Select a user'),
  68. 'current' => t('Logged in user'),
  69. ),
  70. '#default_value' => $conf['type'],
  71. );
  72. $form['user'] = array(
  73. '#title' => t('Enter a user name'),
  74. '#type' => 'textfield',
  75. '#maxlength' => 512,
  76. '#autocomplete_path' => 'user/autocomplete',
  77. '#dependency' => array('radio:type' => array('select')),
  78. );
  79. if (!empty($conf['uid'])) {
  80. $info = user_load($conf['uid']);
  81. if ($info) {
  82. $form['user']['#description'] = t('Currently set to !link', array('!link' => theme('username', array('account' => $info))));
  83. }
  84. }
  85. $form['uid'] = array(
  86. '#type' => 'value',
  87. '#value' => $conf['uid'],
  88. );
  89. $form['set_identifier'] = array(
  90. '#type' => 'checkbox',
  91. '#default_value' => FALSE,
  92. '#title' => t('Reset identifier to username'),
  93. '#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
  94. '#dependency' => array('radio:context[context_settings][type]' => array('select')),
  95. );
  96. return $form;
  97. }
  98. /**
  99. * Validate a user.
  100. */
  101. function ctools_context_user_settings_form_validate($form, &$form_state) {
  102. if ($form_state['values']['type'] != 'select') {
  103. return;
  104. }
  105. // Validate the autocomplete
  106. if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
  107. form_error($form['user'], t('You must select a user.'));
  108. return;
  109. }
  110. if (empty($form_state['values']['user'])) {
  111. return;
  112. }
  113. $account = user_load_by_name($form_state['values']['user']);
  114. if (!$account) {
  115. form_error($form['user'], t('Invalid user selected.'));
  116. }
  117. else {
  118. form_set_value($form['uid'], $account->uid, $form_state);
  119. }
  120. }
  121. function ctools_context_user_settings_form_submit($form, &$form_state) {
  122. if ($form_state['values']['set_identifier']) {
  123. $account = user_load($form_state['values']['uid']);
  124. $form_state['values']['identifier'] = $account->name;
  125. }
  126. $form_state['conf']['type'] = $form_state['values']['type'];
  127. $form_state['conf']['uid'] = $form_state['values']['uid'];
  128. }
  129. /**
  130. * Provide a list of replacements.
  131. */
  132. function ctools_context_user_convert_list() {
  133. $tokens = token_info();
  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. }