From a08509ec171b12f609a8941c86541f3a413ea913 Mon Sep 17 00:00:00 2001 From: icewing Date: Tue, 1 Apr 2008 13:55:34 +0000 Subject: Marcus Povey * Moved import to ElggEntity git-svn-id: https://code.elgg.org/elgg/trunk@302 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'engine') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index e96a781f9..8784e9af4 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -16,7 +16,7 @@ * This class holds methods for accessing the main entities table. * @author Marcus Povey */ - abstract class ElggEntity implements Exportable + abstract class ElggEntity implements Exportable, Importable { /** * The main attributes of an entity. @@ -310,6 +310,55 @@ $tmp->attributes['owner_uuid'] = guid_to_uuid($this->owner_guid); return $tmp; } + + /** + * Import data from an parsed xml data array. + * + * @param array $data + * @param int $version + */ + public function import(array $data, $version = 1) + { + if ($version == 1) + { + $uuid = ""; + + // Get attributes + foreach ($data['elements'][0]['elements'] as $attr) + { + $name = strtolower($attr['name']); + $text = $attr['text']; + + switch ($name) + { + case 'uuid' : $uuid = $text; break; + default : $this->attributes[$name] = $text; + } + } + + // Check uuid as local domain + if (is_uuid_this_domain($uuid)) + throw new ImportException("$uuid belongs to this domain!"); + + // See if this entity has already been imported, if so we don't need to create a new element + $entity = get_entity_from_uuid($uuid); + if ($entity) + $this->attributes['guid'] = $entity->guid; + + // save + if (!$this->save()); + throw new ImportException("There was a problem saving $uuid"); + + // Tag this GUID with the UUID if this is a new entity + if (!$entity) + add_uuid_to_guid($this->attributes['guid'], $uuid); + + // return result + return $this; + } + else + throw new ImportException("Unsupported version ($version) passed to ElggEntity::import()"); + } } /** -- cgit v1.2.3