invite.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Invite a user to join a group
  4. *
  5. * @package ElggGroups
  6. */
  7. elgg_make_sticky_form('group_invite');
  8. $logged_in_user = elgg_get_logged_in_user_entity();
  9. $user_guids = get_input("user_guid");
  10. if (!empty($user_guids) && !is_array($user_guids)) {
  11. $user_guids = array($user_guids);
  12. }
  13. $adding = false;
  14. if (elgg_is_admin_logged_in()) {
  15. // add all users?
  16. if (get_input("all_users") == "yes") {
  17. $site = elgg_get_site_entity();
  18. $options = array(
  19. "limit" => false,
  20. "callback" => "group_tools_guid_only_callback"
  21. );
  22. $user_guids = $site->getMembers($options);
  23. }
  24. // add users directly?
  25. if (get_input("submit") == elgg_echo("group_tools:add_users")) {
  26. $adding = true;
  27. }
  28. }
  29. $group_guid = (int) get_input("group_guid");
  30. $text = get_input("comment");
  31. $emails = get_input("user_guid_email");
  32. if (!empty($emails) && !is_array($emails)) {
  33. $emails = array($emails);
  34. }
  35. $csv = get_uploaded_file("csv");
  36. if (get_input("resend") == "yes") {
  37. $resend = true;
  38. } else {
  39. $resend = false;
  40. }
  41. $group = get_entity($group_guid);
  42. if ((!empty($user_guids) || !empty($emails) || !empty($csv)) && !empty($group)) {
  43. if (($group instanceof ElggGroup) && ($group->canEdit() || group_tools_allow_members_invite($group))) {
  44. // show hidden (unvalidated) users
  45. $hidden = access_get_show_hidden_status();
  46. access_show_hidden_entities(true);
  47. // counters
  48. $already_invited = 0;
  49. $invited = 0;
  50. $member = 0;
  51. $join = 0;
  52. // invite existing users
  53. if (!empty($user_guids)) {
  54. if (!$adding) {
  55. // invite users
  56. foreach ($user_guids as $u_id) {
  57. $user = get_user($u_id);
  58. if (!empty($user)) {
  59. if (!$group->isMember($user)) {
  60. if (!check_entity_relationship($group->getGUID(), "invited", $user->getGUID()) || $resend) {
  61. if (group_tools_invite_user($group, $user, $text, $resend)) {
  62. $invited++;
  63. }
  64. } else {
  65. // user was already invited
  66. $already_invited++;
  67. }
  68. } else {
  69. $member++;
  70. }
  71. }
  72. }
  73. } else {
  74. // add users directly
  75. foreach ($user_guids as $u_id) {
  76. $user = get_user($u_id);
  77. if (!empty($user)) {
  78. if (!$group->isMember($user)) {
  79. if (group_tools_add_user($group, $user, $text)) {
  80. $join++;
  81. }
  82. } else {
  83. $member++;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. // Invite member by e-mail address
  90. if (!empty($emails)) {
  91. foreach ($emails as $email) {
  92. $invite_result = group_tools_invite_email($group, $email, $text, $resend);
  93. if ($invite_result === true) {
  94. $invited++;
  95. } elseif ($invite_result === null) {
  96. $already_invited++;
  97. }
  98. }
  99. }
  100. // invite from csv
  101. if (!empty($csv)) {
  102. $file_location = $_FILES["csv"]["tmp_name"];
  103. $fh = fopen($file_location, "r");
  104. if (!empty($fh)) {
  105. while (($data = fgetcsv($fh, 0, ";")) !== false) {
  106. /*
  107. * data structure
  108. * data[0] => displayname
  109. * data[1] => e-mail address
  110. */
  111. $email = "";
  112. if (isset($data[1])) {
  113. $email = trim($data[1]);
  114. }
  115. if (!empty($email) && is_email_address($email)) {
  116. $users = get_user_by_email($email);
  117. if (!empty($users)) {
  118. // found a user with this email on the site, so invite (or add)
  119. $user = $users[0];
  120. if (!$group->isMember($user)) {
  121. if (!$adding) {
  122. if (!check_entity_relationship($group->getGUID(), "invited", $user->getGUID()) || $resend) {
  123. // invite user
  124. if (group_tools_invite_user($group, $user, $text, $resend)) {
  125. $invited++;
  126. }
  127. } else {
  128. // user was already invited
  129. $already_invited++;
  130. }
  131. } else {
  132. if (group_tools_add_user($group, $user, $text)) {
  133. $join++;
  134. }
  135. }
  136. } else {
  137. $member++;
  138. }
  139. } else {
  140. // user not found so invite based on email address
  141. $invite_result = group_tools_invite_email($group, $email, $text, $resend);
  142. if ($invite_result === true) {
  143. $invited++;
  144. } elseif ($invite_result === null) {
  145. $already_invited++;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. // restore hidden users
  153. access_show_hidden_entities($hidden);
  154. // which message to show
  155. if (!empty($invited) || !empty($join)) {
  156. elgg_clear_sticky_form('group_invite');
  157. if (!$adding) {
  158. system_message(elgg_echo("group_tools:action:invite:success:invite", array($invited, $already_invited, $member)));
  159. } else {
  160. system_message(elgg_echo("group_tools:action:invite:success:add", array($join, $already_invited, $member)));
  161. }
  162. } else {
  163. if (!$adding) {
  164. register_error(elgg_echo("group_tools:action:invite:error:invite", array($already_invited, $member)));
  165. } else {
  166. register_error(elgg_echo("group_tools:action:invite:error:add", array($already_invited, $member)));
  167. }
  168. }
  169. } else {
  170. register_error(elgg_echo("group_tools:action:error:edit"));
  171. }
  172. } else {
  173. register_error(elgg_echo("group_tools:action:error:input"));
  174. }
  175. forward(REFERER);