1: <?php
2:
3: /**
4: * ElggMetadata
5: * This class describes metadata that can be attached to ElggEntities.
6: *
7: * @package Elgg.Core
8: * @subpackage Metadata
9: *
10: * @property string $value_type
11: * @property int $owner_guid
12: * @property string $enabled
13: */
14: class ElggMetadata extends ElggExtender {
15:
16: /**
17: * (non-PHPdoc)
18: *
19: * @see ElggData::initializeAttributes()
20: *
21: * @return void
22: */
23: protected function initializeAttributes() {
24: parent::initializeAttributes();
25:
26: $this->attributes['type'] = "metadata";
27: }
28:
29: /**
30: * Construct a metadata object
31: *
32: * @param mixed $id ID of metadata or a database row as stdClass object
33: */
34: function __construct($id = null) {
35: $this->initializeAttributes();
36:
37: if (!empty($id)) {
38: // Create from db row
39: if ($id instanceof stdClass) {
40: $metadata = $id;
41:
42: $objarray = (array) $metadata;
43: foreach ($objarray as $key => $value) {
44: $this->attributes[$key] = $value;
45: }
46: } else {
47: // get an ElggMetadata object and copy its attributes
48: $metadata = elgg_get_metadata_from_id($id);
49: $this->attributes = $metadata->attributes;
50: }
51: }
52: }
53:
54: /**
55: * Determines whether or not the user can edit this piece of metadata
56: *
57: * @param int $user_guid The GUID of the user (defaults to currently logged in user)
58: *
59: * @return bool Depending on permissions
60: */
61: function canEdit($user_guid = 0) {
62: if ($entity = get_entity($this->get('entity_guid'))) {
63: return $entity->canEditMetadata($this, $user_guid);
64: }
65: return false;
66: }
67:
68: /**
69: * Save metadata object
70: *
71: * @return int|bool the metadata object id or true if updated
72: *
73: * @throws IOException
74: */
75: function save() {
76: if ($this->id > 0) {
77: return update_metadata($this->id, $this->name, $this->value,
78: $this->value_type, $this->owner_guid, $this->access_id);
79: } else {
80: $this->id = create_metadata($this->entity_guid, $this->name, $this->value,
81: $this->value_type, $this->owner_guid, $this->access_id);
82:
83: if (!$this->id) {
84: throw new IOException(elgg_echo('IOException:UnableToSaveNew', array(get_class())));
85: }
86: return $this->id;
87: }
88: }
89:
90: /**
91: * Delete the metadata
92: *
93: * @return bool
94: */
95: function delete() {
96: $success = elgg_delete_metastring_based_object_by_id($this->id, 'metadata');
97: if ($success) {
98: // we mark unknown here because this deletes only one value
99: // under this name, and there may be others remaining.
100: elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
101: }
102: return $success;
103: }
104:
105: /**
106: * Disable the metadata
107: *
108: * @return bool
109: * @since 1.8
110: */
111: function disable() {
112: $success = elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'metadata');
113: if ($success) {
114: // we mark unknown here because this disables only one value
115: // under this name, and there may be others remaining.
116: elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
117: }
118: return $success;
119: }
120:
121: /**
122: * Enable the metadata
123: *
124: * @return bool
125: * @since 1.8
126: */
127: function enable() {
128: $success = elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'metadata');
129: if ($success) {
130: elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
131: }
132: return $success;
133: }
134:
135: /**
136: * Get a url for this item of metadata.
137: *
138: * @return string
139: */
140: public function getURL() {
141: return get_metadata_url($this->id);
142: }
143:
144: // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
145:
146: /**
147: * For a given ID, return the object associated with it.
148: * This is used by the river functionality primarily.
149: * This is useful for checking access permissions etc on objects.
150: *
151: * @param int $id Metadata ID
152: *
153: * @return ElggMetadata
154: */
155: public function getObjectFromID($id) {
156: return elgg_get_metadata_from_id($id);
157: }
158: }
159: