profile_sync_email.module 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Implements hook_permission()
  4. */
  5. function profile_sync_email_permission() {
  6. return array(
  7. 'administer profile sync email' => array(
  8. 'title' => t('Administer Profile2 sync email'),
  9. 'description' => t('Select profile email field to sync to user account email.'),
  10. ),
  11. );
  12. } // profile_sync_email_permission()
  13. /**
  14. * Implements hook_form_FORM_ID_alter()
  15. */
  16. function profile_sync_email_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  17. // if this is an email field on a profile
  18. if ($form['#field']['type'] == 'email' && $form['instance']['entity_type']['#value'] == 'profile2') {
  19. $sync_field = variable_get('profile_sync_email_instance', -1);
  20. $form['profile_sync_email'] = array(
  21. '#type' => 'checkbox',
  22. '#title' => t('Sync this email field to the user account'),
  23. '#description' => t('When checked, this field will update the user account email and vice versa.'),
  24. '#default_value' => ($sync_field == $form['#instance']['id'])? 1 : 0,
  25. );
  26. $form['#submit'][] = 'profile_sync_email_submit';
  27. } // if email on a profile
  28. } // profile_sync_email_form_alter()
  29. /**
  30. * form submit
  31. */
  32. function profile_sync_email_submit($form, &$form_state) {
  33. if (isset($form_state['values']['profile_sync_email']) && $form_state['values']['profile_sync_email'] == 1) {
  34. variable_set('profile_sync_email_instance', $form['#instance']['id']);
  35. } // if
  36. } // profile_sync_email_submit()
  37. /**
  38. * hook_form_alter()
  39. */
  40. function profile_sync_email_form_alter(&$form, &$form_state, $form_id) {
  41. // get the sync field info
  42. $instance_info = profile_sync_email_instance_info();
  43. // if this form is for the profile
  44. if (isset($instance_info->bundle) && isset($instance_info->field_name) && isset($form['profile_' . $instance_info->bundle][$instance_info->field_name])) {
  45. if (!isset($form_state['build_info']['args'][0]->uid) || !is_numeric($form_state['build_info']['args'][0]->uid)) return; // no user, end here
  46. // load the user and set the email field
  47. $user = user_load($form_state['build_info']['args'][0]->uid);
  48. $field = &$form['profile_' . $instance_info->bundle][$instance_info->field_name];
  49. $field[$field['#language']][0]['email']['#default_value'] = $user->mail;
  50. // add validation on the email field
  51. $field[$field['#language']][0]['email']['#element_validate'][] = 'profile_sync_email_validate_email';
  52. } // if field exists
  53. } // profile_sync_email_form_alter()
  54. /**
  55. * validation callback
  56. */
  57. function profile_sync_email_validate_email($element, &$form_state, $form) {
  58. if (!empty($element['#value'])) {
  59. $email_user = user_load_by_mail($element['#value']);
  60. if (isset($email_user->uid) && $email_user->uid != $form_state['build_info']['args'][0]->uid) {
  61. form_error($element, t('The email address %email is being used by another user', array('%email' => $element['#value'])));
  62. }
  63. }
  64. } // profile_sync_email_validate_email()
  65. /**
  66. * hook_profile2_insert(), hook_profile2_update()
  67. */
  68. function profile_sync_email_profile2_insert($profile) {
  69. profile_sync_email_profile2($profile);
  70. } // profile_sync_email_profile2_insert()
  71. function profile_sync_email_profile2_update($profile) {
  72. profile_sync_email_profile2($profile);
  73. } // profile_sync_email_profile2_update()
  74. function profile_sync_email_profile2($profile) {
  75. $sync_field = profile_sync_email_instance_info();
  76. $user = user_load($profile->uid);
  77. if (isset($profile->{$sync_field->field_name}[LANGUAGE_NONE][0]['email'])) {
  78. $email = $profile->{$sync_field->field_name}[LANGUAGE_NONE][0]['email'];
  79. } else {
  80. $email = '';
  81. }
  82. if ($user->mail != $email && !empty($email) && (!isset($_SESSION['profile_sync_email_profile']) || !$_SESSION['profile_sync_email_profile'])) {
  83. $user->mail = $email;
  84. $_SESSION['profile_sync_email_profile'] = TRUE;
  85. user_save($user);
  86. $_SESSION['profile_sync_email_profile'] = FALSE;
  87. }
  88. } // profile_sync_email_profile2()
  89. /**
  90. * Update the profile whenever the user is updated
  91. * hook_user_insert(), hook_user_update()
  92. */
  93. function profile_sync_email_user_insert(&$edit, $account, $category) {
  94. // test for a profile
  95. $sync_field = profile_sync_email_instance_info();
  96. profile_sync_email_create_profile($account, $sync_field->bundle);
  97. // sync the profile
  98. profile_sync_email_user($edit, $account, $category);
  99. } // profile_sync_email_user_insert()
  100. function profile_sync_email_user_update(&$edit, $account, $category) {
  101. // test for a profile
  102. $sync_field = profile_sync_email_instance_info();
  103. profile_sync_email_create_profile($account, $sync_field->bundle);
  104. // sync the profile
  105. profile_sync_email_user($edit, $account, $category);
  106. } // profile_sync_email_user_update()
  107. function profile_sync_email_user(&$edit, $account, $category) {
  108. $sync_field = profile_sync_email_instance_info();
  109. $profile = profile2_load_by_user($account, $sync_field->bundle);
  110. if ($profile) {
  111. if (isset($profile->{$sync_field->field_name}[LANGUAGE_NONE][0]['email'])) {
  112. $email = &$profile->{$sync_field->field_name}[LANGUAGE_NONE][0]['email'];
  113. } else {
  114. $email = '';
  115. }
  116. if (isset($edit['mail']) && $email != $edit['mail'] && !empty($edit['mail']) && (!isset($_SESSION['profile_sync_email_user']) || !$_SESSION['profile_sync_email_user'])) {
  117. $email = $edit['mail'];
  118. $_SESSION['profile_sync_email_user'] = TRUE; // set lock
  119. profile2_save($profile);
  120. $_SESSION['profile_sync_email_user'] = FALSE; // remove lock
  121. }
  122. } // if $profile
  123. } // profile_sync_email_user()
  124. /**
  125. * create a profile for the user
  126. */
  127. function profile_sync_email_create_profile($user, $bundle) {
  128. // get the user profile
  129. $profile = profile2_load_by_user($user, $bundle);
  130. // if a profile exists, exit here
  131. if ($profile) return;
  132. // no profile exists, check for an anonymous profile with a matching email
  133. $sync_field = profile_sync_email_instance_info();
  134. $pid_query = db_select('profile', 'p')
  135. ->fields('p', array('pid'))
  136. ->condition('p.uid', 0)
  137. ->condition('fd.' . $sync_field->field_name . '_email', $user->mail);
  138. $pid_query->join('field_data_' . $sync_field->field_name, 'fd',
  139. 'fd.entity_id = p.pid AND fd.entity_type = :entity_type AND fd.bundle = :bundle',
  140. array(':entity_type' => $sync_field->entity_type, ':bundle' => $sync_field->bundle));
  141. $pid = $pid_query->execute()->fetchField();
  142. if ($pid) {
  143. $profile = profile2_load($pid);
  144. $profile->uid = $user->uid;
  145. profile2_save($profile);
  146. return;
  147. }
  148. // build the profile
  149. $profile_values = array(
  150. 'type' => $bundle,
  151. 'uid' => $user->uid,
  152. );
  153. $fields = field_info_instances('profile2', $bundle);
  154. foreach (array_keys($fields) as $field_name) {
  155. $profile_values[$field_name] = array();
  156. }
  157. $profile = profile_create($profile_values);
  158. profile2_save($profile);
  159. } // profile_sync_email_create_profile()
  160. /**
  161. * helper function to return info about the sync field
  162. */
  163. function profile_sync_email_instance_info() {
  164. $sync_field = variable_get('profile_sync_email_instance', -1);
  165. $instance_info = FALSE;
  166. if ($sync_field) {
  167. $instance_info = db_select('field_config_instance', 'fci')
  168. ->fields('fci', array('field_name', 'entity_type', 'bundle'))
  169. ->condition('id', $sync_field)
  170. ->execute()
  171. ->fetch();
  172. }
  173. return $instance_info;
  174. } // profile_sync_email_instance_info()