aboutsummaryrefslogtreecommitdiff
path: root/engine/lib
diff options
context:
space:
mode:
authoricewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-03-06 13:08:30 +0000
committericewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-03-06 13:08:30 +0000
commitc508adaa1531e1d99b7708605f2fa158ea39b2bc (patch)
tree47afc9110527e3d4a10368ad3473fb9696a2a0f9 /engine/lib
parent9e1593a5bf6604a4e22023f3c465f8f571b7feb2 (diff)
downloadelgg-c508adaa1531e1d99b7708605f2fa158ea39b2bc.tar.gz
elgg-c508adaa1531e1d99b7708605f2fa158ea39b2bc.tar.bz2
Marcus Povey <marcus@dushka.co.uk>
* Meta now updates if item already exists (if the user has permission) git-svn-id: https://code.elgg.org/elgg/trunk@95 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/lib')
-rw-r--r--engine/lib/metadata.php43
1 files changed, 40 insertions, 3 deletions
diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php
index b36e9ac44..3a326ac56 100644
--- a/engine/lib/metadata.php
+++ b/engine/lib/metadata.php
@@ -103,6 +103,28 @@
}
/**
+ * Detect the value_type for a given value.
+ * Currently this is very crude.
+ *
+ * TODO: Make better!
+ *
+ * @param mixed $value
+ * @param string $value_type If specified, overrides the detection.
+ * @return string
+ */
+ function detect_metadata_valuetype($value, $value_type = "")
+ {
+ if ($value_type!="")
+ return $value_type;
+
+ // This is crude
+ if (is_int($value)) return 'integer';
+
+ return 'tag';
+ }
+
+
+ /**
* Create a new metadata object, or update an existing one.
*
* @param int $object_id
@@ -121,14 +143,29 @@
$object_type = sanitise_string(trim($object_type));
$name = sanitise_string(trim($name));
$value = sanitise_string(trim($value));
- $value_type = sanitise_string(trim($value_type));
+ $value_type = detect_metadata_valuetype(sanitise_string(trim($value_type)));
$owner_id = (int)$owner_id;
if ($owner_id==0) $owner_id = $_SESSION['id'];
$access_id = (int)$access_id;
- return insert_data("INSERT into {$CONFIG->dbprefix}metadata (object_id, object_type, name, value, value_type, owner_id, created, access_id) VALUES ($object_id,'$object_type','$name','$value','$value_type', $owner_id, $access_id)");
+ $id = insert_data("INSERT into {$CONFIG->dbprefix}metadata (object_id, object_type, name, value, value_type, owner_id, created, access_id) VALUES ($object_id,'$object_type','$name','$value','$value_type', $owner_id, $access_id)");
+
+ if (!$id)
+ {
+ $existing = get_data_row("SELECT * from {$CONFIG->dbprefix}metadata WHERE object_id = $object_id and object_type='$object_type' and name='$name' limit 1");
+ if ($existing)
+ {
+ $id = $existing->id;
+ $result = update_metadata($id,$name, $value, $value_type, $owner_id, $access_id);
+
+ if (!$result) return false;
+ }
+
+ }
+
+ return $id;
}
/**
@@ -148,7 +185,7 @@
$id = (int)$id;
$name = sanitise_string(trim($name));
$value = sanitise_string(trim($value));
- $value_type = sanitise_string(trim($value_type));
+ $value_type = detect_metadata_valuetype(sanitise_string(trim($value_type)));
$owner_id = (int)$owner_id;
if ($owner_id==0) $owner_id = $_SESSION['id'];