Skip to content

Commit

Permalink
Revert "If in design or preview mode pull latest article/product to p…
Browse files Browse the repository at this point in the history
…review else redirect to latest article/product page. #488"

This reverts commit 2d33fb5.
  • Loading branch information
SeriaWei committed Apr 22, 2023
1 parent 2d33fb5 commit 19c7f2d
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 57 deletions.
30 changes: 13 additions & 17 deletions src/ZKEACMS.Article/Service/ArticleDetailWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,32 @@ 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;
}
}
}
else
if (viewModel.Current == null && ApplicationContext.IsAuthenticated)
{
var latest = _articleService.GetLatestPublished();
if (ApplicationContext.CurrentAppContext().IsDesignMode || ApplicationContext.CurrentAppContext().IsPreViewMode)
{
viewModel.Current = latest;
}
else
foreach (var item in _articleService.Get().AsQueryable().OrderByDescending(m => m.ID).Take(1))
{
actionContext.RedirectTo($"{actionContext.RouteData.GetPath()}/{latest.Url}.html", false);
return null;
viewModel.Current = item;
}
}

if (viewModel.Current == null)
{
actionContext.NotFoundResult();
return null;
}

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

namespace ZKEACMS.Article.Service
{
Expand Down Expand Up @@ -119,10 +118,5 @@ 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: 14 additions & 16 deletions src/ZKEACMS.Product/Service/ProductDetailWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,32 @@ 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;
}
}
else
if (product == null && ApplicationContext.IsAuthenticated)
{
if (ApplicationContext.CurrentAppContext().IsDesignMode || ApplicationContext.CurrentAppContext().IsPreViewMode)
product = _productService.Get().OrderByDescending(m => m.ID).FirstOrDefault();
if (product != null)
{
product = _productService.GetLatestPublished();
}
else
{
actionContext.RedirectTo($"{actionContext.RouteData.GetPath()}/{_productService.GetLatestPublished().Url}.html", false);
return null;
product = _productService.Get(product.ID);
}
}
if (product == null)
{
actionContext.NotFoundResult();
return null;
}

var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
if (product != 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;

return product ?? new ProductEntity();
}

private void AddStructuredDataToPageHeader(ProductEntity product)
Expand All @@ -109,7 +107,7 @@ private void AddStructuredDataToPageHeader(ProductEntity product)
MPN = product.PartNumber
};
IHtmlContent jsonLinkingData = HtmlHelper.SerializeToJsonLinkingData(structuredData);
ApplicationContext.CurrentAppContext().HeaderPart.Add(jsonLinkingData);
ApplicationContext.As<CMSApplicationContext>().HeaderPart.Add(jsonLinkingData);
}
private IEnumerable<string> GetImages(ProductEntity product)
{
Expand Down
5 changes: 0 additions & 5 deletions src/ZKEACMS.Product/Service/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,5 @@ 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>().CurrentAppContext());
return current ?? (current = _serviceProvider.GetService<IApplicationContext>().As<CMSApplicationContext>());
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/ZKEACMS/Article/Service/IArticleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ public interface IArticleService : IService<ArticleEntity>
ArticleEntity GetPrev(ArticleEntity article);
ArticleEntity GetNext(ArticleEntity article);
ArticleEntity GetByUrl(string url);
ArticleEntity GetLatestPublished();
}
}
1 change: 0 additions & 1 deletion src/ZKEACMS/CMSApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ 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.CurrentAppContext().HeaderPart.Add(htmlContentBuilder);
ApplicationContext.As<CMSApplicationContext>().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.CurrentAppContext().IsDesignMode)
if (ApplicationContext.As<CMSApplicationContext>().IsDesignMode)
{
var layout = widgetDisplayContext.PageLayout;
if (layout != null && layout.Page != null)
Expand Down
8 changes: 2 additions & 6 deletions src/ZKEACMS/Extend/ApplicationContextExtend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ public static class ApplicationContextExtend
{
public static T GetService<T>(this IApplicationContext applicationContext)
{
return applicationContext.CurrentAppContext().HttpContextAccessor.HttpContext.RequestServices.GetService<T>();
return applicationContext.As<CMSApplicationContext>().HttpContextAccessor.HttpContext.RequestServices.GetService<T>();
}
public static IEnumerable<T> GetServices<T>(this IApplicationContext applicationContext)
{
return applicationContext.CurrentAppContext().HttpContextAccessor.HttpContext.RequestServices.GetServices<T>();
}
public static CMSApplicationContext CurrentAppContext(this IApplicationContext applicationContext)
{
return applicationContext.As<CMSApplicationContext>();
return applicationContext.As<CMSApplicationContext>().HttpContextAccessor.HttpContext.RequestServices.GetServices<T>();
}
}
}
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.CurrentAppContext();
return page.ApplicationContext.As<CMSApplicationContext>();
}
}
}
1 change: 0 additions & 1 deletion src/ZKEACMS/Product/Service/IProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ public interface IProductService : IService<ProductEntity>
void Publish(int ID);
void Publish(ProductEntity product);
ProductEntity GetByUrl(string url);
ProductEntity GetLatestPublished();
}
}

0 comments on commit 19c7f2d

Please sign in to comment.