diff options
author | icewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-04-01 17:15:12 +0000 |
---|---|---|
committer | icewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-04-01 17:15:12 +0000 |
commit | 4e08e618308d7dedfd3983d015950c81e7403e29 (patch) | |
tree | f4ae2137a46b23be2c712ee582c251ec3bd0f1f5 /engine | |
parent | 205dcbf14c473fe64f89e5696cfbafea8fa5ab15 (diff) | |
download | elgg-4e08e618308d7dedfd3983d015950c81e7403e29.tar.gz elgg-4e08e618308d7dedfd3983d015950c81e7403e29.tar.bz2 |
Marcus Povey <marcus@dushka.co.uk>
* Moved common functionality of metadata and annotations into superclass.
git-svn-id: https://code.elgg.org/elgg/trunk@323 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine')
-rw-r--r-- | engine/lib/annotations.php | 88 | ||||
-rw-r--r-- | engine/lib/extender.php | 141 | ||||
-rw-r--r-- | engine/lib/metadata.php | 70 |
3 files changed, 173 insertions, 126 deletions
diff --git a/engine/lib/annotations.php b/engine/lib/annotations.php index b0e01ad31..26664757a 100644 --- a/engine/lib/annotations.php +++ b/engine/lib/annotations.php @@ -11,18 +11,15 @@ * @link http://elgg.org/ */ + require_once('extender.php'); // include the superclass + /** * @class ElggAnnotation * An annotation is similar to metadata each entity can contain more than one of each annotation. * @author Marcus Povey <marcus@dushka.co.uk> */ - class ElggAnnotation implements Exportable + class ElggAnnotation extends ElggExtender { - /** - * This contains the site's main properties (id, etc) - * @var array - */ - private $attributes; /** * Construct a new site object, optionally from a given id value or db row. @@ -49,40 +46,11 @@ } function __get($name) { - if (isset($this->attributes[$name])) { - - // Sanitise outputs if required - 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; + return $this->get($name); } function __set($name, $value) { - $this->attributes[$name] = $value; - return true; - } - - /** - * Return the owner of this annotation. - * - * @return mixed - */ - function getOwner() - { - return get_user($this->owner_guid); + return $this->set($name, $value); } function save() @@ -104,14 +72,7 @@ { return delete_annotation($this->id); } - - public function export() - { - $tmp = new stdClass; - $tmp->attributes = $this->attributes; - $tmp->attributes['owner_uuid'] = guid_to_uuid($this->owner_guid); - return $tmp; - } + } /** @@ -452,42 +413,29 @@ /** * Handler called by trigger_plugin_hook on the "export" event. */ - function export_annotations_plugin_hook($hook, $entity_type, $returnvalue, $params) + function export_annotation_plugin_hook($hook, $entity_type, $returnvalue, $params) { - global $CONFIG; - // 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']; - $access = get_access_list(); - $where = array(); - - if ($guid != 0) - $where[] = "a.entity_guid=$guid"; - - $query = "SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id where "; - foreach ($where as $w) - $query .= " $w and "; - $query .= " (a.access_id in {$access} or (a.access_id = 0 and a.owner_guid = {$_SESSION['id']}))"; // Add access controls - $query .= " order by a.time_created"; // Add order and limit - error_log($query); - $annotations = get_data($query, "row_to_elggannotation"); + $guid = (int)$params['guid']; + $name = $params['name']; - if ($annotations) + $result = get_annotations($guid); + + if ($result) { - foreach ($annotations as $a) - $returnvalue[] = $a; - } - + foreach ($result as $r) + $returnvalue[] = $r; + } + return $returnvalue; } - /** Register the hook, ensuring entities are serialised first */ - register_plugin_hook("export", "all", "export_annotations_plugin_hook", 2); + /** Register the hook */ + register_plugin_hook("export", "all", "export_annotation_plugin_hook", 2); ?>
\ No newline at end of file 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 @@ +<?php + /** + * Elgg Entity Extender. + * This file contains ways of extending an Elgg entity in custom ways. + * + * @package Elgg + * @subpackage Core + * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2 + * @author Marcus Povey + * @copyright Curverider Ltd 2008 + * @link http://elgg.org/ + */ + + /** + * @class ElggExtender + * @author Marcus Povey + */ + abstract class ElggExtender implements Exportable, Importable + { + /** + * This contains the site's main properties (id, etc) + * @var array + */ + protected $attributes; + + protected function get($name) { + if (isset($this->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 diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php index 82ca71bdd..573ea8f4f 100644 --- a/engine/lib/metadata.php +++ b/engine/lib/metadata.php @@ -16,14 +16,9 @@ * This class describes metadata that can be attached to ElggEntities. * @author Marcus Povey <marcus@dushka.co.uk> */ - class ElggMetadata implements Exportable + class ElggMetadata extends ElggExtender { - /** - * This contains the site's main properties (id, etc) - * @var array - */ - private $attributes; - + /** * Construct a new site object, optionally from a given id value or row. * @@ -50,40 +45,11 @@ } function __get($name) { - if (isset($this->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; + return $this->get($name); } function __set($name, $value) { - $this->attributes[$name] = $value; - return true; - } - - /** - * Return the owner of this metadata. - * - * @return mixed - */ - function getOwner() - { - return get_user($this->owner_guid); + return $this->set($name, $value); } function save() @@ -107,13 +73,6 @@ return delete_metadata($this->id); } - public function export() - { - $tmp = new stdClass; - $tmp->attributes = $this->attributes; - $tmp->attributes['owner_uuid'] = guid_to_uuid($this->owner_guid); - return $tmp; - } } @@ -394,22 +353,21 @@ 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); + $guid = (int)$params['guid']; + $name = $params['name']; - if ($metadata) + $result = get_metadata_for_entity($guid); + + if ($result) { - foreach ($metadata as $m) - $returnvalue[] = $m; - } - + foreach ($result as $r) + $returnvalue[] = $r; + } + return $returnvalue; } - /** Register the hook, ensuring entities are serialised first */ + /** Register the hook */ register_plugin_hook("export", "all", "export_metadata_plugin_hook", 2); - ?>
\ No newline at end of file |