diff options
-rw-r--r-- | engine/lib/entities.php | 13 | ||||
-rw-r--r-- | languages/en.php | 6 | ||||
-rw-r--r-- | mod/search/search_hooks.php | 48 | ||||
-rw-r--r-- | views/default/output/tags.php | 13 |
4 files changed, 69 insertions, 11 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php index eb98d5949..712404179 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -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. diff --git a/languages/en.php b/languages/en.php index 47661501f..b495c2396 100644 --- a/languages/en.php +++ b/languages/en.php @@ -878,6 +878,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 */ "aa" => "Afar", diff --git a/mod/search/search_hooks.php b/mod/search/search_hooks.php index a74b1c2b3..2236cb4f4 100644 --- a/mod/search/search_hooks.php +++ b/mod/search/search_hooks.php @@ -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); diff --git a/views/default/output/tags.php b/views/default/output/tags.php index 1afc89d88..b93536797 100644 --- a/views/default/output/tags.php +++ b/views/default/output/tags.php @@ -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; |