events.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * All plugin events are bundled here
  4. */
  5. /**
  6. * Listen to the creation of metadata
  7. *
  8. * @param string $event the name of the event
  9. * @param string $type the type of the event
  10. * @param ElggMetadata $metadata supplied metadata
  11. *
  12. * @return void
  13. */
  14. function tag_tools_create_metadata_event_handler($event, $type, $metadata) {
  15. if (empty($metadata) || !($metadata instanceof ElggMetadata)) {
  16. return;
  17. }
  18. // is it a tag
  19. if ($metadata->name != "tags") {
  20. return;
  21. }
  22. // get the entity for further use
  23. $ia = elgg_set_ignore_access(true);
  24. $entity_guid = $metadata->entity_guid;
  25. // can't use elgg get entity because cache is not correctly updated
  26. $entity_row = get_entity_as_row($entity_guid);
  27. elgg_set_ignore_access($ia);
  28. // shortcut for private entities
  29. if ($entity_row->access_id == ACCESS_PRIVATE) {
  30. return;
  31. }
  32. // only send notifications on creation of the entity
  33. $time_created_treshold = 5;
  34. if ($entity_row->time_created < (time() - $time_created_treshold)) {
  35. // assume it is an update
  36. return;
  37. }
  38. // check of the entity is allowed for notifications
  39. if (!tag_tools_is_notification_entity($entity_row->guid)) {
  40. return;
  41. }
  42. $tag = $metadata->value;
  43. $options = array(
  44. "type" => "user",
  45. "annotation_name_value_pairs" => array(
  46. "name" => "follow_tag",
  47. "value" => $tag
  48. ),
  49. "limit" => false
  50. );
  51. $ia = elgg_set_ignore_access(true);
  52. $dbprefix = elgg_get_config("dbprefix");
  53. $entities = new ElggBatch("elgg_get_entities_from_annotations", $options);
  54. foreach ($entities as $user) {
  55. // check if not trying to notify the owner
  56. if ($user->getGUID() == $entity_row->owner_guid) {
  57. continue;
  58. }
  59. // force a correct access bit
  60. elgg_set_ignore_access(false);
  61. // check access for the user, can't use has_access_to_entity
  62. // because that requires a full entity
  63. $access_bit = _elgg_get_access_where_sql(array("user_guid" => $user->getGUID()));
  64. // ignore access to get the correct next user
  65. elgg_set_ignore_access(true);
  66. // build correct query to check access
  67. $query = "SELECT guid FROM {$dbprefix}entities e
  68. WHERE e.guid = {$entity_guid}
  69. AND {$access_bit}";
  70. if (get_data($query)) {
  71. // regsiter shutdown function because we need the full entity
  72. // this is a workaround and should be reviewed in the near future
  73. register_shutdown_function("tag_tools_notify_user", $user->getGUID(), $entity_row->guid, $tag);
  74. }
  75. }
  76. elgg_set_ignore_access($ia);
  77. }