2010-10-11 10:15:15 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2010-12-01 17:37:11 -08:00
|
|
|
|
using Orchard.Environment.Extensions.Models;
|
2010-11-05 16:33:24 -07:00
|
|
|
|
using Orchard.Environment.Features;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
using Orchard.Widgets.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Widgets.Services {
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly]
|
|
|
|
|
|
public class WidgetsService : IWidgetsService {
|
2010-11-05 16:33:24 -07:00
|
|
|
|
private readonly IFeatureManager _featureManager;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
|
|
|
|
|
|
|
public WidgetsService(
|
|
|
|
|
|
IContentManager contentManager,
|
2010-11-05 16:33:24 -07:00
|
|
|
|
IFeatureManager featureManager) {
|
2010-10-11 10:15:15 -07:00
|
|
|
|
|
|
|
|
|
|
_contentManager = contentManager;
|
2010-11-05 16:33:24 -07:00
|
|
|
|
_featureManager = featureManager;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetWidgetTypes() {
|
|
|
|
|
|
return _contentManager.GetContentTypeDefinitions()
|
2010-10-11 15:51:44 -07:00
|
|
|
|
.Where(contentTypeDefinition => contentTypeDefinition.Settings.ContainsKey("Stereotype") && contentTypeDefinition.Settings["Stereotype"] == "Widget")
|
2010-10-11 10:15:15 -07:00
|
|
|
|
.Select(contentTypeDefinition => contentTypeDefinition.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<LayerPart> GetLayers() {
|
|
|
|
|
|
return _contentManager
|
|
|
|
|
|
.Query<LayerPart, LayerPartRecord>()
|
|
|
|
|
|
.List();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<WidgetPart> GetWidgets() {
|
|
|
|
|
|
return _contentManager
|
|
|
|
|
|
.Query<WidgetPart, WidgetPartRecord>()
|
|
|
|
|
|
.List();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetZones() {
|
2010-11-05 16:33:24 -07:00
|
|
|
|
return _featureManager.GetEnabledFeatures()
|
|
|
|
|
|
.Select(x => x.Extension)
|
2010-12-01 17:52:10 -08:00
|
|
|
|
.Where(x => DefaultExtensionTypes.IsTheme(x.ExtensionType))
|
2010-11-05 16:33:24 -07:00
|
|
|
|
.SelectMany(x => x.Zones.Split(','))
|
|
|
|
|
|
.Distinct()
|
2010-11-11 16:15:49 -08:00
|
|
|
|
.Select(x => x.Trim())
|
2010-11-05 16:33:24 -07:00
|
|
|
|
.ToArray();
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<WidgetPart> GetWidgets(int layerId) {
|
|
|
|
|
|
return GetWidgets().Where(widgetPart => widgetPart.As<ICommonPart>().Container.ContentItem.Id == layerId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public LayerPart GetLayer(int layerId) {
|
|
|
|
|
|
return GetLayers().FirstOrDefault(layer => layer.Id == layerId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public LayerPart CreateLayer(string name, string description, string layerRule) {
|
|
|
|
|
|
LayerPart layerPart = _contentManager.Create<LayerPart>("Layer",
|
|
|
|
|
|
layer => {
|
|
|
|
|
|
layer.Record.Name = name;
|
|
|
|
|
|
layer.Record.Description = description;
|
|
|
|
|
|
layer.Record.LayerRule = layerRule;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return layerPart;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateLayer(int layerId, string name, string description, string layerRule) {
|
|
|
|
|
|
LayerPart layerPart = GetLayer(layerId);
|
|
|
|
|
|
layerPart.Record.Name = name;
|
|
|
|
|
|
layerPart.Record.Description = description;
|
|
|
|
|
|
layerPart.Record.LayerRule = layerRule;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteLayer(int layerId) {
|
2010-10-14 16:24:05 -07:00
|
|
|
|
// Delete widgets in the layer
|
|
|
|
|
|
foreach (WidgetPart widgetPart in GetWidgets(layerId)) {
|
|
|
|
|
|
DeleteWidget(widgetPart.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Delete actual layer
|
2010-10-11 10:15:15 -07:00
|
|
|
|
_contentManager.Remove(GetLayer(layerId).ContentItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WidgetPart GetWidget(int widgetId) {
|
2010-10-14 16:24:05 -07:00
|
|
|
|
return _contentManager
|
|
|
|
|
|
.Query<WidgetPart, WidgetPartRecord>()
|
|
|
|
|
|
.Where(widget => widget.Id == widgetId)
|
|
|
|
|
|
.List()
|
|
|
|
|
|
.FirstOrDefault();
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WidgetPart CreateWidget(int layerId, string widgetType, string title, string position, string zone) {
|
|
|
|
|
|
LayerPart layerPart = GetLayer(layerId);
|
|
|
|
|
|
|
|
|
|
|
|
WidgetPart widgetPart = _contentManager.Create<WidgetPart>(widgetType,
|
|
|
|
|
|
widget => {
|
|
|
|
|
|
widget.Record.Title = title;
|
|
|
|
|
|
widget.Record.Position = position;
|
|
|
|
|
|
widget.Record.Zone = zone;
|
|
|
|
|
|
widget.LayerPart = layerPart;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return widgetPart;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateWidget(int widgetId, string title, string position, string zone) {
|
|
|
|
|
|
WidgetPart widgetPart = GetWidget(widgetId);
|
|
|
|
|
|
widgetPart.Record.Title = title;
|
|
|
|
|
|
widgetPart.Record.Position = position;
|
|
|
|
|
|
widgetPart.Record.Zone = zone;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteWidget(int widgetId) {
|
|
|
|
|
|
_contentManager.Remove(GetWidget(widgetId).ContentItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-10-14 10:54:32 -07:00
|
|
|
|
public bool MoveWidgetUp(WidgetPart widgetPart) {
|
2010-10-19 23:11:35 -07:00
|
|
|
|
int currentPosition = ParsePosition(widgetPart);
|
2010-10-14 10:54:32 -07:00
|
|
|
|
|
2010-10-14 09:21:09 -07:00
|
|
|
|
WidgetPart widgetBefore = GetWidgets(widgetPart.LayerPart.Id)
|
|
|
|
|
|
.Where(widget => widget.Zone == widgetPart.Zone)
|
|
|
|
|
|
.OrderByDescending(widget => widget.Position, new UI.FlatPositionComparer())
|
2010-10-19 23:11:35 -07:00
|
|
|
|
.FirstOrDefault(widget => ParsePosition(widget) < currentPosition);
|
2010-10-14 10:54:32 -07:00
|
|
|
|
|
2010-10-14 09:21:09 -07:00
|
|
|
|
if (widgetBefore != null) {
|
2010-10-14 10:54:32 -07:00
|
|
|
|
SwitchWidgetPositions(widgetBefore, widgetPart);
|
2010-10-14 09:21:09 -07:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
2010-10-14 09:21:09 -07:00
|
|
|
|
|
|
|
|
|
|
return false;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-12-07 16:11:29 -08:00
|
|
|
|
private static int ParsePosition(WidgetPart widgetPart) {
|
2010-10-19 23:11:35 -07:00
|
|
|
|
int value;
|
|
|
|
|
|
if (!int.TryParse(widgetPart.Record.Position, out value))
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-10-14 10:54:32 -07:00
|
|
|
|
public bool MoveWidgetUp(int widgetId) {
|
|
|
|
|
|
return MoveWidgetUp(GetWidget(widgetId));
|
|
|
|
|
|
}
|
2010-10-11 10:15:15 -07:00
|
|
|
|
|
2010-10-14 10:54:32 -07:00
|
|
|
|
public bool MoveWidgetDown(WidgetPart widgetPart) {
|
2010-10-19 23:11:35 -07:00
|
|
|
|
int currentPosition = ParsePosition(widgetPart);
|
2010-10-14 09:21:09 -07:00
|
|
|
|
|
|
|
|
|
|
WidgetPart widgetAfter = GetWidgets(widgetPart.LayerPart.Id)
|
|
|
|
|
|
.Where(widget => widget.Zone == widgetPart.Zone)
|
|
|
|
|
|
.OrderBy(widget => widget.Position, new UI.FlatPositionComparer())
|
2010-10-19 23:11:35 -07:00
|
|
|
|
.FirstOrDefault(widget => ParsePosition(widget) > currentPosition);
|
2010-10-14 10:54:32 -07:00
|
|
|
|
|
2010-10-14 09:21:09 -07:00
|
|
|
|
if (widgetAfter != null) {
|
2010-10-14 10:54:32 -07:00
|
|
|
|
SwitchWidgetPositions(widgetAfter, widgetPart);
|
|
|
|
|
|
|
2010-10-14 09:21:09 -07:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
2010-10-14 10:54:32 -07:00
|
|
|
|
|
|
|
|
|
|
public bool MoveWidgetDown(int widgetId) {
|
|
|
|
|
|
return MoveWidgetDown(GetWidget(widgetId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void SwitchWidgetPositions(WidgetPart sourceWidget, WidgetPart targetWidget) {
|
|
|
|
|
|
string tempPosition = sourceWidget.Record.Position;
|
2010-10-19 23:56:49 -07:00
|
|
|
|
sourceWidget.Record.Position = targetWidget.Record.Position;
|
2010-10-14 10:54:32 -07:00
|
|
|
|
targetWidget.Record.Position = tempPosition;
|
|
|
|
|
|
}
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|