using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.XPath;
using Umbraco.Core;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Xml;
using Umbraco.Web.Composing;
using Umbraco.Web.Mvc;
using Umbraco.Web.Security;
using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web
{
///
/// 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 ITagQuery _tagQuery;
private readonly MembershipHelper _membershipHelper;
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(IPublishedContent currentPage,
ITagQuery tagQuery,
ICultureDictionaryFactory cultureDictionary,
IUmbracoComponentRenderer componentRenderer,
IPublishedContentQuery publishedContentQuery,
MembershipHelper membershipHelper)
{
_tagQuery = tagQuery ?? throw new ArgumentNullException(nameof(tagQuery));
_cultureDictionaryFactory = cultureDictionary ?? throw new ArgumentNullException(nameof(cultureDictionary));
_componentRenderer = componentRenderer ?? throw new ArgumentNullException(nameof(componentRenderer));
_membershipHelper = membershipHelper ?? throw new ArgumentNullException(nameof(membershipHelper));
_publishedContentQuery = publishedContentQuery ?? throw new ArgumentNullException(nameof(publishedContentQuery));
_currentPage = currentPage;
}
///
/// Initializes a new empty instance of .
///
/// For tests - nothing is initialized.
internal UmbracoHelper()
{ }
#endregion
// ensures that we can return the specified value
[Obsolete("This method is only used in Obsolete properties")]
T Ensure(T o) where T : class => o ?? throw new InvalidOperationException("This UmbracoHelper instance has not been initialized.");
[Obsolete("Inject and use an instance of " + nameof(IUmbracoComponentRenderer) + " in the constructor for using it in classes or get it from Current.UmbracoComponentRenderer in views.")]
private IUmbracoComponentRenderer ComponentRenderer => Ensure(_componentRenderer);
[Obsolete("Inject and use an instance of " + nameof(ICultureDictionaryFactory) + " in the constructor for using it in classes or get it from Current.CultureDictionaryFactory in views.")]
private ICultureDictionaryFactory CultureDictionaryFactory => Ensure(_cultureDictionaryFactory);
///
/// Gets the tag context.
///
[Obsolete("Inject and use an instance of " + nameof(ITagQuery) + " in the constructor for using it in classes or get it from Current.TagQuery in views.")]
public ITagQuery TagQuery => Ensure(_tagQuery);
///
/// Gets the query context.
///
[Obsolete("Inject and use an instance of " + nameof(IPublishedContentQuery) + " in the constructor for using it in classes or get it from Current.PublishedContentQuery in views")]
public IPublishedContentQuery ContentQuery => Ensure(_publishedContentQuery);
///
/// Gets the membership helper.
///
[Obsolete("Inject and use an instance of " + nameof(Security.MembershipHelper) + " in the constructor instead")]
public MembershipHelper MembershipHelper => Ensure(_membershipHelper);
///
/// 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
///
///
/// If not specified, will use the template assigned to the node
///
public IHtmlString RenderTemplate(int contentId, int? altTemplateId = null)
{
return ComponentRenderer.RenderTemplate(contentId, altTemplateId);
}
#region RenderMacro
///
/// Renders the macro with the specified alias.
///
/// The alias.
///
public IHtmlString 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 IHtmlString RenderMacro(string alias, object parameters)
{
return ComponentRenderer.RenderMacro(AssignedContentItem?.Id ?? 0, alias, parameters?.ToDictionary