aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-02-12 22:23:38 +0000
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-02-12 22:23:38 +0000
commit9fbed88ef70966dd6075b40bd3a8ed4f609423e8 (patch)
tree95bfbeebc86761b0560bdd99065dde6813f6ced7
parent2d889a3cc9b92fdb1c000236e8953f9d16fe8840 (diff)
downloadelgg-9fbed88ef70966dd6075b40bd3a8ed4f609423e8.tar.gz
elgg-9fbed88ef70966dd6075b40bd3a8ed4f609423e8.tar.bz2
Closes #1506: Added elgg_register_tag_metadata_name(), elgg_get_registered_tag_metadata_names(), and ElggEntity::getTags(). Search on tags updated to use registered tag names instead of hard-coded 'tags' metadata name.
git-svn-id: http://code.elgg.org/elgg/trunk@3936 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--CHANGES.txt4
-rw-r--r--engine/lib/entities.php26
-rw-r--r--engine/lib/tags.php63
-rw-r--r--mod/search/search_hooks.php19
4 files changed, 100 insertions, 12 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index af93f9913..4fcd086e3 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -43,7 +43,9 @@ http://code.elgg.org/elgg/.....
* delete_relationship() triggers the hook delete:relationship and passes the relationship object. #1213
* added ElggEntity::removeRelationship(). #1376.
* get_entity_dates() supports order by. #1406.
- * added elgg_http_add_url_query_elements().
+ * Added elgg_http_add_url_query_elements().
+ * Added elgg_register_tag_metadata_name() and elgg_get_registered_tag_metadata_names();
+ * Added ElggEntity::getTags().
Services API:
* Separated user and api authenticate processing
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
diff --git a/mod/search/search_hooks.php b/mod/search/search_hooks.php
index 77ab5c6e3..a74b1c2b3 100644
--- a/mod/search/search_hooks.php
+++ b/mod/search/search_hooks.php
@@ -165,9 +165,22 @@ function search_users_hook($hook, $type, $value, $params) {
function search_tags_hook($hook, $type, $value, $params) {
global $CONFIG;
+ $valid_tags = elgg_get_registered_tag_metadata_names();
+
// @todo will need to split this up to support searching multiple tags at once.
$query = sanitise_string($params['query']);
- $params['metadata_name_value_pair'] = array ('name' => 'tags', 'value' => $query, 'case_sensitive' => FALSE);
+
+ $name_value_pairs = array();
+ foreach ($valid_tags as $tag_name) {
+ $name_value_pairs[] = array (
+ 'name' => $tag_name,
+ 'value' => $query,
+ 'case_sensitive' => FALSE
+ );
+ }
+
+ $params['metadata_name_value_pairs'] = $name_value_pairs;
+ $params['metadata_name_value_pairs_operator'] = 'OR';
$entities = elgg_get_entities_from_metadata($params);
$params['count'] = TRUE;
@@ -180,10 +193,10 @@ function search_tags_hook($hook, $type, $value, $params) {
// add the volatile data for why these entities have been returned.
foreach ($entities as $entity) {
- $tags = $entity->tags;
+ $tags = $entity->getTags();
if (is_array($tags)) {
- $tags = implode(', ', $entity->tags);
+ $tags = implode(', ', $tags);
}
// Nick told me my idea was dirty, so I'm hard coding the numbers.