ElggAnnotation.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Elgg Annotations
  4. *
  5. * Annotations allow you to attach bits of information to entities. They are
  6. * essentially the same as metadata, but with additional helper functions for
  7. * performing calculations.
  8. *
  9. * @note Internal: Annotations are stored in the annotations table.
  10. *
  11. * @package Elgg.Core
  12. * @subpackage DataModel.Annotations
  13. */
  14. class ElggAnnotation extends \ElggExtender {
  15. /**
  16. * (non-PHPdoc)
  17. *
  18. * @see \ElggData::initializeAttributes()
  19. *
  20. * @return void
  21. */
  22. protected function initializeAttributes() {
  23. parent::initializeAttributes();
  24. $this->attributes['type'] = 'annotation';
  25. }
  26. /**
  27. * Construct a new annotation object
  28. *
  29. * Plugin developers will probably never use the constructor.
  30. * See \ElggEntity for its API for adding annotations.
  31. *
  32. * @param \stdClass $row Database row as \stdClass object
  33. */
  34. public function __construct($row = null) {
  35. $this->initializeAttributes();
  36. if (!empty($row)) {
  37. // Create from db row
  38. if ($row instanceof \stdClass) {
  39. $annotation = $row;
  40. $objarray = (array) $annotation;
  41. foreach ($objarray as $key => $value) {
  42. $this->attributes[$key] = $value;
  43. }
  44. } else {
  45. // get an \ElggAnnotation object and copy its attributes
  46. elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use elgg_get_annotation_from_id()', 1.9);
  47. $annotation = elgg_get_annotation_from_id($row);
  48. $this->attributes = $annotation->attributes;
  49. }
  50. }
  51. }
  52. /**
  53. * Save this instance
  54. *
  55. * @return int an object id
  56. *
  57. * @throws IOException
  58. */
  59. public function save() {
  60. if ($this->id > 0) {
  61. return update_annotation($this->id, $this->name, $this->value, $this->value_type,
  62. $this->owner_guid, $this->access_id);
  63. } else {
  64. $this->id = create_annotation($this->entity_guid, $this->name, $this->value,
  65. $this->value_type, $this->owner_guid, $this->access_id);
  66. if (!$this->id) {
  67. throw new \IOException("Unable to save new " . get_class());
  68. }
  69. return $this->id;
  70. }
  71. }
  72. /**
  73. * Delete the annotation.
  74. *
  75. * @return bool
  76. */
  77. public function delete() {
  78. $result = _elgg_delete_metastring_based_object_by_id($this->id, 'annotation');
  79. if ($result) {
  80. elgg_delete_river(array('annotation_id' => $this->id));
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Disable the annotation.
  86. *
  87. * @return bool
  88. * @since 1.8
  89. */
  90. public function disable() {
  91. return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'annotations');
  92. }
  93. /**
  94. * Enable the annotation.
  95. *
  96. * @return bool
  97. * @since 1.8
  98. */
  99. public function enable() {
  100. return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'annotations');
  101. }
  102. /**
  103. * Determines whether or not the user can edit this annotation
  104. *
  105. * @param int $user_guid The GUID of the user (defaults to currently logged in user)
  106. *
  107. * @return bool
  108. * @see elgg_set_ignore_access()
  109. */
  110. public function canEdit($user_guid = 0) {
  111. if ($user_guid) {
  112. $user = get_user($user_guid);
  113. if (!$user) {
  114. return false;
  115. }
  116. } else {
  117. $user = _elgg_services()->session->getLoggedInUser();
  118. $user_guid = _elgg_services()->session->getLoggedInUserGuid();
  119. }
  120. $result = false;
  121. if ($user) {
  122. // If the owner of annotation is the specified user, they can edit.
  123. if ($this->getOwnerGUID() == $user_guid) {
  124. $result = true;
  125. }
  126. // If the user can edit the entity this is attached to, they can edit.
  127. $entity = $this->getEntity();
  128. if ($result == false && $entity->canEdit($user->getGUID())) {
  129. $result = true;
  130. }
  131. }
  132. // Trigger plugin hook - note that $user may be null
  133. $params = array('entity' => $entity, 'user' => $user, 'annotation' => $this);
  134. return _elgg_services()->hooks->trigger('permissions_check', 'annotation', $params, $result);
  135. }
  136. // SYSTEM LOG INTERFACE
  137. /**
  138. * For a given ID, return the object associated with it.
  139. * This is used by the river functionality primarily.
  140. * This is useful for checking access permissions etc on objects.
  141. *
  142. * @param int $id An annotation ID.
  143. *
  144. * @return \ElggAnnotation
  145. */
  146. public function getObjectFromID($id) {
  147. return elgg_get_annotation_from_id($id);
  148. }
  149. }