From f48adf4356dccf8d7769e12421a6ca4fdf6925dc Mon Sep 17 00:00:00 2001 From: icewing Date: Wed, 21 May 2008 17:08:35 +0000 Subject: Marcus Povey * Introducing find_metadata as a counterpart to get_entities_from_metadata git-svn-id: https://code.elgg.org/elgg/trunk@673 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/metadata.php | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'engine/lib') diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php index c099d6a9c..5de47d52e 100644 --- a/engine/lib/metadata.php +++ b/engine/lib/metadata.php @@ -317,6 +317,58 @@ return get_data("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.entity_guid=$entity_guid and (m.access_id in {$access} or (m.access_id = 0 and m.owner_guid = {$_SESSION['id']}))", "row_to_elggmetadata"); } + /** + * Get the metadata where the entities they are referring to match a given criteria. + * + * @param mixed $meta_name + * @param mixed $meta_value + * @param string $entity_type The type of entity to look for, eg 'site' or 'object' + * @param string $entity_subtype The subtype of the entity. + * @param int $limit + * @param int $offset + * @param string $order_by Optional ordering. + * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites. + */ + function find_metadata($meta_name = "", $meta_value = "", $entity_type = "", $entity_subtype = "", $limit = 10, $offset = 0, $order_by = "e.time_created desc", $site_guid = 0) + { + global $CONFIG; + + $meta_n = get_metastring_id($meta_name); + $meta_v = get_metastring_id($meta_value); + + $entity_type = sanitise_string($entity_type); + $entity_subtype = get_subtype_id($entity_type, $entity_subtype); + $limit = (int)$limit; + $offset = (int)$offset; + $order_by = sanitise_string($order_by); + $site_guid = (int) $site_guid; + if ($site_guid == 0) + $site_guid = $CONFIG->site_guid; + + $access = get_access_list(); + + $where = array(); + + if ($entity_type!="") + $where[] = "e.type='$entity_type'"; + if ($entity_subtype) + $where[] = "e.subtype=$entity_subtype"; + if ($meta_name!="") + $where[] = "m.name_id='$meta_n'"; + if ($meta_value!="") + $where[] = "m.value_id='$meta_v'"; + if ($site_guid > 0) + $where[] = "e.site_guid = {$site_guid}"; + + $query = "SELECT m.*, n.string as name, v.string as value from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}metadata m on e.guid = m.entity_guid JOIN {$CONFIG->dbprefix}metastrings v on m.value_id = v.id JOIN {$CONFIG->dbprefix}metastrings n on m.name_id = n.id where"; + foreach ($where as $w) + $query .= " $w and "; + $query .= " (e.access_id in {$access} or (e.access_id = 0 and e.owner_guid = {$_SESSION['id']}))"; // Add access controls + $query .= " order by $order_by limit $offset, $limit"; // Add order and limit + + return get_data($query, "row_to_elggmetadata"); + } + /** * Return a list of entities based on the given search criteria. * -- cgit v1.2.3