/// <summary> /// Prefixes the Cdn Base URL to the specified resource path. /// </summary> public static string ResourceUrl(this IOrchardHelper orchardHelper, string resourcePath, bool?appendVersion = null) { var options = orchardHelper.HttpContext.RequestServices.GetRequiredService <IOptions <ResourceManagementOptions> >().Value; var fileVersionProvider = orchardHelper.HttpContext.RequestServices.GetRequiredService <IFileVersionProvider>(); if (resourcePath.StartsWith("~/", StringComparison.Ordinal)) { resourcePath = orchardHelper.HttpContext.Request.PathBase.Add(resourcePath.Substring(1)).Value; } // If append version is set, allow it to override the site setting. if (resourcePath != null && ((appendVersion.HasValue && appendVersion == true) || (!appendVersion.HasValue && options.AppendVersion == true))) { resourcePath = fileVersionProvider.AddFileVersionToPath(orchardHelper.HttpContext.Request.PathBase, resourcePath); } // Don't prefix cdn if the path includes a protocol, i.e. is an external url, or is in debug mode. if (!options.DebugMode && !String.IsNullOrEmpty(options.CdnBaseUrl) && // Don't evaluate with Uri.TryCreate as it produces incorrect results on Linux. !resourcePath.StartsWith("https://", StringComparison.OrdinalIgnoreCase) && !resourcePath.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !resourcePath.StartsWith("//", StringComparison.OrdinalIgnoreCase) && !resourcePath.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) { resourcePath = options.CdnBaseUrl + resourcePath; } return(resourcePath); }
/// <summary> /// Prefixes the Cdn Base URL to the specified resource path. /// </summary> public static string ResourceUrl(this IOrchardHelper orchardHelper, string resourcePath, bool?appendVersion = null) { var options = orchardHelper.HttpContext.RequestServices.GetRequiredService <IOptions <ResourceManagementOptions> >().Value; var fileVersionProvider = orchardHelper.HttpContext.RequestServices.GetRequiredService <IFileVersionProvider>(); if (resourcePath.StartsWith("~/", StringComparison.Ordinal)) { resourcePath = orchardHelper.HttpContext.Request.PathBase.Add(resourcePath.Substring(1)).Value; } // If append version is set, allow it to override the site setting. if (resourcePath != null && ((appendVersion.HasValue && appendVersion == true) || (!appendVersion.HasValue && options.AppendVersion == true))) { resourcePath = fileVersionProvider.AddFileVersionToPath(orchardHelper.HttpContext.Request.PathBase, resourcePath); } // Don't prefix cdn if the path is absolute, or is in debug mode. if (!options.DebugMode && !String.IsNullOrEmpty(options.CdnBaseUrl) && !Uri.TryCreate(resourcePath, UriKind.Absolute, out var uri)) { resourcePath = options.CdnBaseUrl + resourcePath; } return(resourcePath); }
/// <summary> /// Parses a liquid string and returns the result as a string /// </summary> /// <param name="liquid">The liquid to parse.</param> /// <param name="model">A model to bind against.</param> public static Task <string> LiquidAsync(this IOrchardHelper orchardHelper, string liquid, object model = null) { var liquidTemplateManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <ILiquidTemplateManager>(); var htmlEncoder = orchardHelper.HttpContext.RequestServices.GetRequiredService <HtmlEncoder>(); return(liquidTemplateManager.RenderAsync(liquid, htmlEncoder, model)); }
/// <summary> /// Gets the culture for a given <see cref="ContentItem"/>. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param> /// <param name="contentItem">The <see cref="ContentItem"/> in which to get its culture.</param> /// <returns></returns> public async static Task <CultureInfo> GetContentCultureAsync(this IOrchardHelper orchardHelper, ContentItem contentItem) { var contentManager = orchardHelper.HttpContext.RequestServices.GetService <IContentManager>(); var cultureAspect = await contentManager.PopulateAspectAsync(contentItem, new CultureAspect()); return(cultureAspect.Culture); }
/// <summary> /// Converts Markdown string to HTML. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param> /// <param name="markdown">The markdown to convert.</param> /// <param name="sanitize">Whether to sanitze the markdown. Defaults to <see langword="true"/>.</param> public static async Task <IHtmlContent> MarkdownToHtmlAsync(this IOrchardHelper orchardHelper, string markdown, bool sanitize = true) { var shortcodeService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IShortcodeService>(); var markdownService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IMarkdownService>(); // The default Markdown option is to entity escape html // so filters must be run after the markdown has been processed. markdown = markdownService.ToHtml(markdown ?? ""); // The liquid rendering is for backwards compatability and can be removed in a future version. if (!sanitize) { var liquidTemplateManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <ILiquidTemplateManager>(); var htmlEncoder = orchardHelper.HttpContext.RequestServices.GetRequiredService <HtmlEncoder>(); markdown = await liquidTemplateManager.RenderAsync(markdown, htmlEncoder); } // TODO provide context argument (optional on this helper as with the liquid helper?) markdown = await shortcodeService.ProcessAsync(markdown); if (sanitize) { var sanitizer = orchardHelper.HttpContext.RequestServices.GetRequiredService <IHtmlSanitizerService>(); markdown = sanitizer.Sanitize(markdown); } return(new HtmlString(markdown)); }
public static async Task <IQueryResults> ContentQueryResultsAsync(this IOrchardHelper orchardHelper, string queryName, Dictionary <string, object> parameters) { var contentItems = new List <ContentItem>(); var queryResult = await orchardHelper.QueryResultsAsync(queryName, parameters); if (queryResult.Items != null) { foreach (var item in queryResult.Items) { if (!(item is ContentItem contentItem)) { contentItem = null; if (item is JObject jObject) { contentItem = jObject.ToObject <ContentItem>(); } } // If input is a 'JObject' but which not represents a 'ContentItem', // a 'ContentItem' is still created but with some null properties. if (contentItem?.ContentItemId == null) { continue; } contentItems.Add(contentItem); } queryResult.Items = contentItems; } return(queryResult); }
/// <summary> /// Returns a content item id by its slug. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param> /// <param name="slug">The slug.</param> /// <example>GetContentItemIdBySlugAsync("myblog/my-blog-post")</example> /// <returns>A content item id or <c>null</c> if it was not found.</returns> public static async Task <string> GetContentItemIdBySlugAsync(this IOrchardHelper orchardHelper, string slug) { if (String.IsNullOrEmpty(slug)) { return(null); } // Provided for backwards compatability and avoiding confusion. if (slug.StartsWith("slug:", StringComparison.OrdinalIgnoreCase)) { slug = slug.Substring(5); } if (!slug.StartsWith('/')) { slug = "/" + slug; } var autorouteEntries = orchardHelper.HttpContext.RequestServices.GetService <IAutorouteEntries>(); (var found, var entry) = await autorouteEntries.TryGetEntryByPathAsync(slug); if (found) { return(entry.ContentItemId); } return(null); }
/// <summary> /// Applies short codes to html. /// </summary> /// <param name="html">The html to apply short codes.</param> public static async Task <IHtmlContent> HtmlToShortCodesAsync(this IOrchardHelper orchardHelper, string html) { var shortCodeService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IShortCodeService>(); html = await shortCodeService.ProcessAsync(html); return(new HtmlString(html)); }
/// <summary> /// Applies short codes to html. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/></param> /// <param name="html">The html to apply short codes.</param> public static async Task <string> ShortcodesAsync(this IOrchardHelper orchardHelper, string html) { var shortcodeService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IShortcodeService>(); html = await shortcodeService.ProcessAsync(html); return(html); }
/// <summary> /// Sanitizes a string of html. /// </summary> /// <param name="html">The html to sanitize.</param> public static IHtmlContent SanitizeHtml(this IOrchardHelper orchardHelper, string html) { var sanitizer = orchardHelper.HttpContext.RequestServices.GetRequiredService <IHtmlSanitizerService>(); html = sanitizer.Sanitize(html); return(new HtmlString(html)); }
public TaxonomyPartDisplayDriver( IOrchardHelper orchardHelper, IContentDefinitionManager contentDefinitionManager ) { _orchardHelper = orchardHelper; _contentDefinitionManager = contentDefinitionManager; }
/// <summary> /// Loads a content item by its handle. /// </summary> /// <param name="handle">The handle to load.</param> /// <param name="latest">Whether a draft should be loaded if available. <c>false</c> by default.</param> /// <example>GetContentItemByHandleAsync("alias:carousel")</example> /// <example>GetContentItemByHandleAsync("slug:myblog/my-blog-post", true)</example> /// <returns>A content item with the specific name, or <c>null</c> if it doesn't exist.</returns> public static async Task <ContentItem> GetContentItemByHandleAsync(this IOrchardHelper orchardHelper, string handle, bool latest = false) { var contentItemId = await GetContentItemIdByHandleAsync(orchardHelper, handle); var contentManager = orchardHelper.HttpContext.RequestServices.GetService <IContentManager>(); return(await contentManager.GetAsync(contentItemId, latest?VersionOptions.Latest : VersionOptions.Published)); }
/// <summary> /// Query content items. /// </summary> public static async Task <IEnumerable <ContentItem> > QueryContentItemsAsync(this IOrchardHelper orchardHelper, Func <IQuery <ContentItem, ContentItemIndex>, IQuery <ContentItem> > query) { var contentManager = orchardHelper.HttpContext.RequestServices.GetService <IContentManager>(); var session = orchardHelper.HttpContext.RequestServices.GetService <ISession>(); var contentItems = await query(session.Query <ContentItem, ContentItemIndex>()).ListAsync(); return(await contentManager.LoadAsync(contentItems)); }
/// <summary> /// Converts Markdown string to HTML. /// </summary> /// <param name="markdown">The markdown to convert.</param> public static async Task <IHtmlContent> MarkdownToHtmlAsync(this IOrchardHelper orchardHelper, string markdown) { var liquidTemplateManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <ILiquidTemplateManager>(); var htmlEncoder = orchardHelper.HttpContext.RequestServices.GetRequiredService <HtmlEncoder>(); markdown = await liquidTemplateManager.RenderAsync(markdown, htmlEncoder); return(new HtmlString(Markdig.Markdown.ToHtml(markdown))); }
/// <summary> /// Returns a URL with custom resizing parameters for a media profile for an existing image path. /// </summary> public static async Task <string> ImageProfileResizeUrlAsync(this IOrchardHelper orchardHelper, string imagePath, string imageProfile, int?width = null, int?height = null, ResizeMode resizeMode = ResizeMode.Undefined, int?quality = null, Format format = Format.Undefined, Anchor anchor = null, string bgcolor = null) { var mediaProfileService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IMediaProfileService>(); var queryStringParams = await mediaProfileService.GetMediaProfileCommands(imageProfile); var resizedUrl = ImageSharpUrlFormatter.GetImageResizeUrl(imagePath, queryStringParams, width, height, resizeMode, quality, format, anchor, bgcolor); return(orchardHelper.TokenizeUrl(resizedUrl)); }
/// <summary> /// Parses a liquid string to HTML /// </summary> /// <param name="liquid">The liquid to parse.</param> /// <param name="model">(optional)A model to bind against.</param> public static async Task <IHtmlContent> LiquidToSanitizedHtmlAsync(this IOrchardHelper orchardHelper, string liquid, object model = null) { var liquidTemplateManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <ILiquidTemplateManager>(); var htmlSanitizer = orchardHelper.HttpContext.RequestServices.GetRequiredService <IHtmlSanitizer>(); var htmlEncoder = orchardHelper.HttpContext.RequestServices.GetRequiredService <HtmlEncoder>(); liquid = await liquidTemplateManager.RenderAsync(liquid, htmlEncoder, model); return(new HtmlString(htmlSanitizer.Sanitize(liquid))); }
public static async Task <IHtmlContent> EditForLinkAsync(this IOrchardHelper orchardHelper, string linkText, ContentItem contentItem) { var viewContextAccessor = orchardHelper.HttpContext.RequestServices.GetRequiredService <ViewContextAccessor>(); var viewContext = viewContextAccessor.ViewContext; var helper = MakeHtmlHelper(viewContext, viewContext.ViewData); var contentManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <IContentManager>(); var metadata = await contentManager.PopulateAspectAsync <ContentItemMetadata>(contentItem); return(helper.ActionLink(linkText, metadata.EditorRouteValues["action"].ToString(), metadata.EditorRouteValues)); }
/// <summary> /// Parses a liquid string to HTML. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param> /// <param name="liquid">The liquid to parse.</param> /// <param name="model">A model to bind against.</param> public static async Task <IHtmlContent> LiquidToHtmlAsync(this IOrchardHelper orchardHelper, string liquid, object model) { var serviceProvider = orchardHelper.HttpContext.RequestServices; var liquidTemplateManager = serviceProvider.GetRequiredService <ILiquidTemplateManager>(); var htmlEncoder = serviceProvider.GetRequiredService <HtmlEncoder>(); var result = await liquidTemplateManager.RenderHtmlContentAsync(liquid, htmlEncoder, model); return(result); }
public static async Task <int> CommentCount(this IOrchardHelper orchardHelper, ContentItem contentItem) { var session = orchardHelper.HttpContext.RequestServices.GetService <ISession>(); IEnumerable <ContentItem> comments = await session.Query <ContentItem, ContentItemIndex>() .Where(x => x.ContentType == "CommentPost" && x.Latest == true && x.Published == true) .ListAsync(); int commentCount = comments.Count(x => BelongsToDocument(x, contentItem.ContentItemId)); return(commentCount); }
/// <summary> /// Processes shortcodes contained inside html. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/></param> /// <param name="html">The html string contained shortcodes.</param> /// <param name="model">The ambient shape view model.</param> public static async Task <IHtmlContent> ShortcodesToHtmlAsync(this IOrchardHelper orchardHelper, string html, object model = null) { var shortcodeService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IShortcodeService>(); var context = new Context(); // Retrieve the 'ContentItem' from the ambient shape view model. if (model is Shape shape && shape.TryGetProperty("ContentItem", out object contentItem)) { context["ContentItem"] = contentItem; }
/// <summary> /// Returns a the term from its content item id and workmap. /// </summary> /// <param name="workmapContentItemId">The workmap content item id.</param> /// <param name="workmaptermContentItemId">The term content item id.</param> /// <param name="orchardHelper">The term content item id.</param> /// <returns>A content item id <c>null</c> if it was not found.</returns> public static async Task <ContentItem> GetWorkMapTermAsync(this IOrchardHelper orchardHelper, string workmapContentItemId, string workmaptermContentItemId) { var contentManager = orchardHelper.HttpContext.RequestServices.GetService <IContentManager>(); var workmap = await contentManager.GetAsync(workmapContentItemId); if (workmap == null) { return(null); } return(FindTerm(workmap.Content.WorkMapPart.WorkMapTerms as JArray, workmaptermContentItemId)); }
/// <summary> /// Parses a liquid string to HTML /// </summary> /// <param name="liquid">The liquid to parse.</param> /// <param name="model">(optional)A model to bind against.</param> public static async Task <string> LiquidShortcodesAsync(this IOrchardHelper orchardHelper, string liquid, object model = null) { var liquidTemplateManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <ILiquidTemplateManager>(); var shortcodeService = orchardHelper.HttpContext.RequestServices.GetRequiredService <IShortcodeService>(); var htmlEncoder = orchardHelper.HttpContext.RequestServices.GetRequiredService <HtmlEncoder>(); liquid = await liquidTemplateManager.RenderStringAsync(liquid, htmlEncoder, model); liquid = await shortcodeService.ProcessAsync(liquid); return(liquid); }
/// <summary> /// Returns a term from its content item id and taxonomy. /// </summary> /// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param> /// <param name="taxonomyContentItemId">The taxonomy content item id.</param> /// <param name="termContentItemId">The term content item id.</param> /// <returns>A content item id <c>null</c> if it was not found.</returns> public static async Task <ContentItem> GetTaxonomyTermAsync(this IOrchardHelper orchardHelper, string taxonomyContentItemId, string termContentItemId) { var contentManager = orchardHelper.HttpContext.RequestServices.GetService <IContentManager>(); var taxonomy = await contentManager.GetAsync(taxonomyContentItemId); if (taxonomy == null) { return(null); } return(FindTerm(taxonomy.Content.TaxonomyPart.Terms as JArray, termContentItemId)); }
/// <summary> /// Returns the relative URL of the specifier asset path with optional resizing parameters. /// </summary> public static string AssetUrl(this IOrchardHelper orchardHelper, string assetPath, int?width = null, int?height = null, ResizeMode resizeMode = ResizeMode.Undefined) { var mediaFileStore = orchardHelper.HttpContext.RequestServices.GetService <IMediaFileStore>(); if (mediaFileStore == null) { return(assetPath); } var resolvedAssetPath = mediaFileStore.MapPathToPublicUrl(assetPath); return(orchardHelper.ImageResizeUrl(resolvedAssetPath, width, height, resizeMode)); }
public static async Task <IEnumerable> QueryAsync(this IOrchardHelper orchardHelper, string queryName, IDictionary <string, object> parameters) { var queryManager = orchardHelper.HttpContext.RequestServices.GetService <IQueryManager>(); var query = await queryManager.GetQueryAsync(queryName); if (query == null) { return(null); } return((IEnumerable)await queryManager.ExecuteQueryAsync(query, parameters)); }
private static string TokenizeUrl(this IOrchardHelper orchardHelper, string url) { var mediaOptions = orchardHelper.HttpContext.RequestServices.GetService <IOptions <MediaOptions> >().Value; if (mediaOptions.UseTokenizedQueryString) { var mediaTokenService = orchardHelper.HttpContext.RequestServices.GetService <IMediaTokenService>(); url = mediaTokenService.AddTokenToPath(url); } return(url); }
/// <summary> /// Query all system tags. /// </summary> public static async Task <IEnumerable <string> > QueryAllTagsAsync(this IOrchardHelper orchardHelper) { var siteService = orchardHelper.HttpContext.RequestServices.GetService <ISiteService>(); var siteSettings = await siteService.GetSiteSettingsAsync(); var globalTags = siteSettings.As <TagsSettings>().Tags; var vm = new TagsSettingsViewModel(); vm.Tags = globalTags; return(vm.TagsCollection); }
public async static Task <string> GetItemEditLinkAsync( this IOrchardHelper orchardHelper, string linkText, ContentItem contentItem) { var urlHelperFactory = orchardHelper.HttpContext.RequestServices.GetService <IUrlHelperFactory>(); var viewContextAccessor = orchardHelper.HttpContext.RequestServices.GetService <ViewContextAccessor>(); var contentManager = orchardHelper.HttpContext.RequestServices.GetService <IContentManager>(); var urlHelper = urlHelperFactory.GetUrlHelper(viewContextAccessor.ViewContext); var metadata = await contentManager.PopulateAspectAsync <ContentItemMetadata>(contentItem); return ($"<a href='{urlHelper.Action(metadata.EditorRouteValues["action"].ToString(), metadata.EditorRouteValues)}'>{linkText}</a>"); }
public static async ValueTask <List <string> > Tokenize(this IOrchardHelper orchardHelper, string field, params string[] text) { var analyzerManager = orchardHelper.HttpContext.RequestServices.GetService <LuceneAnalyzerManager>(); var luceneIndexSettingsService = orchardHelper.HttpContext.RequestServices.GetService <LuceneIndexSettingsService>(); var siteSettings = await orchardHelper.HttpContext.RequestServices.GetService <ISiteService>()?.GetSiteSettingsAsync(); var searchSettings = siteSettings.As <LuceneSettings>(); var luceneIndexSettings = await luceneIndexSettingsService.GetSettingsAsync(searchSettings.SearchIndex); var analyzerName = await luceneIndexSettingsService.LoadIndexAnalyzerAsync(luceneIndexSettings.IndexName); var analyzer = analyzerManager.CreateAnalyzer(analyzerName); var tokens = LuceneQueryService.Tokenize(field, string.Join(" ", text), analyzer).Distinct(); return(tokens.ToList()); }
/// <summary> /// Parses a liquid string to HTML. /// </summary> /// <param name="liquid">The liquid to parse.</param> /// <param name="model">A model to bind against.</param> /// <summary> public static async Task <IHtmlContent> LiquidToHtmlAsync(this IOrchardHelper orchardHelper, string liquid, object model) { var liquidTemplateManager = orchardHelper.HttpContext.RequestServices.GetRequiredService <ILiquidTemplateManager>(); var context = new TemplateContext(); if (model != null) { context.MemberAccessStrategy.Register(model.GetType()); context.LocalScope.SetValue("Model", model); } liquid = await liquidTemplateManager.RenderAsync(liquid, context); return(new HtmlString(liquid)); }