2011-03-15 15:10:24 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2011-03-25 11:25:42 -07:00
|
|
|
|
using Orchard.Environment.Extensions;
|
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;
|
2011-03-25 11:25:42 -07:00
|
|
|
|
private readonly IExtensionManager _extensionManager;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
|
|
|
|
|
|
|
public WidgetsService(
|
|
|
|
|
|
IContentManager contentManager,
|
2011-03-25 11:25:42 -07:00
|
|
|
|
IFeatureManager featureManager,
|
|
|
|
|
|
IExtensionManager extensionManager) {
|
2010-10-11 10:15:15 -07:00
|
|
|
|
|
|
|
|
|
|
_contentManager = contentManager;
|
2010-11-05 16:33:24 -07:00
|
|
|
|
_featureManager = featureManager;
|
2011-03-25 11:25:42 -07:00
|
|
|
|
_extensionManager = extensionManager;
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-15 15:10:24 -07:00
|
|
|
|
public IEnumerable<Tuple<string, string>> GetWidgetTypes() {
|
2010-10-11 10:15:15 -07:00
|
|
|
|
return _contentManager.GetContentTypeDefinitions()
|
2010-10-11 15:51:44 -07:00
|
|
|
|
.Where(contentTypeDefinition => contentTypeDefinition.Settings.ContainsKey("Stereotype") && contentTypeDefinition.Settings["Stereotype"] == "Widget")
|
2011-03-15 15:10:24 -07:00
|
|
|
|
.Select(contentTypeDefinition =>
|
|
|
|
|
|
Tuple.Create(
|
|
|
|
|
|
contentTypeDefinition.Name,
|
|
|
|
|
|
contentTypeDefinition.Settings.ContainsKey("Description") ? contentTypeDefinition.Settings["Description"] : null));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetWidgetTypeNames() {
|
|
|
|
|
|
return GetWidgetTypes().Select(type => type.Item1);
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-09 11:24:49 -08:00
|
|
|
|
.Where(x => DefaultExtensionTypes.IsTheme(x.ExtensionType) && x.Zones != null)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-25 11:25:42 -07:00
|
|
|
|
public IEnumerable<string> GetZones(ExtensionDescriptor theme) {
|
|
|
|
|
|
IEnumerable<string> zones = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
// get the zones for this theme
|
|
|
|
|
|
if (theme.Zones != null)
|
|
|
|
|
|
zones = theme.Zones.Split(',')
|
|
|
|
|
|
.Distinct()
|
|
|
|
|
|
.Select(x => x.Trim())
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// add the zones for the base theme
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(theme.BaseTheme)) {
|
|
|
|
|
|
string baseTheme = theme.BaseTheme;
|
|
|
|
|
|
theme = _extensionManager.GetExtension(baseTheme);
|
|
|
|
|
|
if (theme != null)
|
|
|
|
|
|
zones.Concat(GetZones(theme).Where(z => !zones.Contains(z)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return zones;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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 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 DeleteWidget(int widgetId) {
|
|
|
|
|
|
_contentManager.Remove(GetWidget(widgetId).ContentItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-17 13:03:59 -07:00
|
|
|
|
public bool MoveWidgetUp(int widgetId) {
|
|
|
|
|
|
return MoveWidgetUp(GetWidget(widgetId));
|
|
|
|
|
|
}
|
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
|
|
|
|
|
2011-03-17 13:03:59 -07:00
|
|
|
|
WidgetPart widgetBefore = GetWidgets()
|
2010-10-14 09:21:09 -07:00
|
|
|
|
.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) {
|
2011-03-18 13:49:27 -07:00
|
|
|
|
widgetPart.Position = widgetBefore.Position;
|
|
|
|
|
|
MakeRoomForWidgetPosition(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
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-17 13:03:59 -07:00
|
|
|
|
public bool MoveWidgetDown(int widgetId) {
|
|
|
|
|
|
return MoveWidgetDown(GetWidget(widgetId));
|
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
|
|
|
|
|
2011-03-17 13:03:59 -07:00
|
|
|
|
WidgetPart widgetAfter = GetWidgets()
|
2010-10-14 09:21:09 -07:00
|
|
|
|
.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) {
|
2011-03-18 13:49:27 -07:00
|
|
|
|
widgetAfter.Position = widgetPart.Position;
|
|
|
|
|
|
MakeRoomForWidgetPosition(widgetAfter);
|
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
|
|
|
|
|
2011-03-21 12:41:28 -07:00
|
|
|
|
public bool MoveWidgetToLayer(int widgetId, int? layerId) {
|
|
|
|
|
|
return MoveWidgetToLayer(GetWidget(widgetId), layerId);
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool MoveWidgetToLayer(WidgetPart widgetPart, int? layerId) {
|
|
|
|
|
|
LayerPart layer = layerId.HasValue
|
|
|
|
|
|
? GetLayer(layerId.Value)
|
|
|
|
|
|
: GetLayers().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (layer != null) {
|
|
|
|
|
|
widgetPart.LayerPart = layer;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-18 13:38:12 -07:00
|
|
|
|
public void MakeRoomForWidgetPosition(int widgetId) {
|
|
|
|
|
|
MakeRoomForWidgetPosition(GetWidget(widgetId));
|
|
|
|
|
|
}
|
|
|
|
|
|
public void MakeRoomForWidgetPosition(WidgetPart widgetPart) {
|
|
|
|
|
|
int targetPosition = ParsePosition(widgetPart);
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerable<WidgetPart> widgetsToMove = GetWidgets()
|
2011-03-18 13:49:27 -07:00
|
|
|
|
.Where(widget => widget.Zone == widgetPart.Zone && ParsePosition(widget) >= targetPosition && widget.Id != widgetPart.Id)
|
2011-03-18 13:38:12 -07:00
|
|
|
|
.OrderBy(widget => widget.Position, new UI.FlatPositionComparer()).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// no need to continue if there are no widgets that will conflict with this widget's position
|
|
|
|
|
|
if (widgetsToMove.Count() == 0 || ParsePosition(widgetsToMove.First()) > targetPosition)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
int position = targetPosition;
|
|
|
|
|
|
foreach (WidgetPart widget in widgetsToMove)
|
|
|
|
|
|
widget.Position = (++position).ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int ParsePosition(WidgetPart widgetPart) {
|
|
|
|
|
|
int value;
|
|
|
|
|
|
if (!int.TryParse(widgetPart.Record.Position, out value))
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
2010-10-11 10:15:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|