user_edit_form.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Plugin to provide a user_edit_form 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 edit form'),
  13. 'description' => t('A user edit form.'),
  14. 'context' => 'ctools_context_create_user_edit_form',
  15. 'edit form' => 'ctools_context_user_edit_form_settings_form',
  16. 'defaults' => array('uid' => ''),
  17. 'keyword' => 'user_edit',
  18. 'context name' => 'user_edit_form',
  19. 'convert list' => 'ctools_context_user_edit_convert_list',
  20. 'convert' => 'ctools_context_user_edit_convert',
  21. 'placeholder form' => array(
  22. '#type' => 'textfield',
  23. '#description' => t('Enter the user ID of a user for this argument:'),
  24. ),
  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_edit_form($empty, $user = NULL, $conf = FALSE) {
  31. // Determine the user category.
  32. $category = !empty($conf['category']) ? $conf['category'] : FALSE;
  33. unset($conf['category']);
  34. // If no category was specified, use the default 'account'.
  35. if (!$category) {
  36. $category = 'account';
  37. }
  38. // Return previously created contexts, per category.
  39. static $created = array();
  40. if (!empty($created[$category])) {
  41. return $created[$category];
  42. }
  43. $context = new ctools_context(array('form', 'user_edit', 'user_form', 'user_edit_form', 'user', 'entity:user'));
  44. // Store this context for later.
  45. $created[$category] = $context;
  46. $context->plugin = 'user_edit_form';
  47. if ($empty) {
  48. return $context;
  49. }
  50. if (!empty($conf)) {
  51. // In this case, $user is actually our $conf array.
  52. $uid = is_array($user) && isset($user['uid']) ? $user['uid'] : (is_object($user) ? $user->uid : 0);
  53. if (module_exists('translation')) {
  54. if ($translation = module_invoke('translation', 'user_uid', $uid, $GLOBALS['language']->language)) {
  55. $uid = $translation;
  56. $reload = TRUE;
  57. }
  58. }
  59. if (is_array($user) || !empty($reload)) {
  60. $user = user_load($uid);
  61. }
  62. }
  63. if (!empty($user)) {
  64. $form_id = 'user_profile_form';
  65. $form_state = array('want form' => TRUE, 'build_info' => array('args' => array($user, $category)));
  66. $file = drupal_get_path('module', 'user') . '/user.pages.inc';
  67. require_once DRUPAL_ROOT . '/' . $file;
  68. // This piece of information can let other modules know that more files
  69. // need to be included if this form is loaded from cache:
  70. $form_state['build_info']['files'] = array($file);
  71. $form = drupal_build_form($form_id, $form_state);
  72. // Fill in the 'node' portion of the context
  73. $context->data = $user;
  74. $context->title = isset($user->name) ? $user->name : '';
  75. $context->argument = $user->uid;
  76. $context->form = $form;
  77. $context->form_state = &$form_state;
  78. $context->form_id = $form_id;
  79. $context->form_title = isset($user->name) ? $user->name : '';
  80. $context->restrictions['form'] = array('form');
  81. return $context;
  82. }
  83. }
  84. function ctools_context_user_edit_form_settings_form($form, &$form_state) {
  85. $conf = &$form_state['conf'];
  86. $form['user'] = array(
  87. '#title' => t('Enter the name or UID of a node'),
  88. '#type' => 'textfield',
  89. '#maxlength' => 512,
  90. '#autocomplete_path' => 'ctools/autocomplete/user',
  91. '#weight' => -10,
  92. );
  93. if (!empty($conf['uid'])) {
  94. $info = db_query('SELECT * FROM {users} WHERE uid = :uid', array(':uid' => $conf['uid']))->fetchObject();
  95. if ($info) {
  96. $link = l(t("'%name' [user id %uid]", array('%name' => $info->name, '%uid' => $info->uid)), "user/$info->uid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
  97. $form['user']['#description'] = t('Currently set to !link', array('!link' => $link));
  98. }
  99. }
  100. $form['uid'] = array(
  101. '#type' => 'value',
  102. '#value' => $conf['uid'],
  103. );
  104. $form['set_identifier'] = array(
  105. '#type' => 'checkbox',
  106. '#default_value' => FALSE,
  107. '#title' => t('Reset identifier to user name'),
  108. '#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
  109. );
  110. return $form;
  111. }
  112. /**
  113. * Validate a node.
  114. */
  115. function ctools_context_user_edit_form_settings_form_validate($form, &$form_state) {
  116. // Validate the autocomplete
  117. if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
  118. form_error($form['user'], t('You must select a user.'));
  119. return;
  120. }
  121. if (empty($form_state['values']['user'])) {
  122. return;
  123. }
  124. $uid = $form_state['values']['user'];
  125. $preg_matches = array();
  126. $match = preg_match('/\[id: (\d+)\]/', $uid, $preg_matches);
  127. if (!$match) {
  128. $match = preg_match('/^id: (\d+)/', $uid, $preg_matches);
  129. }
  130. if ($match) {
  131. $uid = $preg_matches[1];
  132. }
  133. if (is_numeric($uid)) {
  134. $user = db_query('SELECT uid FROM {users} WHERE uid = :uid', array(':uid' => $uid))->fetchObject();
  135. }
  136. else {
  137. $user = db_query('SELECT uid FROM {users} WHERE LOWER(name) = LOWER(:name)', array(':name' => $uid))->fetchObject();
  138. }
  139. form_set_value($form['uid'], $user->uid, $form_state);
  140. }
  141. function ctools_context_user_edit_form_settings_form_submit($form, &$form_state) {
  142. if ($form_state['values']['set_identifier']) {
  143. $user = user_load($form_state['values']['uid']);
  144. $form_state['values']['identifier'] = $user->name;
  145. }
  146. // This will either be the value set previously or a value set by the
  147. // validator.
  148. $form_state['conf']['uid'] = $form_state['values']['uid'];
  149. }
  150. /**
  151. * Provide a list of ways that this context can be converted to a string.
  152. */
  153. function ctools_context_user_edit_convert_list() {
  154. // Pass through to the "node" context convert list.
  155. $plugin = ctools_get_context('user');
  156. return ctools_context_user_convert_list();
  157. }
  158. /**
  159. * Convert a context into a string.
  160. */
  161. function ctools_context_user_edit_convert($context, $type) {
  162. // Pass through to the "node" context convert list.
  163. $plugin = ctools_get_context('user');
  164. return ctools_context_user_convert($context, $type);
  165. }