aboutsummaryrefslogtreecommitdiff
path: root/engine/classes/ElggMetadata.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/classes/ElggMetadata.php')
-rw-r--r--engine/classes/ElggMetadata.php136
1 files changed, 90 insertions, 46 deletions
diff --git a/engine/classes/ElggMetadata.php b/engine/classes/ElggMetadata.php
index 72fc7c379..3a8e2d817 100644
--- a/engine/classes/ElggMetadata.php
+++ b/engine/classes/ElggMetadata.php
@@ -4,92 +4,132 @@
* ElggMetadata
* This class describes metadata that can be attached to ElggEntities.
*
- * @author Curverider Ltd <info@elgg.com>
- * @package Elgg
- * @subpackage Core
+ * @package Elgg.Core
+ * @subpackage Metadata
+ *
+ * @property string $value_type
+ * @property int $owner_guid
+ * @property string $enabled
*/
class ElggMetadata extends ElggExtender {
+
/**
- * Construct a new site object, optionally from a given id value or row.
+ * (non-PHPdoc)
+ *
+ * @see ElggData::initializeAttributes()
*
- * @param mixed $id
+ * @return void
+ */
+ protected function initializeAttributes() {
+ parent::initializeAttributes();
+
+ $this->attributes['type'] = "metadata";
+ }
+
+ /**
+ * Construct a metadata object
+ *
+ * @param mixed $id ID of metadata or a database row as stdClass object
*/
function __construct($id = null) {
- $this->attributes = array();
+ $this->initializeAttributes();
if (!empty($id)) {
// Create from db row
if ($id instanceof stdClass) {
$metadata = $id;
- } else {
- $metadata = get_metadata($id);
- }
-
- if ($metadata) {
+
$objarray = (array) $metadata;
- foreach($objarray as $key => $value) {
+ foreach ($objarray as $key => $value) {
$this->attributes[$key] = $value;
}
- $this->attributes['type'] = "metadata";
+ } else {
+ // get an ElggMetadata object and copy its attributes
+ $metadata = elgg_get_metadata_from_id($id);
+ $this->attributes = $metadata->attributes;
}
}
}
/**
- * Class member get overloading
- *
- * @param string $name
- * @return mixed
- */
- function __get($name) {
- return $this->get($name);
- }
-
- /**
- * Class member set overloading
- *
- * @param string $name
- * @param mixed $value
- * @return mixed
- */
- function __set($name, $value) {
- return $this->set($name, $value);
- }
-
- /**
* Determines whether or not the user can edit this piece of metadata
*
- * @return true|false Depending on permissions
+ * @param int $user_guid The GUID of the user (defaults to currently logged in user)
+ *
+ * @return bool Depending on permissions
*/
- function canEdit() {
+ function canEdit($user_guid = 0) {
if ($entity = get_entity($this->get('entity_guid'))) {
- return $entity->canEditMetadata($this);
+ return $entity->canEditMetadata($this, $user_guid);
}
return false;
}
/**
- * Save matadata object
+ * Save metadata object
*
- * @return int the metadata object id
+ * @return int|bool the metadata object id or true if updated
+ *
+ * @throws IOException
*/
function save() {
if ($this->id > 0) {
- return update_metadata($this->id, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
+ return update_metadata($this->id, $this->name, $this->value,
+ $this->value_type, $this->owner_guid, $this->access_id);
} else {
- $this->id = create_metadata($this->entity_guid, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
+ $this->id = create_metadata($this->entity_guid, $this->name, $this->value,
+ $this->value_type, $this->owner_guid, $this->access_id);
+
if (!$this->id) {
- throw new IOException(sprintf(elgg_echo('IOException:UnableToSaveNew'), get_class()));
+ throw new IOException(elgg_echo('IOException:UnableToSaveNew', array(get_class())));
}
return $this->id;
}
}
/**
- * Delete a given metadata.
+ * Delete the metadata
+ *
+ * @return bool
*/
function delete() {
- return delete_metadata($this->id);
+ $success = elgg_delete_metastring_based_object_by_id($this->id, 'metadata');
+ if ($success) {
+ // we mark unknown here because this deletes only one value
+ // under this name, and there may be others remaining.
+ elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
+ }
+ return $success;
+ }
+
+ /**
+ * Disable the metadata
+ *
+ * @return bool
+ * @since 1.8
+ */
+ function disable() {
+ $success = elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'metadata');
+ if ($success) {
+ // we mark unknown here because this disables only one value
+ // under this name, and there may be others remaining.
+ elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
+ }
+ return $success;
+ }
+
+ /**
+ * Enable the metadata
+ *
+ * @return bool
+ * @since 1.8
+ */
+ function enable() {
+ $success = elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'metadata');
+ if ($success) {
+ elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
+ }
+ return $success;
}
/**
@@ -107,8 +147,12 @@ class ElggMetadata extends ElggExtender {
* For a given ID, return the object associated with it.
* This is used by the river functionality primarily.
* This is useful for checking access permissions etc on objects.
+ *
+ * @param int $id Metadata ID
+ *
+ * @return ElggMetadata
*/
public function getObjectFromID($id) {
- return get_metadata($id);
+ return elgg_get_metadata_from_id($id);
}
-} \ No newline at end of file
+}