Skip to content

Commit

Permalink
nopSolutions#2364 Add preview button for News, Topics, Blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyKulagin authored and DmitriyKulagin committed Apr 16, 2018
1 parent c1f4fcc commit 48e53c1
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 34 deletions.
35 changes: 35 additions & 0 deletions src/Libraries/Nop.Core/Domain/Blogs/BlogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,42 @@ public static string[] ParseTags(this BlogPost blogPost)
parsedTags.Add(tmp);
}
}

return parsedTags.ToArray();
}

/// <summary>
/// Get a value indicating whether a blog post is available now (availability dates)
/// </summary>
/// <param name="blogPost">Blog post</param>
/// <returns>Result</returns>
public static bool IsAvailable(this BlogPost blogPost)
{
return IsAvailable(blogPost, DateTime.UtcNow);
}

/// <summary>
/// Get a value indicating whether a blog post is available now (availability dates)
/// </summary>
/// <param name="blogPost">Blog post</param>
/// <param name="dateTime">Datetime to check</param>
/// <returns>Result</returns>
public static bool IsAvailable(this BlogPost blogPost, DateTime dateTime)
{
if (blogPost == null)
throw new ArgumentNullException(nameof(blogPost));

if (blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= dateTime)
{
return false;
}

if (blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= dateTime)
{
return false;
}

return true;
}
}
}
44 changes: 44 additions & 0 deletions src/Libraries/Nop.Core/Domain/News/NewsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;

namespace Nop.Core.Domain.News
{
/// <summary>
/// News item extensions
/// </summary>
public static class NewsExtensions
{
/// <summary>
/// Get a value indicating whether a news item is available now (availability dates)
/// </summary>
/// <param name="newsItem">News item</param>
/// <returns>Result</returns>
public static bool IsAvailable(this NewsItem newsItem)
{
return IsAvailable(newsItem, DateTime.UtcNow);
}

/// <summary>
/// Get a value indicating whether a news item is available now (availability dates)
/// </summary>
/// <param name="newsItem">News item</param>
/// <param name="dateTime">Datetime to check</param>
/// <returns>Result</returns>
public static bool IsAvailable(this NewsItem newsItem, DateTime dateTime)
{
if (newsItem == null)
throw new ArgumentNullException(nameof(newsItem));

if (newsItem.StartDateUtc.HasValue && newsItem.StartDateUtc.Value >= dateTime)
{
return false;
}

if (newsItem.EndDateUtc.HasValue && newsItem.EndDateUtc.Value <= dateTime)
{
return false;
}

return true;
}
}
}
4 changes: 4 additions & 0 deletions src/Presentation/Nop.Web/Areas/Admin/Views/Blog/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
</small>
</h1>
<div class="pull-right">
<button type="button" onclick="javascript:OpenWindow('@Url.RouteUrl("BlogPost", new {SeName = Model.SeName})', 800, 600, true); return false;" class="btn bg-purple">
<i class="fa fa-eye"></i>
@T("Admin.Common.Preview")
</button>
<button type="submit" name="save" class="btn bg-blue">
<i class="fa fa-floppy-o"></i>
@T("Admin.Common.Save")
Expand Down
6 changes: 5 additions & 1 deletion src/Presentation/Nop.Web/Areas/Admin/Views/News/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
<a asp-action="List">@T("Admin.ContentManagement.News.NewsItems.BackToList")</a>
</small>
</h1>
<div class="pull-right">
<div class="pull-right">
<button type="button" onclick="javascript:OpenWindow('@Url.RouteUrl("NewsItem", new {SeName = Model.SeName})', 800, 600, true); return false;" class="btn bg-purple">
<i class="fa fa-eye"></i>
@T("Admin.Common.Preview")
</button>
<button type="submit" name="save" class="btn bg-blue">
<i class="fa fa-floppy-o"></i>
@T("Admin.Common.Save")
Expand Down
4 changes: 4 additions & 0 deletions src/Presentation/Nop.Web/Areas/Admin/Views/Topic/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
</small>
</h1>
<div class="pull-right">
<button type="button" onclick="javascript:OpenWindow('@Url.RouteUrl("Topic", new {SeName = Model.SeName})', 800, 600, true); return false;" class="btn bg-purple">
<i class="fa fa-eye"></i>
@T("Admin.Common.Preview")
</button>
<button type="submit" name="save" class="btn bg-blue">
<i class="fa fa-floppy-o"></i>
@T("Admin.Common.Save")
Expand Down
11 changes: 7 additions & 4 deletions src/Presentation/Nop.Web/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,20 @@ public virtual IActionResult BlogPost(int blogPostId)
return RedirectToRoute("HomePage");

var blogPost = _blogService.GetBlogPostById(blogPostId);
if (blogPost == null ||
(blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= DateTime.UtcNow) ||
(blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= DateTime.UtcNow))
if (blogPost == null)
return RedirectToRoute("HomePage");

var hasAdminAccess = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageBlog);
//access to Blog preview
if (!blogPost.IsAvailable() && !hasAdminAccess)
return RedirectToRoute("HomePage");

//Store mapping
if (!_storeMappingService.Authorize(blogPost))
return InvokeHttp404();

//display "edit" (manage) link
if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageBlog))
if (hasAdminAccess)
DisplayEditLink(Url.Action("Edit", "Blog", new { id = blogPost.Id, area = AreaNames.Admin }));

var model = new BlogPostModel();
Expand Down
22 changes: 10 additions & 12 deletions src/Presentation/Nop.Web/Controllers/NewsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public partial class NewsController : BasePublicController
private readonly ICustomerActivityService _customerActivityService;
private readonly IStoreMappingService _storeMappingService;
private readonly IPermissionService _permissionService;
private readonly IEventPublisher _eventPublisher;

private readonly IEventPublisher _eventPublisher;
private readonly NewsSettings _newsSettings;
private readonly LocalizationSettings _localizationSettings;
private readonly CaptchaSettings _captchaSettings;
Expand All @@ -60,7 +59,7 @@ public NewsController(INewsModelFactory newsModelFactory,
ICustomerActivityService customerActivityService,
IStoreMappingService storeMappingService,
IPermissionService permissionService,
IEventPublisher eventPublisher,
IEventPublisher eventPublisher,
NewsSettings newsSettings,
LocalizationSettings localizationSettings,
CaptchaSettings captchaSettings)
Expand All @@ -76,7 +75,6 @@ public NewsController(INewsModelFactory newsModelFactory,
this._storeMappingService = storeMappingService;
this._permissionService = permissionService;
this._eventPublisher = eventPublisher;

this._newsSettings = newsSettings;
this._localizationSettings = localizationSettings;
this._captchaSettings = captchaSettings;
Expand Down Expand Up @@ -116,26 +114,26 @@ public virtual IActionResult ListRss(int languageId)
feed.Items = items;
return new RssActionResult(feed, _webHelper.GetThisPageUrl(false));
}

public virtual IActionResult NewsItem(int newsItemId)
{
if (!_newsSettings.Enabled)
return RedirectToRoute("HomePage");

var newsItem = _newsService.GetNewsById(newsItemId);
if (newsItem == null ||
!newsItem.Published ||
(newsItem.StartDateUtc.HasValue && newsItem.StartDateUtc.Value >= DateTime.UtcNow) ||
(newsItem.EndDateUtc.HasValue && newsItem.EndDateUtc.Value <= DateTime.UtcNow) ||
//Store mapping
!_storeMappingService.Authorize(newsItem))
if (newsItem == null)
return RedirectToRoute("HomePage");

var hasAdminAccess = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageNews);
//access to News preview
if ((!newsItem.Published || !newsItem.IsAvailable()) && !hasAdminAccess)
return RedirectToRoute("HomePage");

var model = new NewsItemModel();
model = _newsModelFactory.PrepareNewsItemModel(model, newsItem, true);

//display "edit" (manage) link
if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageNews))
if (hasAdminAccess)
DisplayEditLink(Url.Action("Edit", "News", new { id = newsItem.Id, area = AreaNames.Admin }));

return View(model);
Expand Down
15 changes: 9 additions & 6 deletions src/Presentation/Nop.Web/Controllers/TopicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ public TopicController(ITopicModelFactory topicModelFactory,
}

#endregion

#region Methods

[HttpsRequirement(SslRequirement.No)]
public virtual IActionResult TopicDetails(int topicId)
{
var model = _topicModelFactory.PrepareTopicModelById(topicId);
if (model == null)
var hasAdminAccess = _permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageTopics);
//access to Topics preview
if (model == null || (!model.Published && !hasAdminAccess))
return RedirectToRoute("HomePage");

//display "edit" (manage) link
if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageTopics))
if (hasAdminAccess)
DisplayEditLink(Url.Action("Edit", "Topic", new { id = model.Id, area = AreaNames.Admin }));

//template
Expand Down Expand Up @@ -103,9 +105,10 @@ public virtual IActionResult Authenticate(int id, string password)
error = _localizationService.GetResource("Topic.WrongPassword");
}
}

return Json(new { Authenticated = authResult, Title = title, Body = body, Error = error });
}

#endregion
}
}
14 changes: 4 additions & 10 deletions src/Presentation/Nop.Web/Factories/TopicModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ protected virtual TopicModel PrepareTopicModel(Topic topic)
MetaDescription = topic.GetLocalized(x => x.MetaDescription),
MetaTitle = topic.GetLocalized(x => x.MetaTitle),
SeName = topic.GetSeName(),
TopicTemplateId = topic.TopicTemplateId
TopicTemplateId = topic.TopicTemplateId,
Published = topic.Published
};
return model;
}
Expand All @@ -100,16 +101,9 @@ public virtual TopicModel PrepareTopicModelById(int topicId)
var cachedModel = _cacheManager.Get(cacheKey, () =>
{
var topic = _topicService.GetTopicById(topicId);
if (topic == null)
return null;
if (!topic.Published)
return null;
//Store mapping
if (!_storeMappingService.Authorize(topic))
return null;
//ACL (access control list)
if (!_aclService.Authorize(topic))
return null;
if (topic == null || !_aclService.Authorize(topic))
return null;
return PrepareTopicModel(topic);
});

Expand Down
2 changes: 2 additions & 0 deletions src/Presentation/Nop.Web/Models/Topics/TopicModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public partial class TopicModel : BaseNopEntityModel
public string SeName { get; set; }

public int TopicTemplateId { get; set; }

public bool Published { get; set; }
}
}
69 changes: 68 additions & 1 deletion src/Tests/Nop.Core.Tests/Domain/Blogs/BlogPostTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nop.Core.Domain.Blogs;
using System;
using Nop.Core.Domain.Blogs;
using Nop.Tests;
using NUnit.Framework;

Expand All @@ -21,5 +22,71 @@ public void Can_parse_tags()
tags[1].ShouldEqual("tag2");
tags[2].ShouldEqual("tag 3 4");
}

[Test]
public void Should_be_available_when_startdate_is_not_set()
{
var blogPost = new BlogPost
{
StartDateUtc = null
};

blogPost.IsAvailable(new DateTime(2010, 01, 03)).ShouldEqual(true);
}

[Test]
public void Should_be_available_when_startdate_is_less_than_somedate()
{
var blogPost = new BlogPost
{
StartDateUtc = new DateTime(2010, 01, 02)
};

blogPost.IsAvailable(new DateTime(2010, 01, 03)).ShouldEqual(true);
}

[Test]
public void Should_not_be_available_when_startdate_is_greater_than_somedate()
{
var blogPost = new BlogPost
{
StartDateUtc = new DateTime(2010, 01, 02)
};

blogPost.IsAvailable(new DateTime(2010, 01, 01)).ShouldEqual(false);
}

[Test]
public void Should_be_available_when_enddate_is_not_set()
{
var blogPost = new BlogPost
{
EndDateUtc = null
};

blogPost.IsAvailable(new DateTime(2010, 01, 03)).ShouldEqual(true);
}

[Test]
public void Should_be_available_when_enddate_is_greater_than_somedate()
{
var blogPost = new BlogPost
{
EndDateUtc = new DateTime(2010, 01, 02)
};

blogPost.IsAvailable(new DateTime(2010, 01, 01)).ShouldEqual(true);
}

[Test]
public void Should_not_be_available_when_enddate_is_less_than_somedate()
{
var blogPost = new BlogPost
{
EndDateUtc = new DateTime(2010, 01, 02)
};

blogPost.IsAvailable(new DateTime(2010, 01, 03)).ShouldEqual(false);
}
}
}
Loading

0 comments on commit 48e53c1

Please sign in to comment.