From 6441a960edc875a0c33fa8bbd735fd1faebaa552 Mon Sep 17 00:00:00 2001 From: icewing Date: Fri, 28 Mar 2008 19:18:55 +0000 Subject: Marcus Povey * Metadata and Annotations now exported. git-svn-id: https://code.elgg.org/elgg/trunk@291 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/metadata.php | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'engine/lib/metadata.php') diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php index 4744f6f6f..2c738ebda 100644 --- a/engine/lib/metadata.php +++ b/engine/lib/metadata.php @@ -16,7 +16,7 @@ * This class describes metadata that can be attached to ElggEntities. * @author Marcus Povey */ - class ElggMetadata + class ElggMetadata implements Exportable { /** * This contains the site's main properties (id, etc) @@ -106,6 +106,13 @@ { return delete_metadata($this->id); } + + public function export() + { + $tmp = new stdClass; + $tmp->attributes = $this->attributes; + return $tmp; + } } @@ -299,6 +306,21 @@ return get_data("SELECT m.*, n.string as name, v.string as value from {$CONFIG->dbprefix}metadata m JOIN {$CONFIG->dbprefix}metastrings v on m.value_id = v.id JOIN {$CONFIG->dbprefix}metastrings n on m.name_id = n.id where m.entity_guid=$entity_guid and m.name_id='$meta_name' and (m.access_id in {$access} or (m.access_id = 0 and m.owner_guid = {$_SESSION['id']}))", "row_to_elggmetadata"); } + + /** + * Return all the metadata for a given GUID. + * + * @param int $entity_guid + */ + function get_metadata_for_entity($entity_guid) + { + global $CONFIG; + + $entity_guid = (int)$entity_guid; + $access = get_access_list(); + + return get_data("SELECT m.*, n.string as name, v.string as value from {$CONFIG->dbprefix}metadata m JOIN {$CONFIG->dbprefix}metastrings v on m.value_id = v.id JOIN {$CONFIG->dbprefix}metastrings n on m.name_id = n.id where m.entity_guid=$entity_guid and (m.access_id in {$access} or (m.access_id = 0 and m.owner_guid = {$_SESSION['id']}))", "row_to_elggmetadata"); + } /** * Return a list of entities based on the given search criteria. @@ -360,4 +382,33 @@ return delete_data("DELETE from {$CONFIG->dbprefix}metadata where entity_guid=$entity_guid and access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']})"); } + /** + * Handler called by trigger_plugin_hook on the "export" event. + */ + function export_metadata_plugin_hook($hook, $entity_type, $returnvalue, $params) + { + // Sanity check values + if ((!is_array($params)) && (!isset($params['guid']))) + throw new InvalidParameterException("GUID has not been specified during export, this should never happen."); + + if (!is_array($returnvalue)) + throw new InvalidParameterException("Entity serialisation function passed a non-array returnvalue parameter"); + + $guid = (int)$params['guid']; + + // Get the metadata for the entity + $metadata = get_metadata_for_entity($guid); + + if ($metadata) + { + foreach ($metadata as $m) + $returnvalue[] = $m; + } + + return $returnvalue; + } + + /** Register the hook, ensuring entities are serialised first */ + register_plugin_hook("export", "all", "export_metadata_plugin_hook", 2); + ?> \ No newline at end of file -- cgit v1.2.3