Skip to content

Commit

Permalink
If in design or preview mode pull latest article/product to preview e…
Browse files Browse the repository at this point in the history
…lse redirect to latest article/product page. #488
  • Loading branch information
SeriaWei committed Apr 22, 2023
1 parent 9f025a1 commit 2d33fb5
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 33 deletions.
30 changes: 17 additions & 13 deletions src/ZKEACMS.Article/Service/ArticleDetailWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,36 @@ public override object Display(WidgetDisplayContext widgetDisplayContext)
if (viewModel.Current.Url.IsNotNullAndWhiteSpace() && actionContext.RouteData.GetArticleUrl().IsNullOrWhiteSpace())
{
actionContext.RedirectTo($"{actionContext.RouteData.GetPath()}/{viewModel.Current.Url}.html", true);
return null;
}
}
}
if (viewModel.Current == null && ApplicationContext.IsAuthenticated)
else
{
foreach (var item in _articleService.Get().AsQueryable().OrderByDescending(m => m.ID).Take(1))
var latest = _articleService.GetLatestPublished();
if (ApplicationContext.CurrentAppContext().IsDesignMode || ApplicationContext.CurrentAppContext().IsPreViewMode)
{
viewModel.Current = latest;
}
else
{
viewModel.Current = item;
actionContext.RedirectTo($"{actionContext.RouteData.GetPath()}/{latest.Url}.html", false);
return null;
}
}

if (viewModel.Current == null)
{
actionContext.NotFoundResult();
return null;
}
else
{
var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
{
layout.Page.ConfigSEO(viewModel.Current.Title, viewModel.Current.MetaKeyWords, viewModel.Current.MetaDescription);
}

var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
{
layout.Page.ConfigSEO(viewModel.Current.Title, viewModel.Current.MetaKeyWords, viewModel.Current.MetaDescription);
AddStructuredDataToPageHeader(viewModel.Current);
}


return viewModel;
}
private void AddStructuredDataToPageHeader(ArticleEntity article)
Expand All @@ -124,7 +128,7 @@ private void AddStructuredDataToPageHeader(ArticleEntity article)
Author = new Person[] { new Person { Name = article.CreatebyName } }
};
IHtmlContent jsonLinkingData = HtmlHelper.SerializeToJsonLinkingData(structuredData);
ApplicationContext.As<CMSApplicationContext>().HeaderPart.Add(jsonLinkingData);
ApplicationContext.CurrentAppContext().HeaderPart.Add(jsonLinkingData);
}
private IEnumerable<string> GetImages(ArticleEntity article)
{
Expand Down
6 changes: 6 additions & 0 deletions src/ZKEACMS.Article/Service/ArticleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using Easy.Extend;
using ZKEACMS.Event;
using Easy.Constant;

namespace ZKEACMS.Article.Service
{
Expand Down Expand Up @@ -118,5 +119,10 @@ public void Publish(ArticleEntity article)
_eventManager.Trigger(Events.OnArticlePublished, article);
}
}

public ArticleEntity GetLatestPublished()
{
return Get().Where(m => m.Status == (int)RecordStatus.Active && m.IsPublish).OrderByDescending(m => m.ID).Take(1).ToList().FirstOrDefault();
}
}
}
30 changes: 16 additions & 14 deletions src/ZKEACMS.Product/Service/ProductDetailWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,34 @@ public override object Display(WidgetDisplayContext widgetDisplayContext)
if (product != null && product.Url.IsNotNullAndWhiteSpace() && actionContext.RouteData.GetProductUrl().IsNullOrWhiteSpace())
{
actionContext.RedirectTo($"{actionContext.RouteData.GetPath()}/{product.Url}.html", true);
return null;
}
}
if (product == null && ApplicationContext.IsAuthenticated)
else
{
product = _productService.Get().OrderByDescending(m => m.ID).FirstOrDefault();
if (product != null)
if (ApplicationContext.CurrentAppContext().IsDesignMode || ApplicationContext.CurrentAppContext().IsPreViewMode)
{
product = _productService.Get(product.ID);
product = _productService.GetLatestPublished();
}
else
{
actionContext.RedirectTo($"{actionContext.RouteData.GetPath()}/{_productService.GetLatestPublished().Url}.html", false);
return null;
}
}
if (product == null)
{
actionContext.NotFoundResult();
return null;
}
if (product != null)
{
var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
{
layout.Page.ConfigSEO(product.SEOTitle ?? product.Title, product.SEOKeyWord, product.SEODescription);
}

var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
{
layout.Page.ConfigSEO(product.SEOTitle ?? product.Title, product.SEOKeyWord, product.SEODescription);
AddStructuredDataToPageHeader(product);
}

return product ?? new ProductEntity();
return product;
}

private void AddStructuredDataToPageHeader(ProductEntity product)
Expand All @@ -107,7 +109,7 @@ private void AddStructuredDataToPageHeader(ProductEntity product)
MPN = product.PartNumber
};
IHtmlContent jsonLinkingData = HtmlHelper.SerializeToJsonLinkingData(structuredData);
ApplicationContext.As<CMSApplicationContext>().HeaderPart.Add(jsonLinkingData);
ApplicationContext.CurrentAppContext().HeaderPart.Add(jsonLinkingData);
}
private IEnumerable<string> GetImages(ProductEntity product)
{
Expand Down
5 changes: 5 additions & 0 deletions src/ZKEACMS.Product/Service/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,10 @@ public void Publish(ProductEntity product)
}
_eventManager.Trigger(Events.OnProductPublished, product);
}

public ProductEntity GetLatestPublished()
{
return Get().Where(m => m.Status == (int)RecordStatus.Active && m.IsPublish).OrderByDescending(m => m.ID).Take(1).ToList().FirstOrDefault();
}
}
}
2 changes: 1 addition & 1 deletion src/ZKEACMS/ApplicationContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CMSApplicationContext Current
{
get
{
return current ?? (current = _serviceProvider.GetService<IApplicationContext>().As<CMSApplicationContext>());
return current ?? (current = _serviceProvider.GetService<IApplicationContext>().CurrentAppContext());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ZKEACMS/Article/Service/IArticleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface IArticleService : IService<ArticleEntity>
ArticleEntity GetPrev(ArticleEntity article);
ArticleEntity GetNext(ArticleEntity article);
ArticleEntity GetByUrl(string url);
ArticleEntity GetLatestPublished();
}
}
1 change: 1 addition & 0 deletions src/ZKEACMS/CMSApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public string MapPath(string path)

public PageViewMode PageMode { get; set; }
public bool IsDesignMode { get { return PageMode == PageViewMode.Design; } }
public bool IsPreViewMode { get { return PageMode == PageViewMode.PreView; } }
/// <summary>
/// Append to html/head
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS/Common/Service/HeadWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override object Display(WidgetDisplayContext widgetDisplayContext)
htmlContentBuilder.AppendHtmlLine("<!-- head:{0} -->".FormatWith(widgetDisplayContext.Widget.ID));
htmlContentBuilder.AppendHtmlLine((widgetDisplayContext.Widget as HeadWidget).Content);
htmlContentBuilder.AppendHtmlLine("<!-- end -->");
ApplicationContext.As<CMSApplicationContext>().HeaderPart.Add(htmlContentBuilder);
ApplicationContext.CurrentAppContext().HeaderPart.Add(htmlContentBuilder);
return base.Display(widgetDisplayContext);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS/Common/Service/NavigationWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override object Display(WidgetDisplayContext widgetDisplayContext)

string path = null;
IUrlHelper urlHelper = null;
if (ApplicationContext.As<CMSApplicationContext>().IsDesignMode)
if (ApplicationContext.CurrentAppContext().IsDesignMode)
{
var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
Expand Down
8 changes: 6 additions & 2 deletions src/ZKEACMS/Extend/ApplicationContextExtend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ public static class ApplicationContextExtend
{
public static T GetService<T>(this IApplicationContext applicationContext)
{
return applicationContext.As<CMSApplicationContext>().HttpContextAccessor.HttpContext.RequestServices.GetService<T>();
return applicationContext.CurrentAppContext().HttpContextAccessor.HttpContext.RequestServices.GetService<T>();
}
public static IEnumerable<T> GetServices<T>(this IApplicationContext applicationContext)
{
return applicationContext.As<CMSApplicationContext>().HttpContextAccessor.HttpContext.RequestServices.GetServices<T>();
return applicationContext.CurrentAppContext().HttpContextAccessor.HttpContext.RequestServices.GetServices<T>();
}
public static CMSApplicationContext CurrentAppContext(this IApplicationContext applicationContext)
{
return applicationContext.As<CMSApplicationContext>();
}
}
}
2 changes: 1 addition & 1 deletion src/ZKEACMS/Extend/RazorPageExtend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class RazorPageExtend
{
public static CMSApplicationContext WorkContext<T>(this Easy.Mvc.RazorPages.EasyRazorPage<T> page)
{
return page.ApplicationContext.As<CMSApplicationContext>();
return page.ApplicationContext.CurrentAppContext();
}
}
}
1 change: 1 addition & 0 deletions src/ZKEACMS/Product/Service/IProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface IProductService : IService<ProductEntity>
void Publish(int ID);
void Publish(ProductEntity product);
ProductEntity GetByUrl(string url);
ProductEntity GetLatestPublished();
}
}

0 comments on commit 2d33fb5

Please sign in to comment.