Event.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Elgg\Notifications;
  3. /**
  4. * Notification event
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Notifications
  8. */
  9. class Event {
  10. /* @var string The name of the action/event */
  11. protected $action;
  12. /* @var string The type of the action's object */
  13. protected $object_type;
  14. /* @var string the subtype of the action's object */
  15. protected $object_subtype;
  16. /* @var int The identifier of the object (GUID for entity) */
  17. protected $object_id;
  18. /* @var int The GUID of the user who triggered the event */
  19. protected $actor_guid;
  20. /**
  21. * Create a notification event
  22. *
  23. * @param \ElggData $object The object of the event (\ElggEntity)
  24. * @param string $action The name of the action (default: create)
  25. * @param \ElggEntity $actor The entity that caused the event (default: logged in user)
  26. *
  27. * @throws \InvalidArgumentException
  28. */
  29. public function __construct(\ElggData $object, $action, \ElggEntity $actor = null) {
  30. if (elgg_instanceof($object)) {
  31. $this->object_type = $object->getType();
  32. $this->object_subtype = $object->getSubtype();
  33. $this->object_id = $object->getGUID();
  34. } else {
  35. $this->object_type = $object->getType();
  36. $this->object_subtype = $object->getSubtype();
  37. $this->object_id = $object->id;
  38. }
  39. if ($actor == null) {
  40. $this->actor_guid = _elgg_services()->session->getLoggedInUserGuid();
  41. } else {
  42. $this->actor_guid = $actor->getGUID();
  43. }
  44. $this->action = $action;
  45. }
  46. /**
  47. * Get the actor of the event
  48. *
  49. * @return \ElggEntity|false
  50. */
  51. public function getActor() {
  52. return get_entity($this->actor_guid);
  53. }
  54. /**
  55. * Get the GUID of the actor
  56. *
  57. * @return int
  58. */
  59. public function getActorGUID() {
  60. return $this->actor_guid;
  61. }
  62. /**
  63. * Get the object of the event
  64. *
  65. * @return \ElggData
  66. */
  67. public function getObject() {
  68. switch ($this->object_type) {
  69. case 'object':
  70. case 'user':
  71. case 'site':
  72. case 'group':
  73. return get_entity($this->object_id);
  74. break;
  75. case 'relationship':
  76. return get_relationship($this->object_id);
  77. break;
  78. case 'annotation':
  79. return elgg_get_annotation_from_id($this->object_id);
  80. break;
  81. }
  82. return null;
  83. }
  84. /**
  85. * Get the name of the action
  86. *
  87. * @return string
  88. */
  89. public function getAction() {
  90. return $this->action;
  91. }
  92. /**
  93. * Get a description of the event
  94. *
  95. * @return string
  96. */
  97. public function getDescription() {
  98. return "{$this->action}:{$this->object_type}:{$this->object_subtype}";
  99. }
  100. }
  101. /**
  102. * Notification event
  103. *
  104. * @package Elgg.Core
  105. * @subpackage Notifications
  106. * @since 1.9.0
  107. */
  108. class Elgg_Notifications_Event extends \Elgg\Notifications\Event {}