ElggNotification.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Class for notification functionalities
  4. *
  5. * @property int $status The status of the notification (read, unread)
  6. * @property string $event String "action:type:subtype" that can be used
  7. * to add more subjects to a notification later
  8. *
  9. * @package Notifer
  10. */
  11. class ElggNotification extends ElggObject {
  12. const HAS_ACTOR = "hasActor";
  13. const HAS_OBJECT = "hasObject";
  14. /** Override */
  15. protected function initializeAttributes() {
  16. parent::initializeAttributes();
  17. $this->attributes['subtype'] = "notification";
  18. }
  19. /**
  20. * Set the user triggering the notification
  21. *
  22. * @param ElggUser $user User whose action triggered the notification
  23. * @return bool
  24. */
  25. public function setSubject ($user) {
  26. return $this->addRelationship($user->guid, self::HAS_ACTOR);
  27. }
  28. /**
  29. * Set the object involved in the notification
  30. *
  31. * @param ElggEntity $entity Entity that the notification is about
  32. * @return bool
  33. */
  34. public function setTarget ($entity) {
  35. return $this->addRelationship($entity->guid, self::HAS_OBJECT);
  36. }
  37. /**
  38. * Get the object of the notification
  39. *
  40. * @return ElggObject $object
  41. */
  42. public function getTarget () {
  43. $object = $this->getEntitiesFromRelationship(array('relationship' => self::HAS_OBJECT));
  44. if ($object) {
  45. $object = $object[0];
  46. }
  47. return $object;
  48. }
  49. /**
  50. * Get display name of the notification target
  51. *
  52. * @return string $name Display name
  53. */
  54. public function getTargetName() {
  55. $name = elgg_echo('unknown');
  56. $target = $this->getTarget();
  57. if (!$target) {
  58. // This may happen if the target owner changes the ACL
  59. // after the notification has already been created
  60. return $name;
  61. }
  62. $name = $target->getDisplayName();
  63. if (empty($name) && $target->description) {
  64. $name = elgg_get_excerpt($target->description, 20);
  65. }
  66. return $name;
  67. }
  68. /**
  69. * Get the user who triggered the notification
  70. *
  71. * @return ElggUser $subject
  72. */
  73. public function getSubject () {
  74. $subject = $this->getSubjects();
  75. if ($subject) {
  76. $subject = $subject[0];
  77. }
  78. return $subject;
  79. }
  80. /**
  81. * Get all users who participate in the notification
  82. *
  83. * @return ElggUser[]|false
  84. */
  85. public function getSubjects() {
  86. return $this->getEntitiesFromRelationship(array('relationship' => self::HAS_ACTOR));
  87. }
  88. /**
  89. * Mark this notification as read
  90. *
  91. * @return void
  92. */
  93. public function markRead() {
  94. $this->status = 'read';
  95. }
  96. /**
  97. * Mark this notification as unread
  98. *
  99. * @return void
  100. */
  101. public function markUnread() {
  102. $this->status = 'unread';
  103. }
  104. /** Override */
  105. public function save () {
  106. // We are writing to someone else's container so ignore access
  107. $ia = elgg_set_ignore_access(true);
  108. $this->access_id = ACCESS_PRIVATE;
  109. $this->status = 'unread';
  110. $success = parent::save();
  111. elgg_set_ignore_access($ia);
  112. return $success;
  113. }
  114. }