using System;
using System.Collections.Generic;
using System.Web;
using Examine;
using Microsoft.AspNetCore.Html;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Examine;
namespace Umbraco.Extensions
{
public static class PublishedContentExtensions
{
#region Creator/Writer Names
///
/// Gets the name of the content item creator.
///
/// The content item.
///
public static string CreatorName(this IPublishedContent content, IUserService userService) => userService.GetProfileById(content.CreatorId)?.Name;
///
/// Gets the name of the content item writer.
///
/// The content item.
///
public static string WriterName(this IPublishedContent content, IUserService userService) => userService.GetProfileById(content.WriterId)?.Name;
#endregion
#region Variations
///
/// Gets the culture assigned to a document by domains, in the context of a current Uri.
///
/// The document.
///
///
/// An optional current Uri.
/// The culture assigned to the document by domains.
///
/// In 1:1 multilingual setup, a document contains several cultures (there is not
/// one document per culture), and domains, withing the context of a current Uri, assign
/// a culture to that document.
///
public static string GetCultureFromDomains(this IPublishedContent content, IUmbracoContextAccessor umbracoContextAccessor, ISiteDomainMapper siteDomainHelper, Uri current = null)
{
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
return DomainUtilities.GetCultureFromDomains(content.Id, content.Path, current, umbracoContext, siteDomainHelper);
}
#endregion
#region Search
public static IEnumerable SearchDescendants(this IPublishedContent content, IExamineManager examineManager, IUmbracoContextAccessor umbracoContextAccessor, string term, string indexName = null)
{
indexName = string.IsNullOrEmpty(indexName) ? Constants.UmbracoIndexes.ExternalIndexName : indexName;
if (!examineManager.TryGetIndex(indexName, out var index))
{
throw new InvalidOperationException("No index found with name " + indexName);
}
//var t = term.Escape().Value;
//var luceneQuery = "+__Path:(" + content.Path.Replace("-", "\\-") + "*) +" + t;
var query = index.Searcher.CreateQuery()
.Field(UmbracoExamineFieldNames.IndexPathFieldName, (content.Path + ",").MultipleCharacterWildcard())
.And()
.ManagedQuery(term);
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
return query.Execute().ToPublishedSearchResults(umbracoContext.Content);
}
public static IEnumerable SearchChildren(this IPublishedContent content, IExamineManager examineManager, IUmbracoContextAccessor umbracoContextAccessor, string term, string indexName = null)
{
indexName = string.IsNullOrEmpty(indexName) ? Constants.UmbracoIndexes.ExternalIndexName : indexName;
if (!examineManager.TryGetIndex(indexName, out var index))
{
throw new InvalidOperationException("No index found with name " + indexName);
}
//var t = term.Escape().Value;
//var luceneQuery = "+parentID:" + content.Id + " +" + t;
var query = index.Searcher.CreateQuery()
.Field("parentID", content.Id)
.And()
.ManagedQuery(term);
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
return query.Execute().ToPublishedSearchResults(umbracoContext.Content);
}
#endregion
#region IsSomething: equality
///
/// If the specified is equal to , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
///
/// The HTML encoded value.
///
public static IHtmlContent IsEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsEqual(other, valueIfTrue, string.Empty);
///
/// If the specified is equal to , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
/// The value if false.
///
/// The HTML encoded value.
///
public static IHtmlContent IsEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsEqual(other) ? valueIfTrue : valueIfFalse));
///
/// If the specified is not equal to , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
///
/// The HTML encoded value.
///
public static IHtmlContent IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsNotEqual(other, valueIfTrue, string.Empty);
///
/// If the specified is not equal to , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
/// The value if false.
///
/// The HTML encoded value.
///
public static IHtmlContent IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsNotEqual(other) ? valueIfTrue : valueIfFalse));
#endregion
#region IsSomething: ancestors and descendants
///
/// If the specified is a decendant of , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
///
/// The HTML encoded value.
///
public static IHtmlContent IsDescendant(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsDescendant(other, valueIfTrue, string.Empty);
///
/// If the specified is a decendant of , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
/// The value if false.
///
/// The HTML encoded value.
///
public static IHtmlContent IsDescendant(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsDescendant(other) ? valueIfTrue : valueIfFalse));
public static IHtmlContent IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsDescendantOrSelf(other, valueIfTrue, string.Empty);
///
/// If the specified is a decendant of or are the same, the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
/// The value if false.
///
/// The HTML encoded value.
///
public static IHtmlContent IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsDescendantOrSelf(other) ? valueIfTrue : valueIfFalse));
public static IHtmlContent IsAncestor(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsAncestor(other, valueIfTrue, string.Empty);
///
/// If the specified is an ancestor of , the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
/// The value if false.
///
/// The HTML encoded value.
///
public static IHtmlContent IsAncestor(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsAncestor(other) ? valueIfTrue : valueIfFalse));
public static IHtmlContent IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsAncestorOrSelf(other, valueIfTrue, string.Empty);
///
/// If the specified is an ancestor of or are the same, the HTML encoded will be returned; otherwise, .
///
/// The content.
/// The other content.
/// The value if true.
/// The value if false.
///
/// The HTML encoded value.
///
public static IHtmlContent IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsAncestorOrSelf(other) ? valueIfTrue : valueIfFalse));
#endregion
}
}