notifications.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Enable or disable group notifications for all members
  4. */
  5. $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethods();
  6. $toggle = get_input("toggle");
  7. $guid = (int) get_input("guid");
  8. $forward_url = REFERER;
  9. if (!empty($guid) && !empty($toggle)) {
  10. $group = get_entity($guid);
  11. if (!empty($group) && elgg_instanceof($group, "group")) {
  12. // get group members
  13. $members = $group->getMembers(array("count" => true));
  14. if (!empty($members)) {
  15. $options = array(
  16. "type" => "user",
  17. "relationship" => "member",
  18. "relationship_guid" => $group->getGUID(),
  19. "inverse_relationship" => true,
  20. "limit" => false,
  21. );
  22. $members = new ElggBatch("elgg_get_entities_from_relationship", $options);
  23. if ($toggle == "enable") {
  24. // fix notifications settings for site amd email
  25. $auto_notification_handlers = array(
  26. "site",
  27. "email"
  28. );
  29. // enable notification for everyone
  30. foreach ($members as $member) {
  31. foreach ($NOTIFICATION_HANDLERS as $method => $dummy) {
  32. if (in_array($method, $auto_notification_handlers)) {
  33. add_entity_relationship($member->getGUID(), "notify" . $method, $group->getGUID());
  34. }
  35. }
  36. }
  37. system_message(elgg_echo("group_tools:action:notifications:success:enable"));
  38. $forward_url = $group->getURL();
  39. } elseif ($toggle == "disable") {
  40. // disable notification for everyone
  41. foreach ($members as $member) {
  42. foreach ($NOTIFICATION_HANDLERS as $method => $dummy) {
  43. remove_entity_relationship($member->getGUID(), "notify" . $method, $group->getGUID());
  44. }
  45. }
  46. system_message(elgg_echo("group_tools:action:notifications:success:disable"));
  47. $forward_url = $group->getURL();
  48. } else {
  49. register_error(elgg_echo("group_tools:action:notifications:error:toggle"));
  50. }
  51. }
  52. } else {
  53. register_error(elgg_echo("group_tools:action:error:entity"));
  54. }
  55. } else {
  56. register_error(elgg_echo("group_tools:action:error:input"));
  57. }
  58. forward($forward_url);