[Fixes #7339] Orchard.Widgets - Refactor #7251 (#7342)

Fixes #7339
This commit is contained in:
Matthew Harris
2016-10-27 20:24:30 +01:00
committed by Sébastien Ros
parent 7a16cf527c
commit d91aa41be3
6 changed files with 170 additions and 197 deletions

View File

@@ -1,36 +1,24 @@
using System;
using System.Linq;
using Orchard.Blogs.Models;
using Orchard.Blogs.Services;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;
namespace Orchard.Blogs.Commands {
public class BlogWidgetCommands : DefaultOrchardCommandHandler {
private readonly IWidgetsService _widgetsService;
private readonly IWidgetCommandsService _widgetCommandsService;
private readonly IBlogService _blogService;
private readonly ISiteService _siteService;
private readonly IMembershipService _membershipService;
private readonly IContentManager _contentManager;
private BlogPart blog;
public BlogWidgetCommands(
IWidgetsService widgetsService,
IWidgetCommandsService widgetCommandsService,
IBlogService blogService,
ISiteService siteService,
IMembershipService membershipService,
IContentManager contentManager) {
_widgetsService = widgetsService;
_widgetCommandsService = widgetCommandsService;
_blogService = blogService;
_siteService = siteService;
_membershipService = membershipService;
_contentManager = contentManager;
RenderTitle = true;
@@ -75,11 +63,21 @@ namespace Orchard.Blogs.Commands {
public void CreateRecentBlogPostsWidget() {
var type = "RecentBlogPosts";
var widget = CreateStandardWidget(type);
// Check any custom parameters that could cause creating the widget to fail.
blog = GetBlog(BlogId, BlogPath);
if (blog == null) {
Context.Output.WriteLine(T("Creating {0} widget failed: blog was not found.", type));
return;
}
// Create the widget using the standard parameters.
var widget = _widgetCommandsService.CreateBaseWidget(
Context, type, Title, Name, Zone, Position, Layer, Identity, RenderTitle, Owner, null, false, null);
if (widget == null) {
return;
}
// Publish the successfully created widget.
widget.As<RecentBlogPostsPart>().BlogId = blog.Id;
// Setting count to 0 means all posts. It's an optional parameter and defaults to 5.
@@ -90,7 +88,8 @@ namespace Orchard.Blogs.Commands {
}
}
_contentManager.Publish(widget.ContentItem);
// Publish the successfully created widget.
_widgetCommandsService.Publish(widget);
Context.Output.WriteLine(T("{0} widget created successfully.", type).Text);
}
@@ -100,64 +99,28 @@ namespace Orchard.Blogs.Commands {
public void CreateBlogArchivesWidget() {
var type = "BlogArchives";
var widget = CreateStandardWidget(type);
// Check any custom parameters that could cause creating the widget to fail.
blog = GetBlog(BlogId, BlogPath);
if (blog == null) {
Context.Output.WriteLine(T("Creating {0} widget failed: blog was not found.", type));
return;
}
// Create the widget using the standard parameters.
var widget = _widgetCommandsService.CreateBaseWidget(
Context, type, Title, Name, Zone, Position, Layer, Identity, RenderTitle, Owner, null, false, null);
if (widget == null) {
return;
}
// Set the custom parameters.
widget.As<BlogArchivesPart>().BlogId = blog.Id;
_contentManager.Publish(widget.ContentItem);
// Publish the successfully created widget.
_widgetCommandsService.Publish(widget);
Context.Output.WriteLine(T("{0} widget created successfully.", type).Text);
}
private WidgetPart CreateStandardWidget(string type) {
var widgetTypeNames = _widgetsService.GetWidgetTypeNames().ToList();
if (!widgetTypeNames.Contains(type)) {
Context.Output.WriteLine(T("Creating widget failed: type {0} was not found. Supported widget types are: {1}.",
type,
string.Join(" ", widgetTypeNames)));
return null;
}
var layer = GetLayer(Layer);
if (layer == null) {
Context.Output.WriteLine(T("Creating {0} widget failed: layer {1} was not found.", type, Layer));
return null;
}
blog = GetBlog(BlogId, BlogPath);
if (blog == null) {
Context.Output.WriteLine(T("Creating {0} widget failed: blog was not found.", type));
return null;
}
var widget = _widgetsService.CreateWidget(layer.ContentItem.Id, type, T(Title).Text, Position, Zone);
if (!String.IsNullOrWhiteSpace(Name)) {
widget.Name = Name.Trim();
}
widget.RenderTitle = RenderTitle;
if (String.IsNullOrEmpty(Owner)) {
Owner = _siteService.GetSiteSettings().SuperUser;
}
var owner = _membershipService.GetUser(Owner);
widget.As<ICommonPart>().Owner = owner;
if (widget.Has<IdentityPart>() && !String.IsNullOrEmpty(Identity)) {
widget.As<IdentityPart>().Identifier = Identity;
}
return widget;
}
private LayerPart GetLayer(string layer) {
var layers = _widgetsService.GetLayers();
return layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, layer, StringComparison.OrdinalIgnoreCase));
}
private BlogPart GetBlog(int blogId, string blogPath) {
return _contentManager.Get<BlogPart>(blogId) ?? _blogService.Get(blogPath);
}

View File

@@ -1,31 +1,16 @@
using System;
using System.Linq;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Tags.Models;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;
namespace Orchard.Tags.Commands {
public class TagWidgetCommands : DefaultOrchardCommandHandler {
private readonly IWidgetsService _widgetsService;
private readonly ISiteService _siteService;
private readonly IMembershipService _membershipService;
private readonly IContentManager _contentManager;
private readonly IWidgetCommandsService _widgetCommandsService;
public TagWidgetCommands(
IWidgetsService widgetsService,
ISiteService siteService,
IMembershipService membershipService,
IContentManager contentManager) {
_widgetsService = widgetsService;
_siteService = siteService;
_membershipService = membershipService;
_contentManager = contentManager;
IWidgetCommandsService widgetCommandsService) {
_widgetCommandsService = widgetCommandsService;
RenderTitle = true;
}
@@ -66,34 +51,18 @@ namespace Orchard.Tags.Commands {
public void CreateTagsCloudWidget() {
var type = "TagCloud";
var layer = GetLayer(Layer);
if (layer == null) {
Context.Output.WriteLine(T("Creating {0} widget failed: layer {1} was not found.", type, Layer));
return;
}
// Check any custom parameters that could cause creating the widget to fail.
// Nothing to check in this widget, see BlogWidgetCommands.cs for an example.
var widget = _widgetsService.CreateWidget(layer.ContentItem.Id, type, T(Title).Text, Position, Zone);
if (!String.IsNullOrWhiteSpace(Name)) {
widget.Name = Name.Trim();
}
widget.RenderTitle = RenderTitle;
if (String.IsNullOrEmpty(Owner)) {
Owner = _siteService.GetSiteSettings().SuperUser;
}
var owner = _membershipService.GetUser(Owner);
widget.As<ICommonPart>().Owner = owner;
if (widget.Has<IdentityPart>() && !String.IsNullOrEmpty(Identity)) {
widget.As<IdentityPart>().Identifier = Identity;
}
// Create the widget using the standard parameters.
var widget = _widgetCommandsService.CreateBaseWidget(
Context, type, Title, Name, Zone, Position, Layer, Identity, RenderTitle, Owner, null, false, null);
if (widget == null) {
return;
}
// Set the custom parameters.
widget.As<TagCloudPart>().Slug = Slug;
// It's an optional parameter and defaults to 5.
@@ -104,13 +73,9 @@ namespace Orchard.Tags.Commands {
}
}
_contentManager.Publish(widget.ContentItem);
// Publish the successfully created widget.
_widgetCommandsService.Publish(widget);
Context.Output.WriteLine(T("{0} widget created successfully.", type).Text);
}
private LayerPart GetLayer(string layer) {
var layers = _widgetsService.GetLayers();
return layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, layer, StringComparison.OrdinalIgnoreCase));
}
}
}

View File

@@ -1,37 +1,13 @@
using System;
using System.Linq;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Core.Navigation.Models;
using Orchard.Core.Navigation.Services;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Widgets.Models;
using Orchard.Commands;
using Orchard.Widgets.Services;
namespace Orchard.Widgets.Commands {
public class WidgetCommands : DefaultOrchardCommandHandler {
private readonly IWidgetsService _widgetsService;
private readonly ISiteService _siteService;
private readonly IMembershipService _membershipService;
private readonly IMenuService _menuService;
private readonly IContentManager _contentManager;
private const string LoremIpsum = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a nibh ut tortor dapibus vestibulum. Aliquam vel sem nibh. Suspendisse vel condimentum tellus.</p>";
private readonly IWidgetCommandsService _widgetCommandsService;
public WidgetCommands(
IWidgetsService widgetsService,
ISiteService siteService,
IMembershipService membershipService,
IMenuService menuService,
IContentManager contentManager) {
_widgetsService = widgetsService;
_siteService = siteService;
_membershipService = membershipService;
_menuService = menuService;
_contentManager = contentManager;
IWidgetCommandsService widgetCommandsService) {
_widgetCommandsService = widgetCommandsService;
RenderTitle = true;
}
@@ -76,67 +52,15 @@ namespace Orchard.Widgets.Commands {
[CommandHelp("widget create <type> /Title:<title> /Name:<name> /Zone:<zone> /Position:<position> /Layer:<layer> [/Identity:<identity>] [/RenderTitle:true|false] [/Owner:<owner>] [/Text:<text>] [/UseLoremIpsumText:true|false] [/MenuName:<name>]\r\n\t" + "Creates a new widget")]
[OrchardSwitches("Title,Name,Zone,Position,Layer,Identity,Owner,Text,UseLoremIpsumText,MenuName,RenderTitle")]
public void Create(string type) {
var widgetTypeNames = _widgetsService.GetWidgetTypeNames().ToList();
if (!widgetTypeNames.Contains(type)) {
Context.Output.WriteLine(T("Creating widget failed : type {0} was not found. Supported widget types are: {1}.",
type,
string.Join(" ", widgetTypeNames)));
var widget = _widgetCommandsService.CreateBaseWidget(
Context, type, Title, Name, Zone, Position, Layer, Identity, RenderTitle, Owner, Text, UseLoremIpsumText, MenuName);
if (widget == null) {
return;
}
var layer = GetLayer(Layer);
if (layer == null) {
Context.Output.WriteLine(T("Creating widget failed : layer {0} was not found.", Layer));
return;
}
var widget = _widgetsService.CreateWidget(layer.ContentItem.Id, type, T(Title).Text, Position, Zone);
if (!String.IsNullOrWhiteSpace(Name)) {
widget.Name = Name.Trim();
}
var text = String.Empty;
if (widget.Has<BodyPart>()) {
if (UseLoremIpsumText) {
text = T(LoremIpsum).Text;
}
else {
if (!String.IsNullOrEmpty(Text)) {
text = Text;
}
}
widget.As<BodyPart>().Text = text;
}
widget.RenderTitle = RenderTitle;
if(widget.Has<MenuWidgetPart>() && !String.IsNullOrWhiteSpace(MenuName)) {
var menu = _menuService.GetMenu(MenuName);
if(menu != null) {
widget.RenderTitle = false;
widget.As<MenuWidgetPart>().MenuContentItemId = menu.ContentItem.Id;
}
}
if (String.IsNullOrEmpty(Owner)) {
Owner = _siteService.GetSiteSettings().SuperUser;
}
var owner = _membershipService.GetUser(Owner);
widget.As<ICommonPart>().Owner = owner;
if (widget.Has<IdentityPart>() && !String.IsNullOrEmpty(Identity)) {
widget.As<IdentityPart>().Identifier = Identity;
}
_contentManager.Publish(widget.ContentItem);
_widgetCommandsService.Publish(widget);
Context.Output.WriteLine(T("Widget created successfully.").Text);
}
private LayerPart GetLayer(string layer) {
var layers = _widgetsService.GetLayers();
return layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, layer, StringComparison.OrdinalIgnoreCase));
}
}
}

View File

@@ -116,6 +116,7 @@
<Compile Include="Filters\WidgetFilter.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Conditions\ContentDisplayedRuleProvider.cs" />
<Compile Include="Services\IWidgetCommandsService.cs" />
<Compile Include="Services\RuleManager.cs" />
<Compile Include="Services\DefaultLayerEvaluationService.cs" />
<Compile Include="Services\ILayerEvaluationService.cs" />
@@ -124,6 +125,7 @@
<Compile Include="Services\IWidgetsService.cs" />
<Compile Include="Services\LayerResolverSelector.cs" />
<Compile Include="Services\RuleContext.cs" />
<Compile Include="Services\WidgetCommandsService.cs" />
<Compile Include="Services\WidgetsService.cs" />
<Compile Include="Shapes.cs" />
<Compile Include="ViewModels\WidgetElementViewModel.cs" />

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Commands;
using Orchard.Widgets.Models;
namespace Orchard.Widgets.Services {
public interface IWidgetCommandsService : IDependency {
WidgetPart CreateBaseWidget(CommandContext context, string type, string title, string name, string zone, string position, string layer, string identity, bool renderTitle, string owner, string text, bool useLoremIpsumText, string menuName);
void Publish(WidgetPart widget);
}
}

View File

@@ -0,0 +1,106 @@
using System;
using System.Linq;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Core.Navigation.Models;
using Orchard.Core.Navigation.Services;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Widgets.Models;
namespace Orchard.Widgets.Services {
public class WidgetCommandsService : IWidgetCommandsService {
private readonly IMenuService _menuService;
private readonly IWidgetsService _widgetsService;
private readonly ISiteService _siteService;
private readonly IMembershipService _membershipService;
private readonly IContentManager _contentManager;
private const string LoremIpsum = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a nibh ut tortor dapibus vestibulum. Aliquam vel sem nibh. Suspendisse vel condimentum tellus.</p>";
public WidgetCommandsService(
IWidgetsService widgetsService,
IMenuService menuService,
ISiteService siteService,
IMembershipService membershipService,
IContentManager contentManager) {
_siteService = siteService;
_membershipService = membershipService;
_widgetsService = widgetsService;
_menuService = menuService;
_contentManager = contentManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public WidgetPart CreateBaseWidget(CommandContext context, string type, string title, string name, string zone, string position, string layer, string identity, bool renderTitle, string owner, string text, bool useLoremIpsumText, string menuName) {
var widgetTypeNames = _widgetsService.GetWidgetTypeNames().ToList();
if (!widgetTypeNames.Contains(type)) {
context.Output.WriteLine(T("Creating widget failed : type {0} was not found. Supported widget types are: {1}.",
type,
string.Join(" ", widgetTypeNames)));
return null;
}
var layerPart = GetLayer(layer);
if (layerPart == null) {
context.Output.WriteLine(T("Creating widget failed : layer {0} was not found.", layer));
return null;
}
var widget = _widgetsService.CreateWidget(layerPart.ContentItem.Id, type, T(title).Text, position, zone);
if (!String.IsNullOrWhiteSpace(name)) {
widget.Name = name.Trim();
}
var widgetText = String.Empty;
if (widget.Has<BodyPart>()) {
if (useLoremIpsumText) {
widgetText = T(LoremIpsum).Text;
}
else {
if (!String.IsNullOrEmpty(text)) {
widgetText = text;
}
}
widget.As<BodyPart>().Text = text;
}
widget.RenderTitle = renderTitle;
if (widget.Has<MenuWidgetPart>() && !String.IsNullOrWhiteSpace(menuName)) {
var menu = _menuService.GetMenu(menuName);
if (menu != null) {
widget.RenderTitle = false;
widget.As<MenuWidgetPart>().MenuContentItemId = menu.ContentItem.Id;
}
}
if (String.IsNullOrEmpty(owner)) {
owner = _siteService.GetSiteSettings().SuperUser;
}
var widgetOwner = _membershipService.GetUser(owner);
widget.As<ICommonPart>().Owner = widgetOwner;
if (widget.Has<IdentityPart>() && !String.IsNullOrEmpty(identity)) {
widget.As<IdentityPart>().Identifier = identity;
}
return widget;
}
private LayerPart GetLayer(string layer) {
var layers = _widgetsService.GetLayers();
return layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, layer, StringComparison.OrdinalIgnoreCase));
}
public void Publish(WidgetPart widget) {
_contentManager.Publish(widget.ContentItem);
}
}
}