ElggMetadata.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * \ElggMetadata
  4. *
  5. * This class describes metadata that can be attached to an \ElggEntity. It is
  6. * rare that a plugin developer needs to use this API for metadata. Almost all
  7. * interaction with metadata occurs through the methods of \ElggEntity. See its
  8. * __set(), __get(), and setMetadata() methods.
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Metadata
  12. */
  13. class ElggMetadata extends \ElggExtender {
  14. /**
  15. * (non-PHPdoc)
  16. *
  17. * @see \ElggData::initializeAttributes()
  18. *
  19. * @return void
  20. */
  21. protected function initializeAttributes() {
  22. parent::initializeAttributes();
  23. $this->attributes['type'] = "metadata";
  24. }
  25. /**
  26. * Construct a metadata object
  27. *
  28. * Plugin developers will probably never need to use this API. See \ElggEntity
  29. * for its API for setting and getting metadata.
  30. *
  31. * @param \stdClass $row Database row as \stdClass object
  32. */
  33. public function __construct($row = null) {
  34. $this->initializeAttributes();
  35. if (!empty($row)) {
  36. // Create from db row
  37. if ($row instanceof \stdClass) {
  38. $metadata = $row;
  39. $objarray = (array) $metadata;
  40. foreach ($objarray as $key => $value) {
  41. $this->attributes[$key] = $value;
  42. }
  43. } else {
  44. // get an \ElggMetadata object and copy its attributes
  45. elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use elgg_get_metadata_from_id()', 1.9);
  46. $metadata = elgg_get_metadata_from_id($row);
  47. $this->attributes = $metadata->attributes;
  48. }
  49. }
  50. }
  51. /**
  52. * Determines whether or not the user can edit this piece of metadata
  53. *
  54. * @param int $user_guid The GUID of the user (defaults to currently logged in user)
  55. *
  56. * @return bool
  57. * @see elgg_set_ignore_access()
  58. */
  59. public function canEdit($user_guid = 0) {
  60. if ($entity = get_entity($this->entity_guid)) {
  61. return $entity->canEditMetadata($this, $user_guid);
  62. }
  63. return false;
  64. }
  65. /**
  66. * Save metadata object
  67. *
  68. * @return int|bool the metadata object id or true if updated
  69. *
  70. * @throws IOException
  71. */
  72. public function save() {
  73. if ($this->id > 0) {
  74. return update_metadata($this->id, $this->name, $this->value,
  75. $this->value_type, $this->owner_guid, $this->access_id);
  76. } else {
  77. $this->id = create_metadata($this->entity_guid, $this->name, $this->value,
  78. $this->value_type, $this->owner_guid, $this->access_id);
  79. if (!$this->id) {
  80. throw new \IOException("Unable to save new " . get_class());
  81. }
  82. return $this->id;
  83. }
  84. }
  85. /**
  86. * Delete the metadata
  87. *
  88. * @return bool
  89. */
  90. public function delete() {
  91. $success = _elgg_delete_metastring_based_object_by_id($this->id, 'metadata');
  92. if ($success) {
  93. _elgg_services()->metadataCache->clear($this->entity_guid);
  94. }
  95. return $success;
  96. }
  97. /**
  98. * Disable the metadata
  99. *
  100. * @return bool
  101. * @since 1.8
  102. */
  103. public function disable() {
  104. $success = _elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'metadata');
  105. if ($success) {
  106. _elgg_services()->metadataCache->clear($this->entity_guid);
  107. }
  108. return $success;
  109. }
  110. /**
  111. * Enable the metadata
  112. *
  113. * @return bool
  114. * @since 1.8
  115. */
  116. public function enable() {
  117. $success = _elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'metadata');
  118. if ($success) {
  119. _elgg_services()->metadataCache->clear($this->entity_guid);
  120. }
  121. return $success;
  122. }
  123. // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
  124. /**
  125. * For a given ID, return the object associated with it.
  126. * This is used by the river functionality primarily.
  127. * This is useful for checking access permissions etc on objects.
  128. *
  129. * @param int $id Metadata ID
  130. *
  131. * @return \ElggMetadata
  132. */
  133. public function getObjectFromID($id) {
  134. return elgg_get_metadata_from_id($id);
  135. }
  136. }