]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Cleaned up tag searching so you can search on a specific tag. Useful in search so...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 13 Feb 2010 22:52:54 +0000 (22:52 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 13 Feb 2010 22:52:54 +0000 (22:52 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3938 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/entities.php
languages/en.php
mod/search/search_hooks.php
views/default/output/tags.php

index eb98d5949d8f914e7e7733c931bad93b46c4fe35..7124041798e3897e76d221ab0b66ab7cacd3495b 100644 (file)
@@ -1122,17 +1122,26 @@ abstract class ElggEntity implements
        }
 
        /**
-        * Returns tags for this entity using registered tag metadata names.
+        * Returns tags for this entity.
         *
+        * @param array $tag_names Optionally restrict by tag metadata names.
         * @return array
         */
-       public function getTags() {
+       public function getTags($tag_names = NULL) {
                global $CONFIG;
 
+               if ($tag_names && !is_array($tag_names)) {
+                       $tag_names = array($tag_names);
+               }
+
                $valid_tags = elgg_get_registered_tag_metadata_names();
                $entity_tags = array();
 
                foreach ($valid_tags as $tag_name) {
+                       if (is_array($tag_names) && !in_array($tag_name, $tag_names)) {
+                               continue;
+                       }
+
                        if ($tags = $this->$tag_name) {
                                // if a single tag, metadata returns a string.
                                // if multiple tags, metadata returns an array.
index 47661501fcceba10dc9abc2e7d399f6f403a1f13..b495c239673264d39a9d1747784d81783dc14992 100644 (file)
@@ -877,6 +877,12 @@ You cannot reply to this email.",
  */
        'word:blacklist' => 'and, the, then, but, she, his, her, him, one, not, also, about, now, hence, however, still, likewise, otherwise, therefore, conversely, rather, consequently, furthermore, nevertheless, instead, meanwhile, accordingly, this, seems, what, whom, whose, whoever, whomever',
 
+/**
+ * Tag labels
+ */
+
+       'tag_names:tags' => 'Tags',
+
 /**
  * Languages according to ISO 639-1
  */
index a74b1c2b3b366350177c227570c253cca2be36da..2236cb4f43eb453a8a5837e4ce12a13fd3c20808 100644 (file)
@@ -165,13 +165,33 @@ 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();
+       $valid_tag_names = 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']);
 
+       // if passed a tag metadata name, only search on that tag name.
+       // tag_name isn't included in the params because it's specific to
+       // tag searches.
+       if ($tag_names = get_input('tag_names')) {
+               if (is_array($tag_names)) {
+                       $search_tag_names = $tag_names;
+               } else {
+                       $search_tag_names = array($tag_names);
+               }
+
+               // check these are valid to avoid arbitrary metadata searches.
+               foreach ($search_tag_names as $i => $tag_name) {
+                       if (!in_array($tag_name, $valid_tag_names)) {
+                               unset($search_tag_names[$i]);
+                       }
+               }
+       } else {
+               $search_tag_names = $valid_tag_names;
+       }
+
        $name_value_pairs = array();
-       foreach ($valid_tags as $tag_name) {
+       foreach ($search_tag_names as $tag_name) {
                $name_value_pairs[] = array (
                        'name' => $tag_name,
                        'value' => $query,
@@ -193,10 +213,21 @@ 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->getTags();
-
-               if (is_array($tags)) {
-                       $tags = implode(', ', $tags);
+               $matched_tags_strs = array();
+
+               // get tags for each tag name requested to find which ones matched.
+               foreach ($search_tag_names as $tag_name) {
+                       $tags = $entity->getTags($tag_name);
+
+                       // @todo make one long tag string and run this through the highlight
+                       // function.  This might be confusing as it could chop off
+                       // the tag labels.
+                       if (in_array($query, $tags)) {
+                               if (is_array($tags)) {
+                                       $tag_name_str = elgg_echo("tag_names:$tag_name");
+                                       $matched_tags_strs[] = "$tag_name_str: " . implode(', ', $tags);
+                               }
+                       }
                }
 
                // Nick told me my idea was dirty, so I'm hard coding the numbers.
@@ -214,8 +245,9 @@ function search_tags_hook($hook, $type, $value, $params) {
                        $desc_str = $desc_tmp;
                }
 
-               $tags_str = search_get_highlighted_relevant_substrings($tags, $params['query']);
-               $tags_str = '(' . elgg_echo('tags') . ": $tags_str)";
+               $tags_str = implode('. ', $matched_tags_strs);
+               $tags_str = search_get_highlighted_relevant_substrings($tags_str, $params['query']);
+               $tags_str = "($tags_str)";
 
                $entity->setVolatileData('search_matched_title', $title_str);
                $entity->setVolatileData('search_matched_description', $desc_str);
index 1afc89d885a6a74b08d034a6e92d878171e4f3cf..b935367978f5f630fd98b50dbff32b76802eb1b5 100644 (file)
@@ -29,6 +29,17 @@ if (empty($vars['tags']) && !empty($vars['value'])) {
        $vars['tags'] = $vars['value'];
 }
 
+$tag_names_str = '';
+if (isset($vars['tag_names'])) {
+       if (is_array($vars['tag_names'])) {
+               foreach ($vars['tag_names'] as $tag_name) {
+                       $tag_names_str .= "&tag_names[]=$tag_name";
+               }
+       } else {
+               $tag_names_str = "&tag_names={$vars['tag_names']}";
+       }
+}
+
 if (!empty($vars['tags'])) {
        $tagstr = "";
        if (!is_array($vars['tags'])) {
@@ -45,7 +56,7 @@ if (!empty($vars['tags'])) {
                        $type = "";
                }
                if (is_string($tag)) {
-                       $tagstr .= "<a rel=\"tag\" href=\"{$vars['url']}pg/search/?q=".urlencode($tag) . "&search_type=tags{$type}{$subtype}{$object}\">" . htmlentities($tag, ENT_QUOTES, 'UTF-8') . "</a>";
+                       $tagstr .= "<a rel=\"tag\" href=\"{$vars['url']}pg/search/?q=".urlencode($tag) . "&search_type=tags{$type}{$subtype}{$object}{$tag_names_str}\">" . htmlentities($tag, ENT_QUOTES, 'UTF-8') . "</a>";
                }
        }
        echo $tagstr;