using System; using System.Collections.Generic; using Examine; using Microsoft.Extensions.DependencyInjection; 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.Web.Common.DependencyInjection; using Umbraco.Extensions; namespace Umbraco.FriendlyExtensions { public static class FriendlyPublishedContentExtensions { private static IVariationContextAccessor VariationContextAccessor { get; } = StaticServiceProvider.Instance.GetRequiredService(); private static IPublishedModelFactory PublishedModelFactory { get; } = StaticServiceProvider.Instance.GetRequiredService(); private static IPublishedUrlProvider PublishedUrlProvider { get; } = StaticServiceProvider.Instance.GetRequiredService(); private static IUserService UserService { get; } = StaticServiceProvider.Instance.GetRequiredService(); private static IUmbracoContextAccessor UmbracoContextAccessor { get; } = StaticServiceProvider.Instance.GetRequiredService(); private static ISiteDomainHelper SiteDomainHelper { get; } = StaticServiceProvider.Instance.GetRequiredService(); private static IExamineManager ExamineManager { get; } = StaticServiceProvider.Instance.GetRequiredService(); /// /// Creates a strongly typed published content model for an internal published content. /// /// The internal published content. /// The strongly typed published content model. public static IPublishedContent CreateModel( this IPublishedContent content) => content.CreateModel(PublishedModelFactory); /// /// Gets the name of the content item. /// /// The content item. /// The specific culture to get the name for. If null is used the current culture is used (Default is null). public static string Name( this IPublishedContent content, string culture = null) => content.Name(VariationContextAccessor, culture); /// /// Gets the URL segment of the content item. /// /// The content item. /// The specific culture to get the URL segment for. If null is used the current culture is used (Default is null). public static string UrlSegment( this IPublishedContent content, string culture = null) => content.UrlSegment(VariationContextAccessor, culture); /// /// Gets the url for a media. /// /// The content item. /// The culture (use current culture by default). /// The url mode (use site configuration by default). /// The alias of the property (use 'umbracoFile' by default). /// The url for the media. /// /// The value of this property is contextual. It depends on the 'current' request uri, /// if any. In addition, when the content type is multi-lingual, this is the url for the /// specified culture. Otherwise, it is the invariant url. /// public static string MediaUrl( this IPublishedContent content, string culture = null, UrlMode mode = UrlMode.Default, string propertyAlias = Constants.Conventions.Media.File) => content.MediaUrl(PublishedUrlProvider, culture, mode, propertyAlias); /// /// Gets the name of the content item creator. /// /// The content item. public static string CreatorName(this IPublishedContent content) => content.CreatorName(UserService); /// /// Gets the name of the content item writer. /// /// The content item. public static string WriterName(this IPublishedContent content) => content.WriterName(UserService); /// /// 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, Uri current = null) => content.GetCultureFromDomains(UmbracoContextAccessor, SiteDomainHelper, current); public static IEnumerable SearchDescendants( this IPublishedContent content, string term, string indexName = null) => content.SearchDescendants(ExamineManager, UmbracoContextAccessor, term, indexName); public static IEnumerable SearchChildren( this IPublishedContent content, string term, string indexName = null) => content.SearchChildren(ExamineManager, UmbracoContextAccessor, term, indexName); } }