using System;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Xml.XPath;
using Umbraco.Core;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Security;
using Umbraco.Core.Services;
using Umbraco.Core.Xml;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web.Composing;
namespace Umbraco.Web
{
///
/// A helper class that provides many useful methods and functionality for using Umbraco in templates
///
public class UmbracoHelper : IUmbracoComponentRenderer
{
private static readonly HtmlStringUtilities StringUtilities = new HtmlStringUtilities();
private readonly UmbracoContext _umbracoContext;
private readonly IPublishedContent _currentPage;
private readonly IPublishedContentQuery _iQuery;
private readonly ServiceContext _services;
private readonly CacheHelper _appCache;
private IUmbracoComponentRenderer _componentRenderer;
private PublishedContentQuery _query;
private MembershipHelper _membershipHelper;
private ITagQuery _tag;
private IDataTypeService _dataTypeService;
private ICultureDictionary _cultureDictionary;
#region Constructors
///
/// Initializes a new instance of the class.
///
/// For tests.
internal UmbracoHelper(UmbracoContext umbracoContext, IPublishedContent content,
IPublishedContentQuery query,
ITagQuery tagQuery,
IDataTypeService dataTypeService,
ICultureDictionary cultureDictionary,
IUmbracoComponentRenderer componentRenderer,
MembershipHelper membershipHelper,
ServiceContext services,
CacheHelper appCache)
{
if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext));
if (content == null) throw new ArgumentNullException(nameof(content));
if (query == null) throw new ArgumentNullException(nameof(query));
if (tagQuery == null) throw new ArgumentNullException(nameof(tagQuery));
if (dataTypeService == null) throw new ArgumentNullException(nameof(dataTypeService));
if (cultureDictionary == null) throw new ArgumentNullException(nameof(cultureDictionary));
if (componentRenderer == null) throw new ArgumentNullException(nameof(componentRenderer));
if (membershipHelper == null) throw new ArgumentNullException(nameof(membershipHelper));
if (services == null) throw new ArgumentNullException(nameof(services));
if (appCache == null) throw new ArgumentNullException(nameof(appCache));
_umbracoContext = umbracoContext;
_tag = new TagQuery(tagQuery);
_dataTypeService = dataTypeService;
_cultureDictionary = cultureDictionary;
_componentRenderer = componentRenderer;
_membershipHelper = membershipHelper;
_currentPage = content;
_iQuery = query;
_services = services;
_appCache = appCache;
}
///
/// Initializes a new instance of the class.
///
/// For tests - nothing is initialized.
internal UmbracoHelper()
{ }
///
/// Initializes a new instance of the class with an Umbraco context
/// and a specific content item.
///
/// An Umbraco context.
/// A content item.
/// A services context.
/// An application cache helper.
/// Sets the current page to the supplied content item.
public UmbracoHelper(UmbracoContext umbracoContext, ServiceContext services, CacheHelper appCache, IPublishedContent content)
: this(umbracoContext, services, appCache)
{
if (content == null) throw new ArgumentNullException(nameof(content));
_currentPage = content;
}
///
/// Initializes a new instance of the class with an Umbraco context.
///
/// An Umbraco context.
/// A services context.
/// An application cache helper.
/// Sets the current page to the context's published content request's content item.
public UmbracoHelper(UmbracoContext umbracoContext, ServiceContext services, CacheHelper appCache)
{
if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext));
if (services == null) throw new ArgumentNullException(nameof(services));
if (appCache == null) throw new ArgumentNullException(nameof(appCache));
_umbracoContext = umbracoContext;
if (_umbracoContext.IsFrontEndUmbracoRequest)
_currentPage = _umbracoContext.PublishedContentRequest.PublishedContent;
_services = services;
_appCache = appCache;
}
#endregion
///
/// Gets the tag context.
///
public ITagQuery TagQuery => _tag ??
(_tag = new TagQuery(_services.TagService, _iQuery ?? ContentQuery));
///
/// Gets the query context.
///
public PublishedContentQuery ContentQuery => _query ??
(_query = _iQuery != null
? new PublishedContentQuery(_iQuery)
: new PublishedContentQuery(UmbracoContext.ContentCache, UmbracoContext.MediaCache));
///
/// Gets the Umbraco context.
///
public UmbracoContext UmbracoContext
{
get
{
if (_umbracoContext == null)
throw new NullReferenceException("UmbracoContext has not been set.");
return _umbracoContext;
}
}
///
/// Gets the membership helper.
///
public MembershipHelper MembershipHelper => _membershipHelper
?? (_membershipHelper = new MembershipHelper(UmbracoContext));
///
/// Gets the url provider.
///
public UrlProvider UrlProvider => UmbracoContext.UrlProvider;
///
/// Gets the datatype service.
///
private IDataTypeService DataTypeService => _dataTypeService
?? (_dataTypeService = _services.DataTypeService);
///
/// Gets the component renderer.
///
public IUmbracoComponentRenderer UmbracoComponentRenderer => _componentRenderer
?? (_componentRenderer = new UmbracoComponentRenderer(UmbracoContext));
///
/// Returns the current IPublishedContent 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. 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 with an UmbracoContext and it is not a 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)
throw new InvalidOperationException("Cannot return the " + typeof(IPublishedContent).Name + " because the " + typeof(UmbracoHelper).Name + " was constructed with an " + typeof(UmbracoContext).Name + " and the current request is not a front-end request.");
return _currentPage;
}
}
///
/// 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 pageId, int? altTemplateId = null)
{
return UmbracoComponentRenderer.RenderTemplate(pageId, altTemplateId);
}
#region RenderMacro
///
/// Renders the macro with the specified alias.
///
/// The alias.
///
public IHtmlString RenderMacro(string alias)
{
return UmbracoComponentRenderer.RenderMacro(alias, new { });
}
///
/// Renders the macro with the specified alias, passing in the specified parameters.
///
/// The alias.
/// The parameters.
///
public IHtmlString RenderMacro(string alias, object parameters)
{
return UmbracoComponentRenderer.RenderMacro(alias, parameters.ToDictionary