using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Web.Models;
namespace Umbraco.Web
{
///
/// A class that exposes methods used to query tag data in views
///
public class TagQuery : ITagQuery
{
//TODO: This class also acts as a wrapper for ITagQuery due to breaking changes, need to fix in
// version 8: http://issues.umbraco.org/issue/U4-6899
private readonly ITagQuery _wrappedQuery;
private readonly ITagService _tagService;
private readonly IPublishedContentQuery _contentQuery;
[Obsolete("Use the alternate constructor specifying the contentQuery instead")]
public TagQuery(ITagService tagService)
: this(tagService, new PublishedContentQuery(UmbracoContext.Current.ContentCache, UmbracoContext.Current.MediaCache))
{
}
[Obsolete("Use the alternate constructor specifying the ITypedPublishedContentQuery instead")]
public TagQuery(ITagService tagService, PublishedContentQuery contentQuery)
{
if (tagService == null) throw new ArgumentNullException("tagService");
if (contentQuery == null) throw new ArgumentNullException("contentQuery");
_tagService = tagService;
_contentQuery = contentQuery;
}
///
/// Constructor for wrapping ITagQuery, see http://issues.umbraco.org/issue/U4-6899
///
///
internal TagQuery(ITagQuery wrappedQuery)
{
if (wrappedQuery == null) throw new ArgumentNullException("wrappedQuery");
_wrappedQuery = wrappedQuery;
}
///
/// Constructor
///
///
///
public TagQuery(ITagService tagService, IPublishedContentQuery contentQuery)
{
if (tagService == null) throw new ArgumentNullException("tagService");
if (contentQuery == null) throw new ArgumentNullException("contentQuery");
_tagService = tagService;
_contentQuery = contentQuery;
}
///
/// Returns all content that is tagged with the specified tag value and optional tag group
///
///
///
///
public IEnumerable GetContentByTag(string tag, string tagGroup = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetContentByTag(tag, tagGroup);
var ids = _tagService.GetTaggedContentByTag(tag, tagGroup)
.Select(x => x.EntityId);
return _contentQuery.Content(ids)
.Where(x => x != null);
}
///
/// Returns all content that has been tagged with any tag in the specified group
///
///
///
public IEnumerable GetContentByTagGroup(string tagGroup)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetContentByTagGroup(tagGroup);
var ids = _tagService.GetTaggedContentByTagGroup(tagGroup)
.Select(x => x.EntityId);
return _contentQuery.Content(ids)
.Where(x => x != null);
}
///
/// Returns all Media that is tagged with the specified tag value and optional tag group
///
///
///
///
public IEnumerable GetMediaByTag(string tag, string tagGroup = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetMediaByTag(tag, tagGroup);
var ids = _tagService.GetTaggedMediaByTag(tag, tagGroup)
.Select(x => x.EntityId);
return _contentQuery.Media(ids)
.Where(x => x != null);
}
///
/// Returns all Media that has been tagged with any tag in the specified group
///
///
///
public IEnumerable GetMediaByTagGroup(string tagGroup)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetMediaByTagGroup(tagGroup);
var ids = _tagService.GetTaggedMediaByTagGroup(tagGroup)
.Select(x => x.EntityId);
return _contentQuery.Media(ids)
.Where(x => x != null);
}
//TODO: Should prob implement these, requires a bit of work on the member service to do this,
// also not sure if its necessary ?
//public IEnumerable GetMembersByTag(string tag, string tagGroup = null)
//{
//}
//public IEnumerable GetMembersByTagGroup(string tagGroup)
//{
//}
///
/// Get every tag stored in the database (with optional group)
///
public IEnumerable GetAllTags(string group = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetAllTags(group);
return Mapper.Map>(_tagService.GetAllTags(group));
}
///
/// Get all tags for content items (with optional group)
///
///
///
public IEnumerable GetAllContentTags(string group = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetAllContentTags(group);
return Mapper.Map>(_tagService.GetAllContentTags(group));
}
///
/// Get all tags for media items (with optional group)
///
///
///
public IEnumerable GetAllMediaTags(string group = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetAllMediaTags(group);
return Mapper.Map>(_tagService.GetAllMediaTags(group));
}
///
/// Get all tags for member items (with optional group)
///
///
///
public IEnumerable GetAllMemberTags(string group = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetAllMemberTags(group);
return Mapper.Map>(_tagService.GetAllMemberTags(group));
}
///
/// Returns all tags attached to a property by entity id
///
///
///
///
///
public IEnumerable GetTagsForProperty(int contentId, string propertyTypeAlias, string tagGroup = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetTagsForProperty(contentId, propertyTypeAlias, tagGroup);
return Mapper.Map>(_tagService.GetTagsForProperty(contentId, propertyTypeAlias, tagGroup));
}
///
/// Returns all tags attached to an entity (content, media or member) by entity id
///
///
///
///
public IEnumerable GetTagsForEntity(int contentId, string tagGroup = null)
{
//TODO: http://issues.umbraco.org/issue/U4-6899
if (_wrappedQuery != null) return _wrappedQuery.GetTagsForEntity(contentId, tagGroup);
return Mapper.Map>(_tagService.GetTagsForEntity(contentId, tagGroup));
}
}
}