From 4e08e618308d7dedfd3983d015950c81e7403e29 Mon Sep 17 00:00:00 2001 From: icewing Date: Tue, 1 Apr 2008 17:15:12 +0000 Subject: Marcus Povey * Moved common functionality of metadata and annotations into superclass. git-svn-id: https://code.elgg.org/elgg/trunk@323 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/extender.php | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 engine/lib/extender.php (limited to 'engine/lib/extender.php') diff --git a/engine/lib/extender.php b/engine/lib/extender.php new file mode 100644 index 000000000..f15aaa1ee --- /dev/null +++ b/engine/lib/extender.php @@ -0,0 +1,141 @@ +attributes[$name])) { + + // Sanitise value if necessary + if ($name=='value') + { + switch ($this->attributes['value_type']) + { + case 'integer' : return (int)$this->attributes['value']; + case 'tag' : + case 'text' : + case 'file' : return sanitise_string($this->attributes['value']); + + default : throw new InstallationException("Type {$this->attributes['value_type']} is not supported. This indicates an error in your installation, most likely caused by an incomplete upgrade."); + } + } + + return $this->attributes[$name]; + } + return null; + } + + protected function set($name, $value) { + $this->attributes[$name] = $value; + return true; + } + + /** + * Return the owner of this annotation. + * + * @return mixed + */ + public function getOwner() + { + return get_user($this->owner_guid); + } + + /** + * Save this data to the appropriate database table. + */ + abstract public function save(); + + /** + * Delete this data. + */ + abstract public function delete(); + + public function export() + { + $tmp = new stdClass; + $tmp->attributes = $this->attributes; + $tmp->attributes['owner_uuid'] = guid_to_uuid($this->owner_guid); + return $tmp; + } + + public function import(array $data, $version = 1) + { + if ($version == 1) + { + $entity_uuid = NULL; + + // Get attributes + foreach ($data['elements'][0]['elements'] as $attr) + { + $name = strtolower($attr['name']); + $text = $attr['text']; + + switch ($name) + { + case 'entity_uuid' : $entity_uuid = $text; break; + default : $this->attributes[$name] = $text; + } + + // See if this entity has already been imported, if so then we need to link to it + $entity = get_entity_from_uuid($entity_uuid); + if (!$entity) + throw new ImportException("Sorry $entity_uuid was not found. Could not import annotation."); + + // Set owner ID + $this->attributes['owner_guid'] = $entity->getGUID(); + + return $this; + } + } + else + throw new ImportException("Unsupported version ($version) passed to ElggAnnotation::import()"); + } + } + + /** + * Handler called by trigger_plugin_hook on the "import" event. + */ + function import_extender_plugin_hook($hook, $entity_type, $returnvalue, $params) + { + $name = $params['name']; + $element = $params['element']; + + $tmp = NULL; + + switch ($name) + { + case 'ElggAnnotation' : $tmp = new ElggAnnotation(); break; + case 'ElggMetadata' : $tmp = new ElggMetadata(); break; + } + + if ($tmp) + { + $tmp->import($element); + return $tmp; + } + } + + /** Register the hook */ + register_plugin_hook("import", "all", "import_extender_plugin_hook", 2); + +?> \ No newline at end of file -- cgit v1.2.3