protected async Task <bool> SendEmailAsync(string email, string subject, object model, string viewName) { var options = ControllerContext.HttpContext.RequestServices.GetRequiredService <IOptions <MvcViewOptions> >(); ControllerContext.RouteData.Values["action"] = viewName; ControllerContext.RouteData.Values["controller"] = ""; var viewEngineResult = options.Value.ViewEngines.Select(x => x.FindView(ControllerContext, viewName, true)).FirstOrDefault(x => x != null); var displayContext = new DisplayContext() { ServiceProvider = ControllerContext.HttpContext.RequestServices, Value = await _shapeFactory.CreateAsync(viewName, model), ViewContext = new ViewContext(ControllerContext, viewEngineResult.View, ViewData, TempData, new StringWriter(), new HtmlHelperOptions()) }; var htmlContent = await _displayManager.ExecuteAsync(displayContext); var message = new MailMessage() { Body = WebUtility.HtmlDecode(htmlContent.ToString()), IsBodyHtml = true, Subject = subject }; message.To.Add(email); // send email var result = await _smtpService.SendAsync(message); return(result.Succeeded); }
public async Task <IHtmlContent> ShapeExecuteAsync(object shape) { if (shape == null) { return(new HtmlString(string.Empty)); } var context = new DisplayContext { Display = this, Value = shape, ViewContext = ViewContext }; return(await _displayManager.ExecuteAsync(context)); }
public Task <IHtmlContent> ShapeExecuteAsync(object shape) { if (shape == null) { return(Task.FromResult <IHtmlContent>(HtmlString.Empty)); } var context = new DisplayContext { DisplayAsync = this, Value = shape, ServiceProvider = _serviceProvider }; return(_htmlDisplay.ExecuteAsync(context)); }
protected async Task <bool> SendEmailAsync(string email, string subject, IShape model) { var options = ControllerContext.HttpContext.RequestServices.GetRequiredService <IOptions <MvcViewOptions> >(); // Just use the current context to get a view and then create a view context. var view = options.Value.ViewEngines .Select(x => x.FindView( ControllerContext, ControllerContext.ActionDescriptor.ActionName, false)).FirstOrDefault()?.View; var displayContext = new DisplayContext() { ServiceProvider = ControllerContext.HttpContext.RequestServices, Value = model, ViewContext = new ViewContext(ControllerContext, view, ViewData, TempData, new StringWriter(), new HtmlHelperOptions()) }; var body = string.Empty; using (var sw = new StringWriter()) { var htmlContent = await _displayManager.ExecuteAsync(displayContext); htmlContent.WriteTo(sw, HtmlEncoder.Default); body = sw.ToString(); } var message = new MailMessage() { Body = body, IsBodyHtml = true, Subject = subject }; message.To.Add(email); var result = await _smtpService.SendAsync(message); return(result.Succeeded); }
public Task <IHtmlContent> ShapeExecuteAsync(IShape shape) { // Check if the shape is null or empty. if (shape.IsNullOrEmpty()) { return(Task.FromResult <IHtmlContent>(HtmlString.Empty)); } // Check if the shape is pre-rendered. if (shape is IHtmlContent htmlContent) { return(Task.FromResult(htmlContent)); } var context = new DisplayContext { DisplayHelper = this, Value = shape, ServiceProvider = _serviceProvider }; return(_htmlDisplay.ExecuteAsync(context)); }
private async Task <IEnumerable <ContentItem> > FlattenOneLevel(ContentItem container, DisplayContext displayContext) { var items = new List <ContentItem>(); items.Add(container); var containedItems = await _listContainerService.GetChildContentItems(container.ContentItemId); // If the contained items are linked from the container, take the links' order into account. Also if links are present only linked items // will be processed. if (containedItems.Any()) { // var aliases = containedItems // //.Where(content => content.As<>() .As<IAliasAspect>() != null) // .Where(content => content.Has("AutoroutePart")==true ) // .Select(content => new // { // Content = content, // // Path = content.As<IAliasAspect>().Path // Path = content.Content.AutoroutePart?.Path?.Value?.ToString() // }) // .ToDictionary(alias => alias.Path); var childContentItemAliases = containedItems.Where(content => content.Has("AutoroutePart") == true).ToList(); // var aliases = aliasesContentItems.Select(x => x.Content["AutoroutePart"]["Path"].ToString()).ToDictionary(x=>x ); var aliasesChildContentItems = childContentItemAliases.ToDictionary(x => (x.Content["AutoroutePart"]["Path"]).ToString(), x => x); // var siteUri = new Uri(_siteService.GetSiteSettings().BaseUrl); // var siteSettings = await _siteService.GetSiteSettingsAsync(); var doc = new HtmlDocument(); // Stream outputStream = _shapeOutputGenerator.GenerateOutput(container.ContentItem.ContentManager.BuildDisplay(container, "File-ContainerFlattening")); // Stream outputStream = _shapeOutputGenerator.GenerateOutput(container.ContentItem.ContentManager.BuildDisplay(container, "File-ContainerFlattening")); var contentItemDisplayManager = _serviceProvider.GetService <IContentItemDisplayManager>(); dynamic shape = await contentItemDisplayManager.BuildDisplayAsync(container, _updateModelAccessor.ModelUpdater, "ContainerFlattening"); //replace with actual shape to render displayContext.Value = shape; var htmlContent = await _displayManager.ExecuteAsync(displayContext); doc.LoadHtml(htmlContent.ToString()); // var aliasAspect = container.As<IAliasAspect>(); var containerAliasAspect = container.Content.AutoroutePart; if (containerAliasAspect != null) { Uri siteUri = new Uri(_urlHelper.GetBaseUrl()); var links = doc.DocumentNode.SelectNodes("//a[@href]"); if (links != null) // See: https://htmlagilitypack.codeplex.com/workitem/29175 { //TEMP DISABLED BY GIANNIS string itemrelativepath = containerAliasAspect["Path"]; var containerItemUri = new Uri(siteUri, itemrelativepath); foreach (var link in links) { var href = link.GetAttributeValue("href", null); if (href != null) { Uri uri; if (Helpers.UrlHelper.UrlIsInternal(containerItemUri, href, out uri)) { var alias = uri.LocalPath.TrimStart('/'); if (aliasesChildContentItems.ContainsKey(alias)) { items.AddRange(await FlattenOneLevel( aliasesChildContentItems[alias].ContentItem, displayContext)); } } } } } } if (items.Count == 1) // No contained item was linked { foreach (var item in containedItems) { items.AddRange(await FlattenOneLevel(item, displayContext)); } } } return(items); }
public async Task <Stream> GenerateHtml(IEnumerable <ContentItem> contents, string extension, DisplayContext displayContext) { var firstContent = contents.First(); // var contentManager = firstContent.ContentItem.ContentManager; // var aliases = contents // // .Where(content => content.Has("AutoroutePart") == true) // .Select(content => new // { // Id = content.ContentItem.Id, // // Path = content.As<IAliasAspect>().Path // Path = content.Content.AutoroutePart?.Path?.Value?.ToString() // }) // .ToDictionary(alias => alias.Path); // var aliasesContentItems = contents.Where(content => content.Has("AutoroutePart") == true).ToList(); var aliasesContentItems = contents.Where(content => content.Has("AutoroutePart") == true).ToList(); // var aliases = aliasesContentItems.Select(x => x.Content["AutoroutePart"]["Path"]).ToDictionary(x=>x ); var aliases = aliasesContentItems.ToDictionary(x => (x.Content["AutoroutePart"]["Path"]).ToString(), x => x); // .Select(content => new // { // Id = content.ContentItem.Id, // // Path = content.As<IAliasAspect>().Path // Path = content.Content.AutoroutePart?.Path?.Value?.ToString() // }) // .ToDictionary(alias => alias.Path); // var siteUri = new Uri(_siteService.GetSiteSettings().BaseUrl); // var siteSettings = await _siteService.GetSiteSettingsAsync(); // var siteUri = siteSettings.BaseUrl; var contentItemDisplayManager = _serviceProvider.GetService <IContentItemDisplayManager>(); var contentShapes = contents.Select(async content => { // IShape contentShape = contentManager.BuildDisplay(content, "File-" + extension); IShape contentShape = await contentItemDisplayManager.BuildDisplayAsync(content, _updateModelAccessor.ModelUpdater, "File"); //.Result; // IShape contentShape = contentItemDisplayManager.BuildDisplayAsync(content, null, "Detail").Result; // IShape contentShape = contentItemDisplayManager.BuildDisplayAsync(content, null, "File-" + extension).Result; contentShape.Metadata.OnDisplayed(context => { var doc = new HtmlDocument(); doc.LoadHtml(context.Shape.Metadata.ChildContent.ToString()); //temp disabled bu giannis // var aliasAspect = content.As<IAliasAspect>(); var aliasAspect = content.Content.AutoroutePart; if (aliasAspect != null) { Uri siteUri = new Uri(_urlHelper.GetBaseUrl()); var itemUri = new Uri(siteUri, aliasAspect["Path"]); var links = doc.DocumentNode.SelectNodes("//a[@href]"); if (links != null) // See: https://htmlagilitypack.codeplex.com/workitem/29175 { foreach (var link in links) { var href = link.GetAttributeValue("href", null); if (href != null) { Uri uri; if (Helpers.UrlHelper.UrlIsInternal(itemUri, href, out uri)) { var alias = uri.LocalPath.TrimStart('/'); if (aliases.ContainsKey(alias)) { link.SetAttributeValue("href", "#content-item-top-" + aliases[alias].ContentItemId); } else { link.SetAttributeValue("href", uri.ToString()); } } } } } var srcElements = doc.DocumentNode.SelectNodes("//*[@src]"); if (srcElements != null) { foreach (var element in srcElements) { var src = element.GetAttributeValue("src", null); if (src != null) { Uri uri; if (Helpers.UrlHelper.UrlIsInternal(itemUri, src, out uri)) { element.SetAttributeValue("src", uri.ToString()); } } } } } var stringBuilder = new StringBuilder(); using (var stringWriter = new StringWriter(stringBuilder)) { doc.Save(stringWriter); } context.Shape.Metadata.ChildContent = new HtmlString(stringBuilder.ToString()); }); return(contentShape); }); // var shape = _shapeFactory.File_ContentsWrapper( // Title: metadataitem.DisplayText, // ContentShapes: contentShapes); // var shape = await _shapeFactory.CreateAsync("File_ContentsWrapper", Arguments.From(new // { // Title= metadataitem.DisplayText, // ContentShapes= contentShapes // })); dynamic shape = await _shapeFactory.File_ContentsWrapper( Title : firstContent.DisplayText, ContentShapes : contentShapes ); // shape.Metadata.Alternates.Add("File_ContentsWrapper__" + extension); // shape.Metadata.Alternates.Add("File_ContentsWrapper__" + extension + " __" + firstContent.ContentItem.Id); // shape.Metadata.Alternates.Add("File_ContentsWrapper__" + firstContent.ContentItem.Id); // var result = await _iHtmlDisplay.ExecuteAsync(CreateDisplayContext(shape)); // var displayHelper = _serviceProvider.GetRequiredService<IDisplayHelperFactory>().CreateHelper( _html.ViewContext); // var displayContext =new DisplayContext() // { // // ServiceProvider = page.Context.RequestServices, // DisplayAsync = displayHelper, // ViewContext = _html.ViewContext, // Value = shape // }; //replace with actual shape to render displayContext.Value = shape; // var result = await _iHtmlDisplay.ExecuteAsync(displayContext); var htmlContent = await _displayManager.ExecuteAsync(displayContext); // https://stackoverflow.com/questions/8047064/convert-string-to-system-io-stream // convert string to stream byte[] byteArray = Encoding.UTF8.GetBytes(htmlContent.ToString()); // byte[] byteArray = Encoding.UTF8.GetBytes(WebUtility.HtmlDecode(htmlContent.ToString())); //byte[] byteArray = Encoding.ASCII.GetBytes(contents); MemoryStream stream = new MemoryStream(byteArray); return(stream);// result.ToString(); // return _shapeOutputGenerator.GenerateOutput(shape); }