events.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * All event handlers for this plugin a in this file
  4. */
  5. /**
  6. * When a user joins a group
  7. *
  8. * @param string $event join
  9. * @param string $type group
  10. * @param array $params array with the user and the user
  11. *
  12. * @return void
  13. */
  14. function group_tools_join_group_event($event, $type, $params) {
  15. $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethods();
  16. static $auto_notification;
  17. // only load plugin setting once
  18. if (!isset($auto_notification)) {
  19. $auto_notification = array();
  20. if (isset($NOTIFICATION_HANDLERS) && is_array($NOTIFICATION_HANDLERS)) {
  21. if (elgg_get_plugin_setting("auto_notification", "group_tools") == "yes") { // Backwards compatibility
  22. $auto_notification = array("email", "site");
  23. }
  24. foreach ($NOTIFICATION_HANDLERS as $method => $foo) {
  25. if (elgg_get_plugin_setting("auto_notification_" . $method, "group_tools") == "1") {
  26. $auto_notification[] = $method;
  27. }
  28. }
  29. }
  30. }
  31. if (!empty($params) && is_array($params)) {
  32. $group = elgg_extract("group", $params);
  33. $user = elgg_extract("user", $params);
  34. if (($user instanceof ElggUser) && ($group instanceof ElggGroup)) {
  35. // check for the auto notification settings
  36. if (!empty($NOTIFICATION_HANDLERS) && is_array($NOTIFICATION_HANDLERS)) {
  37. foreach ($NOTIFICATION_HANDLERS as $method => $dummy) {
  38. if (in_array($method, $auto_notification)) {
  39. add_entity_relationship($user->getGUID(), "notify" . $method, $group->getGUID());
  40. }
  41. }
  42. }
  43. // cleanup invites
  44. remove_entity_relationship($group->getGUID(), "invited", $user->getGUID());
  45. // and requests
  46. remove_entity_relationship($user->getGUID(), "membership_request", $group->getGUID());
  47. // cleanup email invitations
  48. $options = array(
  49. "annotation_name" => "email_invitation",
  50. "annotation_value" => group_tools_generate_email_invite_code($group->getGUID(), $user->email),
  51. "limit" => false
  52. );
  53. if (elgg_is_logged_in()) {
  54. elgg_delete_annotations($options);
  55. } elseif ($annotations = elgg_get_annotations($options)) {
  56. group_tools_delete_annotations($annotations);
  57. }
  58. // welcome message
  59. $welcome_message = $group->getPrivateSetting("group_tools:welcome_message");
  60. $check_message = trim(strip_tags($welcome_message));
  61. if (!empty($check_message)) {
  62. // replace the place holders
  63. $welcome_message = str_ireplace("[name]", $user->name, $welcome_message);
  64. $welcome_message = str_ireplace("[group_name]", $group->name, $welcome_message);
  65. $welcome_message = str_ireplace("[group_url]", $group->getURL(), $welcome_message);
  66. // notify the user
  67. notify_user($user->getGUID(), $group->getGUID(), elgg_echo("group_tools:welcome_message:subject", array($group->name)), $welcome_message);
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Event when the user joins a site, mostly when registering
  74. *
  75. * @param string $event create
  76. * @param string $type member_of_site
  77. * @param ElggRelationship $relationship the membership relation
  78. *
  79. * @return void
  80. */
  81. function group_tools_join_site_handler($event, $type, $relationship) {
  82. if (!empty($relationship) && ($relationship instanceof ElggRelationship)) {
  83. $user_guid = $relationship->guid_one;
  84. $site_guid = $relationship->guid_two;
  85. $user = get_user($user_guid);
  86. if (!empty($user)) {
  87. // ignore access
  88. $ia = elgg_set_ignore_access(true);
  89. // add user to the auto join groups
  90. $auto_joins = elgg_get_plugin_setting("auto_join", "group_tools");
  91. if (!empty($auto_joins)) {
  92. $auto_joins = string_to_tag_array($auto_joins);
  93. foreach ($auto_joins as $group_guid) {
  94. $group = get_entity($group_guid);
  95. if (!empty($group) && ($group instanceof ElggGroup)) {
  96. if ($group->site_guid == $site_guid) {
  97. // join the group
  98. $group->join($user);
  99. }
  100. }
  101. }
  102. }
  103. // auto detect email invited groups
  104. $groups = group_tools_get_invited_groups_by_email($user->email, $site_guid);
  105. if (!empty($groups)) {
  106. foreach ($groups as $group) {
  107. // join the group
  108. $group->join($user);
  109. }
  110. }
  111. // check for manual email invited groups
  112. $group_invitecode = get_input("group_invitecode");
  113. if (!empty($group_invitecode)) {
  114. $group = group_tools_check_group_email_invitation($group_invitecode);
  115. if (!empty($group)) {
  116. // join the group
  117. $group->join($user);
  118. // cleanup the invite code
  119. $group_invitecode = sanitise_string($group_invitecode);
  120. $options = array(
  121. "guid" => $group->getGUID(),
  122. "annotation_name" => "email_invitation",
  123. "wheres" => array("(v.string = '" . $group_invitecode . "' OR v.string LIKE '" . $group_invitecode . "|%')"),
  124. "annotation_owner_guid" => $group->getGUID(),
  125. "limit" => 1
  126. );
  127. // ignore access in order to cleanup the invitation
  128. $ia = elgg_set_ignore_access(true);
  129. elgg_delete_annotations($options);
  130. // restore access
  131. elgg_set_ignore_access($ia);
  132. }
  133. }
  134. // find domain based groups
  135. $groups = group_tools_get_domain_based_groups($user, $site_guid);
  136. if (!empty($groups)) {
  137. foreach ($groups as $group) {
  138. // join the group
  139. $group->join($user);
  140. }
  141. }
  142. // restore access settings
  143. elgg_set_ignore_access($ia);
  144. }
  145. }
  146. }
  147. /**
  148. * Event to remove the admin role when a user leaves a group
  149. *
  150. * @param string $event leave
  151. * @param string $type group
  152. * @param array $params array with the user and the group
  153. *
  154. * @return void|boolean
  155. */
  156. function group_tools_multiple_admin_group_leave($event, $type, $params) {
  157. if (!empty($params) && is_array($params)) {
  158. if (array_key_exists("group", $params) && array_key_exists("user", $params)) {
  159. $entity = $params["group"];
  160. $user = $params["user"];
  161. if (($entity instanceof ElggGroup) && ($user instanceof ElggUser)) {
  162. if (check_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID())) {
  163. return remove_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID());
  164. }
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * Notify the group admins about a membership request
  171. *
  172. * @param string $event create
  173. * @param string $type membership_request
  174. * @param ElggRelationship $relationship the created membership request relation
  175. *
  176. * @return void
  177. */
  178. function group_tools_membership_request($event, $type, $relationship) {
  179. if (!($relationship instanceof ElggRelationship)) {
  180. return;
  181. }
  182. $group = get_entity($relationship->guid_two);
  183. $user = get_user($relationship->guid_one);
  184. if (!elgg_instanceof($group, 'group') || !elgg_instanceof($user, 'user')) {
  185. return;
  186. }
  187. // only send a message if group admins are allowed
  188. if (!group_tools_multiple_admin_enabled()) {
  189. return;
  190. }
  191. // Notify group admins
  192. $options = array(
  193. "relationship" => "group_admin",
  194. "relationship_guid" => $group->getGUID(),
  195. "inverse_relationship" => true,
  196. "type" => "user",
  197. "limit" => false,
  198. "wheres" => array("e.guid <> " . $group->owner_guid),
  199. );
  200. $admins = elgg_get_entities_from_relationship($options);
  201. if (!empty($admins)) {
  202. $url = elgg_get_site_url() . "groups/requests/" . $group->getGUID();
  203. $subject = elgg_echo("groups:request:subject", array(
  204. $user->name,
  205. $group->name,
  206. ));
  207. foreach ($admins as $a) {
  208. $body = elgg_echo("groups:request:body", array(
  209. $a->name,
  210. $user->name,
  211. $group->name,
  212. $user->getURL(),
  213. $url,
  214. ));
  215. notify_user($a->getGUID(), $user->getGUID(), $subject, $body);
  216. }
  217. }
  218. }