Skip to content

Commit

Permalink
Feature/blogposts search (#8570)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaPiovanelli committed Jul 8, 2022
1 parent d0bd8d0 commit c38e681
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Orchard.Web/Modules/Orchard.Blogs/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AdminMenu : INavigationProvider {
if (singleBlog != null)
menu.Add(T("New Post"), "1.1",
item =>
item.Action("Create", "BlogPostAdmin", new {area = "Orchard.Blogs", blogId = singleBlog.Id}).Permission(Permissions.MetaListOwnBlogs));
item.Action("Create", "BlogPostAdmin", new { area = "Orchard.Blogs", blogId = singleBlog.Id }).Permission(Permissions.MetaListOwnBlogs));

menu.Add(T("New Blog"), "1.2",
item =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Orchard.Blogs.Services;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Navigation;

namespace Orchard.Blogs {
public class BlogPostsLocalNavigationProvider : INavigationProvider {
private readonly IBlogService _blogService;
private readonly IAuthorizationService _authorizationService;
private readonly IWorkContextAccessor _workContextAccessor;

public BlogPostsLocalNavigationProvider(
IBlogService blogService,
IAuthorizationService authorizationService,
IWorkContextAccessor workContextAccessor) {

T = NullLocalizer.Instance;
_blogService = blogService;
_authorizationService = authorizationService;
_workContextAccessor = workContextAccessor;
}

public Localizer T { get; set; }

public string MenuName {
get { return "blogposts-navigation"; }
}

public void GetNavigation(NavigationBuilder builder) {
var blogId = 0;
int.TryParse(_workContextAccessor.GetContext().HttpContext.Request.RequestContext.RouteData.Values["blogId"]?.ToString(), out blogId);
if (blogId > 0) {
builder.Add(T("Blog posts"),
item => item.Action("Item", "BlogAdmin", new { area = "Orchard.Blogs", blogId })
.LocalNav()
.Permission(Permissions.MetaListOwnBlogs));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Web.Mvc;
using Orchard.Blogs.Extensions;
using Orchard.Blogs.Models;
using Orchard.Blogs.Routing;
using Orchard.Blogs.Services;
using Orchard.ContentManagement;
using Orchard.Data;
Expand All @@ -13,6 +12,7 @@
using Orchard.UI.Navigation;
using Orchard.UI.Notify;
using Orchard.Settings;
using System.Collections.Generic;

namespace Orchard.Blogs.Controllers {

Expand All @@ -21,6 +21,7 @@ public class BlogAdminController : Controller, IUpdateModel {
private readonly IBlogService _blogService;
private readonly IBlogPostService _blogPostService;
private readonly IContentManager _contentManager;
private readonly INavigationManager _navigationManager;
private readonly ITransactionManager _transactionManager;
private readonly ISiteService _siteService;

Expand All @@ -29,13 +30,15 @@ public class BlogAdminController : Controller, IUpdateModel {
IBlogService blogService,
IBlogPostService blogPostService,
IContentManager contentManager,
INavigationManager navigationManager,
ITransactionManager transactionManager,
ISiteService siteService,
IShapeFactory shapeFactory) {
Services = services;
_blogService = blogService;
_blogPostService = blogPostService;
_contentManager = contentManager;
_navigationManager = navigationManager;
_transactionManager = transactionManager;
_siteService = siteService;
T = NullLocalizer.Instance;
Expand Down Expand Up @@ -151,10 +154,10 @@ public class BlogAdminController : Controller, IUpdateModel {
list.AddRange(_blogService.Get(VersionOptions.Latest)
.Where(x => Services.Authorizer.Authorize(Permissions.MetaListOwnBlogs, x))
.Select(b => {
var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin");
blog.TotalPostCount = _blogPostService.PostCount(b, VersionOptions.Latest);
return blog;
}));
var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin");
blog.TotalPostCount = _blogPostService.PostCount(b, VersionOptions.Latest);
return blog;
}));

var viewModel = Services.New.ViewModel()
.ContentItems(list);
Expand All @@ -179,6 +182,18 @@ public class BlogAdminController : Controller, IUpdateModel {

var totalItemCount = _blogPostService.PostCount(blogPart, VersionOptions.Latest);
blog.Content.Add(Shape.Pager(pager).TotalItemCount(totalItemCount), "Content:after");
// Adds LocalMenus;
var menuItems = _navigationManager.BuildMenu("blogposts-navigation");
var request = Services.WorkContext.HttpContext.Request;

// Set the currently selected path
Stack<MenuItem> selectedPath = NavigationHelper.SetSelectedPath(menuItems, request, request.RequestContext.RouteData);

// Populate local nav
dynamic localMenuShape = Shape.LocalMenu().MenuName("local-admin");
// NavigationHelper.PopulateLocalMenu(Shape, localMenuShape, localMenuShape, selectedPath);
NavigationHelper.PopulateLocalMenu(Shape, localMenuShape, localMenuShape, menuItems);
Services.WorkContext.Layout.LocalNavigation.Add(localMenuShape);

return View(blog);
}
Expand Down
1 change: 1 addition & 0 deletions src/Orchard.Web/Modules/Orchard.Blogs/Orchard.Blogs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="BlogPostLocalNavigationProvider.cs" />
<Compile Include="Commands\BlogWidgetCommands.cs" />
<Compile Include="Controllers\RemoteBlogPublishingController.cs" />
<Compile Include="Drivers\BlogArchivesPartDriver.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
Layout.Title = T("Manage Blog").ToString();
}
@* Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type *@
@Display(Model)
@* Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type *@
@Display(Model)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using Orchard.Blogs;
using Orchard.Blogs.Services;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Navigation;

namespace Orchard.Search {
[OrchardFeature("Orchard.Search.Blogs")]
public class BlogPostsLocalNavigationProvider : INavigationProvider {
private readonly IBlogService _blogService;
private readonly IAuthorizationService _authorizationService;
private readonly IWorkContextAccessor _workContextAccessor;

public BlogPostsLocalNavigationProvider(
IBlogService blogService,
IAuthorizationService authorizationService,
IWorkContextAccessor workContextAccessor) {

T = NullLocalizer.Instance;
_blogService = blogService;
_authorizationService = authorizationService;
_workContextAccessor = workContextAccessor;
}

public Localizer T { get; set; }

public string MenuName {
get { return "blogposts-navigation"; }
}
public void GetNavigation(NavigationBuilder builder) {
var blogId = 0;
int.TryParse(_workContextAccessor.GetContext().HttpContext.Request.RequestContext.RouteData.Values["blogId"]?.ToString(), out blogId);
if (blogId > 0) {
builder.Add(T("Search Posts"), "2.0", item => item.Action("Index", "BlogSearch", new { area = "Orchard.Search", blogId })
.LocalNav()
.Permission(Permissions.MetaListOwnBlogs));
}
}
}
}
12 changes: 12 additions & 0 deletions src/Orchard.Web/Modules/Orchard.Search/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Environment.Extensions;

namespace Orchard.Search {
[OrchardFeature("Orchard.Search.Blogs")]
public class BlogSearchConstants {
public static string ADMIN_BLOGPOSTS_INDEX = "AdminBlogPosts";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orchard.Collections;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.DisplayManagement;
using Orchard.Environment.Extensions;
using Orchard.Indexing;
using Orchard.Localization;
using Orchard.Localization.Services;
using Orchard.Logging;
using Orchard.Search.Helpers;
using Orchard.Search.Models;
using Orchard.Search.Services;
using Orchard.Security;
using Orchard.Settings;
using Orchard.UI.Admin;
using Orchard.UI.Navigation;
using Orchard.UI.Notify;

namespace Orchard.Search.Controllers {
[OrchardFeature("Orchard.Search.Blogs")]
[Admin]
public class BlogSearchController : Controller {
private readonly ISearchService _searchService;
private readonly ISiteService _siteService;
private readonly IIndexManager _indexManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IContentManager _contentManager;
private readonly IAuthorizer _authorizer;
private readonly ICultureManager _cultureManager;
private readonly INavigationManager _navigationManager;

public BlogSearchController(
IOrchardServices orchardServices,
ISearchService searchService,
ISiteService siteService,
IIndexManager indexManager,
IContentDefinitionManager contentDefinitionManager,
IContentManager contentManager,
IAuthorizer authorizer,
ICultureManager cultureManager,
INavigationManager navigationManager,
IShapeFactory shapeFactory) {

_searchService = searchService;
_siteService = siteService;
Services = orchardServices;
_indexManager = indexManager;
_contentDefinitionManager = contentDefinitionManager;
_contentManager = contentManager;
_authorizer = authorizer;
_cultureManager = cultureManager;
_navigationManager = navigationManager;
Shape = shapeFactory;
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}

public IOrchardServices Services { get; set; }
public ILogger Logger { get; set; }
public Localizer T { get; set; }
public dynamic Shape { get; set; }

public ActionResult Index(int blogId, PagerParameters pagerParameters, string searchText = "") {
var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
var searchSettingsPart = Services.WorkContext.CurrentSite.As<SearchSettingsPart>();

IPageOfItems<ISearchHit> searchHits = new PageOfItems<ISearchHit>(new ISearchHit[] { });
try {
if (!string.IsNullOrWhiteSpace(searchText)) {
var searchableTypes = new List<string>();
// add the type to the list of types we will filter for
// BlogPost for now but we would add more types in the future (i.e. "Article")
searchableTypes.Add("BlogPost");
var searchBuilder = _indexManager.HasIndexProvider()
? _indexManager
.GetSearchIndexProvider()
.CreateSearchBuilder(BlogSearchConstants.ADMIN_BLOGPOSTS_INDEX)
: new NullSearchBuilder();

searchBuilder
.Parse(searchSettingsPart
.GetSearchFields(BlogSearchConstants.ADMIN_BLOGPOSTS_INDEX),
searchText);

// filter by Blog
searchBuilder
.WithField("container-id", blogId)
.Mandatory()
.NotAnalyzed()
.AsFilter();

foreach (var searchableType in searchableTypes) {
// filter by type
searchBuilder
.WithField("type", searchableType)
.NotAnalyzed()
.AsFilter();
}
// pagination
var totalCount = searchBuilder.Count();
if (pager != null) {
searchBuilder = searchBuilder
.Slice(
(pager.Page > 0 ? pager.Page - 1 : 0) * pager.PageSize,
pager.PageSize);
}
// search
var searchResults = searchBuilder.Search();
// prepare the shape for the page
searchHits = new PageOfItems<ISearchHit>(searchResults.Select(searchHit => searchHit)) {
PageNumber = pager != null ? pager.Page : 0,
PageSize = pager != null ? (pager.PageSize != 0 ? pager.PageSize : totalCount) : totalCount,
TotalItemCount = totalCount
};
}

}
catch (Exception exception) {
Logger.Error(T("Invalid search query: {0}", exception.Message).Text);
Services.Notifier.Error(T("Invalid search query: {0}", exception.Message));
}

var list = Services.New.List();
foreach (var contentItem in Services.ContentManager.GetMany<IContent>(searchHits.Select(x => x.ContentItemId), VersionOptions.Latest, QueryHints.Empty)) {
// ignore search results which content item has been removed
if (contentItem == null) {
searchHits.TotalItemCount--;
continue;
}

list.Add(Services.ContentManager.BuildDisplay(contentItem, "SummaryAdmin"));
}

var pagerShape = Services.New.Pager(pager).TotalItemCount(searchHits.TotalItemCount);

var viewModel = Services.New.ViewModel()
.ContentItems(list)
.Pager(pagerShape)
.SearchText(searchText)
.BlogId(blogId);

// Adds LocalMenus;
var menuItems = _navigationManager.BuildMenu("blogposts-navigation");
var request = Services.WorkContext.HttpContext.Request;

// Set the currently selected path
Stack<MenuItem> selectedPath = NavigationHelper.SetSelectedPath(menuItems, request, request.RequestContext.RouteData);

// Populate local nav
dynamic localMenuShape = Shape.LocalMenu().MenuName("local-admin");

NavigationHelper.PopulateLocalMenu(Shape, localMenuShape, localMenuShape, menuItems);
Services.WorkContext.Layout.LocalNavigation.Add(localMenuShape);

return View(viewModel);
}
}
}
19 changes: 19 additions & 0 deletions src/Orchard.Web/Modules/Orchard.Search/Migrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ public class AdminSearchMigration : DataMigrationImpl {
return 1;
}
}

[OrchardFeature("Orchard.Search.Blogs")]
public class BlogsMigration : DataMigrationImpl {
private readonly IIndexManager _indexManager;

public BlogsMigration(IIndexManager indexManager) {
_indexManager = indexManager;
}

public int Create() {

_indexManager.GetSearchIndexProvider().CreateIndex(BlogSearchConstants.ADMIN_BLOGPOSTS_INDEX);

ContentDefinitionManager.AlterTypeDefinition("BlogPost", cfg => cfg.WithSetting("TypeIndexing.Indexes", BlogSearchConstants.ADMIN_BLOGPOSTS_INDEX + ":latest"));

return 1;
}
}

}
5 changes: 5 additions & 0 deletions src/Orchard.Web/Modules/Orchard.Search/Module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ Features:
Name: Media Library Search
Description: Provides search menu item in the Media Library explorer.
Dependencies: Orchard.MediaLibrary, Orchard.Search
Category: Search
Orchard.Search.Blogs:
Name: Blog posts Search
Description: Provides search menu item in the Blog section.
Dependencies: Orchard.Blogs, Orchard.Search
Category: Search

0 comments on commit c38e681

Please sign in to comment.