diff options
Diffstat (limited to 'engine/lib')
-rw-r--r-- | engine/lib/entities.php | 26 | ||||
-rw-r--r-- | engine/lib/tags.php | 63 |
2 files changed, 81 insertions, 8 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 930772943..eb98d5949 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -1121,6 +1121,32 @@ abstract class ElggEntity implements return $this->owner_guid; } + /** + * Returns tags for this entity using registered tag metadata names. + * + * @return array + */ + public function getTags() { + global $CONFIG; + + $valid_tags = elgg_get_registered_tag_metadata_names(); + $entity_tags = array(); + + foreach ($valid_tags as $tag_name) { + if ($tags = $this->$tag_name) { + // if a single tag, metadata returns a string. + // if multiple tags, metadata returns an array. + if (is_array($tags)) { + $entity_tags = array_merge($entity_tags, $tags); + } else { + $entity_tags[] = $tags; + } + } + } + + return $entity_tags; + } + // ITERATOR INTERFACE ////////////////////////////////////////////////////////////// /* * This lets an entity's attributes be displayed using foreach as a normal array. diff --git a/engine/lib/tags.php b/engine/lib/tags.php index 6f0caf8af..ea8d3ebcc 100644 --- a/engine/lib/tags.php +++ b/engine/lib/tags.php @@ -15,7 +15,6 @@ * This is quick and dirty. */ function calculate_tag_size($min, $max, $number_of_tags, $buckets = 6) { - $delta = (($max - $min) / $buckets); $thresholds = array(); @@ -90,6 +89,11 @@ function get_tags($threshold = 1, $limit = 10, $metadata_name = "", $entity_type $threshold = (int) $threshold; $limit = (int) $limit; + $registered_tags = elgg_get_registered_tag_metadata_names(); + if (!in_array($metadata_name, $registered_tags)) { + elgg_deprecated_notice('Tag metadata names must be registered by elgg_register_tag_metadata_name()', 1.7); + } + if (!empty($metadata_name)) { $metadata_name = (int) get_metastring_id($metadata_name); // test if any metadata with that name @@ -116,8 +120,6 @@ function get_tags($threshold = 1, $limit = 10, $metadata_name = "", $entity_type $site_guid = $CONFIG->site_id; } - //$access = get_access_list(); - $query = "SELECT msvalue.string as tag, count(msvalue.id) as total "; $query .= "FROM {$CONFIG->dbprefix}entities e join {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid "; if ($entity_subtype > 0) { @@ -154,9 +156,8 @@ function get_tags($threshold = 1, $limit = 10, $metadata_name = "", $entity_type $query .= " and e.time_created<=$end_ts"; } - //$userid = get_loggedin_userid(); - //$query .= " and (e.access_id in {$access} or (e.access_id = " . ACCESS_PRIVATE . " and e.owner_guid = {$userid}))"; - $query .= ' and ' . get_access_sql_suffix("e"); // Add access controls + // Add access controls + $query .= ' and ' . get_access_sql_suffix("e"); $query .= " group by msvalue.string having total > {$threshold} order by total desc limit {$limit} "; @@ -179,7 +180,53 @@ function get_tags($threshold = 1, $limit = 10, $metadata_name = "", $entity_type */ function display_tagcloud($threshold = 1, $limit = 10, $metadata_name = "", $entity_type = "object", $entity_subtype = "", $owner_guid = "", $site_guid = -1, $start_ts = "", $end_ts = "") { + + if (!in_array($metadata_name, $registered_tags)) { + elgg_deprecated_notice('Tag metadata names must be registered by elgg_register_tag_metadata_name()', 1.7); + } + return elgg_view("output/tagcloud",array('value' => get_tags($threshold, $limit, $metadata_name, $entity_type, $entity_subtype, $owner_guid, $site_guid, $start_ts, $end_ts), - 'object' => $entity_type, + 'object' => $entity_type, 'subtype' => $entity_subtype)); -}
\ No newline at end of file +} + +/** + * Registers a metadata name as containing tags for an entity. + * This is required if you are using a non-standard metadata name + * for your tags. + * + * @since 1.7 + * + * @param string $name + * @return TRUE + */ +function elgg_register_tag_metadata_name($name) { + global $CONFIG; + + if (!isset($CONFIG->registered_tag_metadata_names)) { + $CONFIG->registered_tag_metadata_names = array(); + } + + if (!in_array($name, $CONFIG->registered_tag_metadata_names)) { + $CONFIG->registered_tag_metadata_names[] = $name; + } + + return TRUE; +} + +/** + * Returns an array of valid metadata names for tags. + * + * @since 1.7 + * + * @return array + */ +function elgg_get_registered_tag_metadata_names() { + global $CONFIG; + + $names = (isset($CONFIG->registered_tag_metadata_names)) ? $CONFIG->registered_tag_metadata_names : array(); + return $names; +} + +// register the standard tags metadata name +elgg_register_tag_metadata_name('tags');
\ No newline at end of file |