diff options
Diffstat (limited to 'engine/lib')
-rw-r--r-- | engine/lib/entities.php | 6 | ||||
-rw-r--r-- | engine/lib/metadata.php | 30 | ||||
-rw-r--r-- | engine/lib/users.php | 12 |
3 files changed, 43 insertions, 5 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 77dc3f073..e2624132d 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -144,9 +144,11 @@ { if (is_array($value)) {
+ remove_metadata($this->getGUID(), $name);
$multiple = true; - foreach ($value as $v) - if (!create_metadata($this->getGUID(), $name, $v, $value_type, $this->getOwner(), $this->getAccessID(), $multiple)) return false; + foreach ($value as $v) { + if (!create_metadata($this->getGUID(), $name, $v, $value_type, $this->getOwner(), $this->getAccessID(), $multiple)) return false;
+ } return true; } diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php index aba7e782d..06c187a70 100644 --- a/engine/lib/metadata.php +++ b/engine/lib/metadata.php @@ -138,6 +138,27 @@ return row_to_elggmetadata(get_data_row("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.id=$id and (m.access_id in {$access} or (m.access_id = 0 and m.owner_guid = {$_SESSION['id']}))")); } +
+ /**
+ * Removes metadata on an entity with a particular name
+ *
+ * @param int $entity_guid The entity GUID
+ * @param string $name The name of the entity
+ * @return true|false Depending on success
+ */
+ function remove_metadata($entity_guid, $name) {
+
+ global $CONFIG;
+ $entity_guid = (int) $entity_guid;
+ $name = sanitise_string(trim($name));
+ if ($existing = get_data("SELECT * from {$CONFIG->dbprefix}metadata WHERE entity_guid = $entity_guid and name_id=" . add_metastring($name))) {
+ foreach($existing as $ex)
+ delete_metadata($ex->id);
+ return true;
+ }
+ return false;
+
+ }
/** * Create a new metadata object, or update an existing one. @@ -169,14 +190,14 @@ $id = false; $existing = get_data_row("SELECT * from {$CONFIG->dbprefix}metadata WHERE entity_guid = $entity_guid and name_id=" . add_metastring($name) . " limit 1"); - if (($existing) && (!$allow_multiple)) + if (($existing) && (!$allow_multiple) && (!empty($value))) { $id = $existing->id; $result = update_metadata($id, $name, $value, $value_type, $owner_guid, $access_id); if (!$result) return false; } - else + else if (!empty($value)) { // Add the metastrings $value = add_metastring($value); @@ -187,6 +208,11 @@ // If ok then add it $id = insert_data("INSERT into {$CONFIG->dbprefix}metadata (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES ($entity_guid, '$name','$value','$value_type', $owner_guid, $time, $access_id)"); + } else if ($existing) {
+
+ $id = $existing->id;
+ delete_metadata($id);
+
} return $id; diff --git a/engine/lib/users.php b/engine/lib/users.php index 5669da06d..efe2f6f83 100644 --- a/engine/lib/users.php +++ b/engine/lib/users.php @@ -230,7 +230,7 @@ * @return true|false
*/
function isFriendOf($user_guid) { return user_is_friend($user_guid, $this->getGUID()); }
-
+
/**
* Retrieves a list of this user's friends
*
@@ -270,6 +270,16 @@ */
public function getCollections($subtype="", $limit = 10, $offset = 0) { return get_user_collections($this->getGUID(), $subtype, $limit, $offset); }
+ /**
+ * If a user's owner is blank, return its own GUID as the owner
+ *
+ * @return int User GUID
+ */
+ function getOwner() {
+ if ($this->owner_guid == 0)
+ return $this->getGUID();
+ }
+
}
/**
|