Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. <?php
  2.  
  3. // In my extension it was good enough to hardcode in a constant. If desirable, one or more special group
  4. // IDs could be stored in a CiviCRM setting.
  5. define('EXAMPLEEXTENSION_GROUP_ID', 44);
  6.  
  7. /**
  8. * Implements hook_civicrm_post().
  9. */
  10. function exampleextension_civicrm_post($op, $objectName, $groupId, &$contactIds) {
  11. if ($op === 'create' && $objectName === 'GroupContact' && $groupId == EXAMPLEEXTENSION_GROUP_ID) {
  12. foreach ($contactIds as $cid) {
  13. try {
  14. $email = civicrm_api3('Contact', 'getvalue', array(
  15. 'id' => $cid,
  16. 'return' => 'email',
  17. ));
  18. } catch (Exception $e) {
  19. continue;
  20. }
  21.  
  22. // it's theoretically possible that a Drupal user exists with the given email
  23. // address but that it doesn't have a UFJoin, so we'll check for the existence
  24. // of the actual account
  25. if (!user_load_by_mail($email)) {
  26. $drupalUser = _ee_createAccount($email);
  27. if ($drupalUser) {
  28. $operation = 'register_no_approval_required';
  29. _user_mail_notify($operation, $drupalUser);
  30. }
  31. }
  32. }
  33. }
  34. }
  35.  
  36. /**
  37. * Creates a Drupal user.
  38. *
  39. * Assumes that the nonexistence of the Drupal user has already been validated.
  40. *
  41. * @param string $email
  42. * @return mixed
  43. * Drupal user object if a new user was created,
  44. * FALSE if creation failed
  45. */
  46. function _ee_createAccount($email) {
  47. $result = FALSE;
  48.  
  49. $params = array(
  50. 'init' => $email,
  51. 'name' => $email,
  52. 'mail' => $email,
  53. 'pass' => md5(uniqid(rand(), true)),
  54. 'status' => 1,
  55. );
  56. $result = user_save(NULL, $params);
  57.  
  58. return $result;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement