using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml.XPath;
using Umbraco.Core;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Strings;
using Umbraco.Core.Templates;
using Umbraco.Core.Xml;
namespace Umbraco.Web.Website
{
///
/// A helper class that provides many useful methods and functionality for using Umbraco in templates
///
///
/// This object is a request based lifetime
///
public class UmbracoHelper
{
private readonly IPublishedContentQuery _publishedContentQuery;
private readonly IUmbracoComponentRenderer _componentRenderer;
private readonly ICultureDictionaryFactory _cultureDictionaryFactory;
private IPublishedContent _currentPage;
private ICultureDictionary _cultureDictionary;
#region Constructors
///
/// Initializes a new instance of .
///
/// The item assigned to the helper.
///
///
///
/// Sets the current page to the context's published content request's content item.
public UmbracoHelper(ICultureDictionaryFactory cultureDictionary,
IUmbracoComponentRenderer componentRenderer,
IPublishedContentQuery publishedContentQuery)
{
_cultureDictionaryFactory = cultureDictionary ?? throw new ArgumentNullException(nameof(cultureDictionary));
_componentRenderer = componentRenderer ?? throw new ArgumentNullException(nameof(componentRenderer));
_publishedContentQuery = publishedContentQuery ?? throw new ArgumentNullException(nameof(publishedContentQuery));
}
///
/// Initializes a new empty instance of .
///
/// For tests - nothing is initialized.
internal UmbracoHelper()
{ }
#endregion
///
/// Gets (or sets) the current item assigned to the UmbracoHelper.
///
///
///
/// Note that this is the assigned IPublishedContent item to the
/// UmbracoHelper, this is not necessarily the Current IPublishedContent
/// item being rendered that is assigned to the UmbracoContext.
/// This IPublishedContent object is contextual to the current UmbracoHelper instance.
///
///
/// In some cases accessing this property will throw an exception if
/// there is not IPublishedContent assigned to the Helper this will
/// only ever happen if the Helper is constructed via DI during a non front-end request.
///
///
/// Thrown if the
/// UmbracoHelper is constructed with an UmbracoContext and it is not a
/// front-end request.
public IPublishedContent AssignedContentItem
{
get
{
if (_currentPage != null)
{
return _currentPage;
}
throw new InvalidOperationException(
$"Cannot return the {nameof(IPublishedContent)} because the {nameof(UmbracoHelper)} was not constructed with an {nameof(IPublishedContent)}."
);
}
set => _currentPage = value;
}
///
/// Renders the template for the specified pageId and an optional altTemplateId
///
/// The content id
/// If not specified, will use the template assigned to the node
public async Task RenderTemplateAsync(int contentId, int? altTemplateId = null)
=> await _componentRenderer.RenderTemplateAsync(contentId, altTemplateId);
#region RenderMacro
///
/// Renders the macro with the specified alias.
///
/// The alias.
///
public IHtmlEncodedString RenderMacro(string alias)
{
return _componentRenderer.RenderMacro(AssignedContentItem?.Id ?? 0, alias, null);
}
///
/// Renders the macro with the specified alias, passing in the specified parameters.
///
/// The alias.
/// The parameters.
///
public IHtmlEncodedString RenderMacro(string alias, object parameters)
{
return _componentRenderer.RenderMacro(AssignedContentItem?.Id ?? 0, alias, parameters?.ToDictionary());
}
///
/// Renders the macro with the specified alias, passing in the specified parameters.
///
/// The alias.
/// The parameters.
///
public IHtmlEncodedString RenderMacro(string alias, IDictionary parameters)
{
return _componentRenderer.RenderMacro(AssignedContentItem?.Id ?? 0, alias, parameters);
}
#endregion
#region Dictionary
///
/// Returns the dictionary value for the key specified
///
///
///
public string GetDictionaryValue(string key)
{
return CultureDictionary[key];
}
///
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
///
/// key of dictionary item
/// fall back text if dictionary item is empty - Name altText to match Umbraco.Field
///
public string GetDictionaryValue(string key, string altText)
{
var dictionaryValue = GetDictionaryValue(key);
if (String.IsNullOrWhiteSpace(dictionaryValue))
{
dictionaryValue = altText;
}
return dictionaryValue;
}
///
/// Returns the ICultureDictionary for access to dictionary items
///
public ICultureDictionary CultureDictionary => _cultureDictionary ??= _cultureDictionaryFactory.CreateDictionary();
#endregion
#region Content
///
/// Gets a content item from the cache.
///
/// The unique identifier, or the key, of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent Content(object id)
{
return ContentForObject(id);
}
private IPublishedContent ContentForObject(object id) => _publishedContentQuery.Content(id);
public IPublishedContent ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
{
return _publishedContentQuery.ContentSingleAtXPath(xpath, vars);
}
///
/// Gets a content item from the cache.
///
/// The unique identifier of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent Content(int id) => _publishedContentQuery.Content(id);
///
/// Gets a content item from the cache.
///
/// The key of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent Content(Guid id) => _publishedContentQuery.Content(id);
///
/// Gets a content item from the cache.
///
/// The unique identifier, or the key, of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent Content(string id) => _publishedContentQuery.Content(id);
public IPublishedContent Content(Udi id) => _publishedContentQuery.Content(id);
///
/// Gets content items from the cache.
///
/// The unique identifiers, or the keys, of the content items.
/// The content items that were found in the cache.
/// Does not support mixing identifiers and keys.
public IEnumerable Content(params object[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(params Udi[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(params GuidUdi[] ids) => _publishedContentQuery.Content(ids);
private IEnumerable ContentForObjects(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets content items from the cache.
///
/// The unique identifiers of the content items.
/// The content items that were found in the cache.
public IEnumerable Content(params int[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets content items from the cache.
///
/// The keys of the content items.
/// The content items that were found in the cache.
public IEnumerable Content(params Guid[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets content items from the cache.
///
/// The unique identifiers, or the keys, of the content items.
/// The content items that were found in the cache.
/// Does not support mixing identifiers and keys.
public IEnumerable Content(params string[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
public IEnumerable ContentAtXPath(string xpath, params XPathVariable[] vars)
{
return _publishedContentQuery.ContentAtXPath(xpath, vars);
}
public IEnumerable ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)
{
return _publishedContentQuery.ContentAtXPath(xpath, vars);
}
public IEnumerable ContentAtRoot()
{
return _publishedContentQuery.ContentAtRoot();
}
#endregion
#region Media
public IPublishedContent Media(Udi id) => _publishedContentQuery.Media(id);
public IPublishedContent Media(Guid id) => _publishedContentQuery.Media(id);
///
/// Overloaded method accepting an 'object' type
///
///
///
///
/// We accept an object type because GetPropertyValue now returns an 'object', we still want to allow people to pass
/// this result in to this method.
/// This method will throw an exception if the value is not of type int or string.
///
public IPublishedContent Media(object id)
{
return MediaForObject(id);
}
private IPublishedContent MediaForObject(object id) => _publishedContentQuery.Media(id);
public IPublishedContent Media(int id) => _publishedContentQuery.Media(id);
public IPublishedContent Media(string id) => _publishedContentQuery.Media(id);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params object[] ids) => _publishedContentQuery.Media(ids);
private IEnumerable MediaForObjects(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params int[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params string[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params Udi[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params GuidUdi[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
public IEnumerable MediaAtRoot()
{
return _publishedContentQuery.MediaAtRoot();
}
#endregion
}
}