}
/**
- * 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.
*/
'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
*/
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,
// 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.
$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);
$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'])) {
$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;