aboutsummaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorben <ben@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-03-10 16:14:36 +0000
committerben <ben@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-03-10 16:14:36 +0000
commit657ae7c449d85904d7f017c89e08d45d78fe397b (patch)
tree4b2865277d77e7e7b33df8e3ae5f63d2a0cd5114 /engine
parentc21600e85f148f41173c68d3024a4c1329b8ff58 (diff)
downloadelgg-657ae7c449d85904d7f017c89e08d45d78fe397b.tar.gz
elgg-657ae7c449d85904d7f017c89e08d45d78fe397b.tar.bz2
Updated objects with metadata and annotations
git-svn-id: https://code.elgg.org/elgg/trunk@141 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine')
-rw-r--r--engine/lib/objects.php623
1 files changed, 330 insertions, 293 deletions
diff --git a/engine/lib/objects.php b/engine/lib/objects.php
index f939b0ce5..3ab4040c0 100644
--- a/engine/lib/objects.php
+++ b/engine/lib/objects.php
@@ -12,6 +12,188 @@
* @link http://elgg.org/
*/
+
+ /**
+ * This class represents an Elgg object.
+ *
+ */
+ class ElggObject {
+
+ private $attributes = array();
+
+ function __get($name) {
+
+ if (isset($this->attributes[$name])) {
+ return $this->attributes[$name];
+ }
+ return null;
+ }
+
+ function __set($name, $value) {
+ $this->attributes[$name] = $value;
+ return true;
+ }
+
+ function __construct($id = null) {
+
+ global $CONFIG;
+ $this->attributes = array();
+
+ if (!empty($id)) {
+ if ($id instanceof stdClass)
+ $object = $id; // Create from db row
+ else
+ $object = get_object($id);
+
+ if ($object) {
+ $objarray = (array) $object;
+ foreach($objarray as $key => $value) {
+ $this->attributes[$key] = $value;
+ }
+ }
+ } else {
+ $this->site_id = $CONFIG->site_id;
+ }
+ }
+
+
+ /**
+ * Obtains the parent site
+ *
+ * @return ElggSite The parent site
+ */
+ function getSite() {
+ return get_site($this->site_id);
+ }
+
+ /**
+ * Obtains the owning user
+ *
+ * @return ElggUser The owning user
+ */
+ function getOwner() {
+ return get_user($this->owner_id);
+ }
+
+ /**
+ * Inserts or updates the object, depending on whether it's new or not
+ *
+ * @return true|false Depending on success
+ */
+ function save() {
+ if (!empty($this->id)) {
+ return update_object($this->id, $this->title, $this->description, $this->type, $this->owner_id, $this->access_id, $this->site_id);
+ } else if ($id = create_object($this->title,$this->description,$this->type,$this->owner_id,$this->access_id,$this->site_id)) {
+ $this->id = $id;
+ return true;
+ }
+ }
+
+ /**
+ * Deletes this object
+ *
+ * @uses delete_object
+ * @return int|false The number of objects deleted, or false on failure
+ */
+ function delete() {
+ if (!empty($this->id)) {
+ return delete_object($this->id);
+ }
+ return false;
+ }
+
+
+ /**
+ * Set the meta data.
+ *
+ * @param string $name
+ * @param string $value
+ * @param int $access_id
+ * @param string $vartype
+ */
+ function setMetadata($name, $value, $access_id = 0, $vartype = "") { return set_object_metadata($name, $value, $access_id, $this->id, $vartype); }
+
+ /**
+ * Get the metadata for a object.
+ *
+ * @param string $name
+ */
+ function getMetadata($name) { return get_object_metadata($name, $this->id); }
+
+ /**
+ * Clear the metadata for a given object.
+ *
+ * @param string $name
+ */
+ function clearMetadata($name = "") { return remove_object_metadata($this->id, $name); }
+
+ /**
+ * Adds an annotation to a object. By default, the type is detected automatically; however,
+ * it can also be set. Note that by default, annotations are private.
+ *
+ * @param string $name
+ * @param string $value
+ * @param int $access_id
+ * @param int $owner_id
+ * @param string $vartype
+ */
+ function annotate($name, $value, $access_id = 0, $owner_id = 0, $vartype = "") { return add_site_annotation($name, $value, $access_id, $owner_id, $this->id, $vartype); }
+
+ /**
+ * Get the annotations for a object.
+ *
+ * @param string $name
+ * @param int $limit
+ * @param int $offset
+ */
+ function getAnnotations($name, $limit = 50, $offset = 0) { return get_site_annotations($name, $this->id, $limit, $offset); }
+
+ /**
+ * Return the annotations for the object.
+ *
+ * @param string $name The type of annotation.
+ */
+ function countAnnotations($name) { return count_object_annotations($name, $this->id); }
+
+ /**
+ * Get the average of an integer type annotation.
+ *
+ * @param string $name
+ */
+ function getAnnotationsAvg($name) { return get_object_annotations_avg($name, $this->id); }
+
+ /**
+ * Get the sum of integer type annotations of a given type.
+ *
+ * @param string $name
+ */
+ function getAnnotationsSum($name) { return get_object_annotations_sum($name, $this->id); }
+
+ /**
+ * Get the minimum of integer type annotations of given type.
+ *
+ * @param string $name
+ */
+ function getAnnotationsMin($name) { return get_object_annotations_min($name, $this->id); }
+
+ /**
+ * Get the maximum of integer type annotations of a given type.
+ *
+ * @param string $name
+ */
+ function getAnnotationsMax($name) { return get_object_annotations_max($name, $this->id); }
+
+ /**
+ * Remove all annotations or all annotations of a given object.
+ *
+ * @param string $name
+ */
+ function removeAnnotations($name = "") { return remove_object_annotations($this->id, $name); }
+
+
+ }
+
+
/**
* Converts a standard database row result to an ElggObject
*
@@ -30,14 +212,12 @@
*
* @param int $user_id The ID of the publishing user; set to 0 for all users
* @param string $type The type of the object; set to blank for all types
- * @param string $metadata_type The type of metadata that we're searching on (blank for none)
- * @param string $metadata_value The value of metadata that we're searching on (blank for none)
* @param int $limit The number of objects (default 10)
* @param int $offset The offset of the return, for pagination
* @param int $site_id The site the objects belong to (leave blank for default site)
* @return unknown
*/
- function get_objects($user_id = 0, $type = "", $metadata_type = "", $metadata_value = "", $limit = 10, $offset = 0, $site_id = 0) {
+ function get_objects($user_id = 0, $type = "", $limit = 10, $offset = 0, $site_id = 0) {
global $CONFIG;
@@ -51,21 +231,10 @@
$query = "select o.*, ot.name as typename from {$CONFIG->dbprefix}objects o ";
if (!empty($type)) $query .= " left join {$CONFIG->dbprefix}object_types ot on ot.id = o.type_id ";
- if (!empty($metadata_type) && !empty($metadata_value)) {
- $metadata_type = sanitise_string($metadata_type);
- $metadata_value = sanitise_string($metadata_value);
- $query .= " left join {$CONFIG->dbprefix}object_metadata om on om.object_id = o.id ";
- $query .= " left join {$CONFIG->dbprefix}metadata_value mv on mv.id = om.value_id ";
- $query .= " left join {$CONFIG->dbprefix}metadata_type mt on mt.id = om.metadata_type_id ";
- }
$query .= " where o.site_id = {$site_id} ";
$query .= " and (o.access_id in {$access} or (o.access_id = 0 and o.owner_id = {$_SESSION['id']}))";
if (!empty($type)) $query .= " and ot.name = '{$type}'";
if ($user_id > 0) $query .= " and o.owner_id = {$user_id} ";
- if (!empty($metadata_type) && !empty($metadata_value)) {
- $query .= " and mv.value = '{$metadata_value}' and mt.name = '{$metadata_type}' ";
- $query .= " and (om.access_id in {$access} or (om.access_id = 0 and o.owner_id = {$_SESSION['id']}))";
- }
$query .= " order by o.time_created desc ";
if ($limit > 0 || $offset > 0) $query .= " limit {$offset}, {$limit}";
@@ -104,8 +273,16 @@
$object_id = (int) $object_id;
$access = get_access_list();
+ if (!($object = get_object($object_id)))
+ return false;
+
+ if (!(trigger_event("delete","object",$ibject)))
+ return false;
+
+ $object->removeAnnotations();
+ $object->clearMetadata();
+
if (delete_data("delete from {$CONFIG->dbprefix}objects where o.id = {$object_id} and o.owner_id = {$_SESSION['id']}")) {
- remove_object_metadata($object_id);
return true;
}
@@ -216,308 +393,168 @@
}
+
/**
- * Gets the ID of an object metadata type in the database, setting it if necessary
+ * Set the site metadata.
*
- * @param string $type The name of the metadata type
- * @return int|false The database ID of the metadata type, or false if the given type was invalid
+ * @param string $name
+ * @param string $value
+ * @param int $access_id
+ * @param int $object_id
+ * @param string $vartype
*/
- function get_metadata_type_id($type) {
-
- global $CONFIG;
- $type = strtolower(trim(sanitise_string($type)));
- if (!empty($type) && $dbtype = get_data_row("select id from {$CONFIG->dbprefix}metadata_type where name = '{$type}'")) {
- return $dbtype->id;
- } else if (!empty($type)) {
- return insert_data("insert into {$CONFIG->dbprefix}metadata_type set name = '{$type}'");
- }
- return false;
-
- }
-
+ function set_object_metadata($name, $value, $access_id, $object_id, $vartype = "")
+ {
+ $name = sanitise_string($name);
+ $value = sanitise_string($value);
+ $access_id = (int)$access_id;
+ $object_id = (int)$object_id;
+ $vartype = sanitise_string($vartype);
+ $owner_id = $_SESSION['id'];
+
+ $id = create_metadata($object_id, 'object', $name, $value, $vartype, $owner_id, $access_id);
+ return $id;
+ }
+
/**
- * Gets the ID of an object metadata value in the database, setting it if necessary
+ * Get object metadata.
*
- * @param string $type The metadata value
- * @return int|false The database ID of the metadata value, or false if the given value was invalid
+ * @param string $name
+ * @param int $object_id
*/
- function get_metadata_value_id($value) {
-
- global $CONFIG;
- $type = strtolower(trim(sanitise_string($value)));
- if (!empty($value) && $dbtype = get_data_row("select id from {$CONFIG->dbprefix}metadata_value where value = '{$value}'")) {
- return $dbtype->id;
- } else if (!empty($value)) {
- return insert_data("insert into {$CONFIG->dbprefix}metadata_value set value = '{$value}'");
- }
- return false;
-
- }
+ function get_object_metadata($name, $object_id)
+ {
+ $name = sanitise_string($name);
+ $object_id = (int)$object_id;
+ return get_metadatas($object_id, 'object');
+ }
+
/**
- * Sets a piece of metadata for a particular object.
+ * Remove object metadata
*
- * @param string $metadata_name The type of metadata
- * @param string $metadata_value Its value
- * @param int $access_id The access level of the metadata
- * @param int $object_id The ID of the object
- * @return true|false depending on success
+ * @param int $object_id
+ * @param string $name
*/
- function set_object_metadata($metadata_name, $metadata_value, $access_id, $object_id, $site_id = 0) {
- global $CONFIG;
- $object_id = (int) $object_id;
- if ($object = get_object($object_id)) {
- if ($object->owner_id == $_SESSION['id']) {
-
- $access_id = (int) $access_id;
- if ($site_id == 0) $site_id = $CONFIG->site_id;
- $site_id = (int) $site_id;
-
- if ($type_id = get_object_metadata_type_id($metadata_name)
- && $value_id = get_object_metadata_value_id($metadata_value)
- && in_array($access_id,get_access_array())) {
- delete_data("delete from {$CONFIG->dbprefix}object_metadata where metadata_type_id = {$type_id} and object_id = {$object_id}");
- return insert_data("insert into {$CONFIG->dbprefix}object_metadata set object_id = {$object_id}, access_id = {$access_id}, metadata_type_id = {$type_id}, value_id = {$value_id}, site_id = {$site_id}");
- } else {
- return false;
- }
-
- }
- } else {
- return false;
- }
+ function remove_object_metadata($object_id, $name)
+ {
+ $result = get_metadatas($object_id, 'object', $name);
+
+ if ($result)
+ {
+ foreach ($result as $r)
+ delete_metadata($r->id);
}
-
+
+ return false;
+ }
+
/**
- * Returns the value of a particular piece of metadata on an object
- *
- * @param string $metadata_name The name of the metadata
- * @param int $object_id The object ID
- * @param int $site_id The site ID, optionally
- * @return mixed The value of the metadata
+ * Adds an annotation to a object. By default, the type is detected automatically;
+ * however, it can also be set. Note that by default, annotations are private.
+ *
+ * @param string $name
+ * @param string $value
+ * @param int $access_id
+ * @param int $owner_id
+ * @param int $object_id
+ * @param string $vartype
*/
- function get_object_metadata($metadata_name, $object_id, $site_id = 0) {
-
- if ($type_id = get_metadata_type_id($metadata_name)) {
-
- $accesslist = get_access_list();
- $object_id = (int) $objet_id;
- if ($site_id == 0) $site_id = $CONFIG->site_id;
- $site_id = (int) $site_id;
-
- if ($result = get_data_row("select mv.value from object_metadata om left join metadata_value mv on mv.id = om.value_id where om.object_id = {$object_id} and om.site_id = {$site_id} and om.metadata_type_id = {$type_id}")) {
- return $result->value;
- }
- return false;
-
- }
-
- }
+ function add_object_annotation($name, $value, $access_id, $owner_id, $object_id, $vartype)
+ {
+ $name = sanitise_string($name);
+ $value = sanitise_string($value);
+ $access_id = (int)$access_id;
+ $owner_id = (int)$owner_id; if ($owner_id==0) $owner_id = $_SESSION['id'];
+ $object_id = (int)$object_id;
+ $vartype = sanitise_string($vartype);
+
+ $id = create_annotation($object_id, 'object', $name, $value, $vartype, $owner_id, $access_id);
+ return $id;
+ }
+
/**
- * Removes a piece of (or all) metadata for a particular object.
+ * Get the annotations for a object.
*
- * @param int $object_id The ID of the object
- * @param string $metadata_name The type of metadata; blank for all metadata
- * @return true|false depending on success
+ * @param string $name
+ * @param int $object_id
+ * @param int $limit
+ * @param int $offset
*/
- function remove_object_metadata($object_id, $metadata_name = "") {
- global $CONFIG;
- $object_id = (int) $object_id;
- if ($object = get_object($object_id)) {
- if ($object->owner_id == $_SESSION['id']) {
-
- if ($type_id = get_object_metadata_type_id($metadata_name)) {
- return delete_data("delete from {$CONFIG->dbprefix}object_metadata where metadata_type_id = {$type_id} and object_id = {$object_id}");
- } else {
- return false;
- }
-
- }
- } else {
- return false;
- }
- return true;
- }
+ function get_object_annotations($name, $object_id, $limit, $offset)
+ {
+ $name = sanitise_string($name);
+ $object_id = (int)$object_id;
+ $limit = (int)$limit;
+ $offset = (int)$offset;
+ $owner_id = (int)$owner_id; if ($owner_id==0) $owner_id = $_SESSION['id']; // Consider adding the option to change in param?
+ return get_annotations($object_id, 'site', "","", $owner_id, "created desc", $limit, $offset);
+ }
+
/**
- * This class represents an Elgg object.
+ * Count the annotations for a object of a given type.
*
+ * @param string $name
+ * @param int $object_id
*/
- class ElggObject {
-
- private $attributes = array();
-
- function __get($name) {
-
- if (isset($this->attributes[$name])) {
- return $this->attributes[$name];
- }
- return null;
- }
-
- function __set($name, $value) {
- $this->attributes[$name] = $value;
- return true;
- }
-
- function __construct($id = null) {
-
- $this->attributes = array();
-
- if (!empty($id)) {
- if ($id instanceof stdClass)
- $object = $id; // Create from db row
- else
- $object = get_object($id);
-
- if ($object) {
- $objarray = (array) $object;
- foreach($objarray as $key => $value) {
- $this->attributes[$key] = $value;
- }
- }
- }
- }
-
-
- /**
- * Obtains the parent site
- *
- * @return ElggSite The parent site
- */
- function getSite() {
- // TODO: gets the parent site
- }
-
+ function count_object_annotations($name, $object_id) { return count_annotations($object_id, 'object', $name); }
+
+ /**
+ * Get the average of an integer type annotation.
+ *
+ * @param string $name
+ * @param int $object_id
+ */
+ function get_object_annotations_avg($name, $object_id) { return get_annotations_avg($object_id, 'object', $name); }
+
+ /**
+ * Get the sum of integer type annotations of a given type.
+ *
+ * @param string $name
+ * @param int $object_id
+ */
+ function get_object_annotations_sum($name, $object_id) { return get_annotations_sum($object_id, 'object', $name); }
+
+ /**
+ * Get the min of integer type annotations of a given type.
+ *
+ * @param string $name
+ * @param int $object_id
+ */
+ function get_object_annotations_min($name, $object_id) { return get_annotations_min($object_id, 'object', $name); }
+
+ /**
+ * Get the max of integer type annotations of a given type.
+ *
+ * @param string $name
+ * @param int $object_id
+ */
+ function get_object_annotations_max($name, $object_id) { return get_annotations_max($object_id, 'object', $name); }
+
+ /**
+ * Remove all object annotations, or object annotations of a given type.
+ *
+ * @param int $object_id
+ * @param string $name
+ */
+ function remove_object_annotations($object_id, $name)
+ {
+ $annotations = get_annotations($object_id, 'site', $name);
-
- /**
- * Returns the value of a particular piece of metadata
- *
- * @param string $name The name of the metadata
- * @return mixed|false The metadata value; false on failure
- */
- function getMetadata($name) {
- if (!empty($this->id)) {
- return get_object_metadata($name, $this->id, $this->site_id);
- }
- return false;
- }
-
- /**
- * Adds metadata for this object
- *
- * @uses set_object_metadata
- * @param string $name The name of the metadata type
- * @param string $value The value for the metadata to set
- * @param int $access_id The access level for this piece of metadata (default: private)
- * @return true|false Depending on success
- */
- function setMetadata($name, $value, $access_id = 0) {
- if (!empty($this->id)) {
- return set_object_metadata($name, $value, $access_id, $this->id, $this->site_id);
- }
- return false;
- }
-
- /**
- * Clears metadata for this object, either for a particular type or across the board
- *
- * @uses remove_object_metadata
- * @param string $name Optionally, the name of the metadata to remove
- * @return true|false Depending on success
- */
- function clearMetadata($name = "") {
- if (!empty($this->id)) {
- return remove_object_metadata($this->id, $name);
- }
- return false;
- }
-
- /**
- * Adds an annotation to an object
- *
- * @param string $name Name of the annotation type
- * @param string|int $value The annotation value
- * @param int $access_id The access level for the annotation
- * @param int $owner_id The annotation owner
- * @param string $vartype Optionally, the variable type of the annotation (eg "int")
- */
- function annotate($name, $value, $access_id = 0, $owner_id = 0, $vartype = "") {
- // TODO: add annotation
- }
-
- /**
- * Returns the object's annotations of a particular type (eg "comment")
- *
- * @param string $name The type of annotation
- * @param int $limit Number of annotations to get
- * @param int $offset Any offset
- */
- function getAnnotations($name, $limit = 50, $offset = 0) {
- // TODO: get annotations
- }
-
- /**
- * Gets the average of the integer annotations on this object
- *
- * @param string $name Optionally, the type of annotation
- */
- function getAnnotationsAvg($name) {
- }
-
- /**
- * Gets the sum of the integer annotations on this object
- *
- * @param string $name Optionally, the type of annotation
- */
- function getAnnotationsSum($name) {
+ if($annotations)
+ {
+ foreach ($annotations as $a)
+ {
+ delete_annotation($a->id);
}
- /**
- * Gets the minimum value of the integer annotations on this object
- *
- * @param string $name Optionally, the type of annotation
- */
- function getAnnotationsMin($name) {
- }
-
- /**
- * Gets the maximum value of the integer annotations on this object
- *
- * @param string $name Optionally, the type of annotation
- */
- function getAnnotationsMax($name) {
- }
-
- /**
- * Inserts or updates the object, depending on whether it's new or not
- *
- * @return true|false Depending on success
- */
- function save() {
- if (!empty($this->id)) {
- return update_object($this->id, $this->title, $this->description, $this->type, $this->owner_id, $this->access_id, $this->site_id);
- } else if ($id = create_object($this->title,$this->description,$this->type,$this->owner_id,$this->access_id,$this->site_id)) {
- $this->id = $id;
- return true;
- }
- }
-
- /**
- * Deletes this object
- *
- * @uses delete_object
- * @return int|false The number of objects deleted, or false on failure
- */
- function delete() {
- if (!empty($this->id)) {
- return delete_object($this->id);
- }
- return false;
- }
-
+ return true;
}
+ return false;
+ }
+
+
?> \ No newline at end of file