mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Addind events to field storage
--HG-- branch : 1.x
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using Orchard.Events;
|
||||||
|
|
||||||
|
namespace Orchard.ContentManagement.FieldStorage
|
||||||
|
{
|
||||||
|
public class FieldStorageEventContext
|
||||||
|
{
|
||||||
|
public IContent Content { get; set; }
|
||||||
|
public string PartName { get; set; }
|
||||||
|
public string FieldName { get; set; }
|
||||||
|
public string ValueName { get; set; }
|
||||||
|
public object Value { get; set; }
|
||||||
|
public Type ValueType { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IFieldStorageEvents : IEventHandler
|
||||||
|
{
|
||||||
|
void SetCalled(FieldStorageEventContext context);
|
||||||
|
}
|
||||||
|
}
|
@@ -1,9 +1,16 @@
|
|||||||
using System.Xml;
|
using System.Collections.Generic;
|
||||||
|
using System.Xml;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Orchard.ContentManagement.MetaData.Models;
|
using Orchard.ContentManagement.MetaData.Models;
|
||||||
|
|
||||||
namespace Orchard.ContentManagement.FieldStorage.InfosetStorage {
|
namespace Orchard.ContentManagement.FieldStorage.InfosetStorage {
|
||||||
public class InfosetStorageProvider : IFieldStorageProvider {
|
public class InfosetStorageProvider : IFieldStorageProvider {
|
||||||
|
private readonly IEnumerable<IFieldStorageEvents> _events;
|
||||||
|
|
||||||
|
public InfosetStorageProvider(IEnumerable<IFieldStorageEvents> events) {
|
||||||
|
_events = events;
|
||||||
|
}
|
||||||
|
|
||||||
public string ProviderName {
|
public string ProviderName {
|
||||||
get { return FieldStorageProviderSelector.DefaultProviderName; }
|
get { return FieldStorageProviderSelector.DefaultProviderName; }
|
||||||
}
|
}
|
||||||
@@ -14,8 +21,24 @@ namespace Orchard.ContentManagement.FieldStorage.InfosetStorage {
|
|||||||
var infosetPart = contentPart.As<InfosetPart>();
|
var infosetPart = contentPart.As<InfosetPart>();
|
||||||
|
|
||||||
return new SimpleFieldStorage(
|
return new SimpleFieldStorage(
|
||||||
name => Get(infosetPart.Infoset.Element, partName, fieldName, name),
|
(name, valueType) => Get(infosetPart.Infoset.Element, partName, fieldName, name),
|
||||||
(name, value) => Set(infosetPart.Infoset.Element, partName, fieldName, name, value));
|
(name, valueType, value) => {
|
||||||
|
Set(infosetPart.Infoset.Element, partName, fieldName, name, value);
|
||||||
|
|
||||||
|
var context = new FieldStorageEventContext {
|
||||||
|
FieldName = partFieldDefinition.Name,
|
||||||
|
PartName = contentPart.PartDefinition.Name,
|
||||||
|
Value = value,
|
||||||
|
ValueName = name,
|
||||||
|
ValueType = valueType,
|
||||||
|
Content = infosetPart
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach(var fieldEvent in _events) {
|
||||||
|
fieldEvent.SetCalled(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string Get(XElement element, string partName, string fieldName, string valueName) {
|
private static string Get(XElement element, string partName, string fieldName, string valueName) {
|
||||||
@@ -37,7 +60,7 @@ namespace Orchard.ContentManagement.FieldStorage.InfosetStorage {
|
|||||||
return valueAttribute.Value;
|
return valueAttribute.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Set(XElement element, string partName, string fieldName, string valueName, string value) {
|
private void Set(XElement element, string partName, string fieldName, string valueName, string value) {
|
||||||
var partElement = element.Element(partName);
|
var partElement = element.Element(partName);
|
||||||
if (partElement == null) {
|
if (partElement == null) {
|
||||||
partElement = new XElement(partName);
|
partElement = new XElement(partName);
|
||||||
|
@@ -3,23 +3,23 @@ using System.Globalization;
|
|||||||
|
|
||||||
namespace Orchard.ContentManagement.FieldStorage {
|
namespace Orchard.ContentManagement.FieldStorage {
|
||||||
public class SimpleFieldStorage : IFieldStorage {
|
public class SimpleFieldStorage : IFieldStorage {
|
||||||
public SimpleFieldStorage(Func<string, string> getter, Action<string, string> setter) {
|
public SimpleFieldStorage(Func<string, Type, string> getter, Action<string, Type, string> setter) {
|
||||||
Getter = getter;
|
Getter = getter;
|
||||||
Setter = setter;
|
Setter = setter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Func<string, string> Getter { get; set; }
|
public Func<string, Type, string> Getter { get; set; }
|
||||||
public Action<string, string> Setter { get; set; }
|
public Action<string, Type, string> Setter { get; set; }
|
||||||
|
|
||||||
public T Get<T>(string name) {
|
public T Get<T>(string name) {
|
||||||
var value = Getter(name);
|
var value = Getter(name, typeof(T));
|
||||||
return string.IsNullOrEmpty(value)
|
return string.IsNullOrEmpty(value)
|
||||||
? default(T)
|
? default(T)
|
||||||
: (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
|
: (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set<T>(string name, T value) {
|
public void Set<T>(string name, T value) {
|
||||||
Setter(name, Convert.ToString(value, CultureInfo.InvariantCulture));
|
Setter(name, typeof(T), Convert.ToString(value, CultureInfo.InvariantCulture));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -171,6 +171,7 @@
|
|||||||
<Compile Include="ContentManagement\ContentPartBehavior.cs" />
|
<Compile Include="ContentManagement\ContentPartBehavior.cs" />
|
||||||
<Compile Include="ContentManagement\DefaultContentDisplay.cs" />
|
<Compile Include="ContentManagement\DefaultContentDisplay.cs" />
|
||||||
<Compile Include="ContentManagement\Drivers\ContentShapeResult.cs" />
|
<Compile Include="ContentManagement\Drivers\ContentShapeResult.cs" />
|
||||||
|
<Compile Include="ContentManagement\FieldStorage\IFieldStorageEvents.cs" />
|
||||||
<Compile Include="ContentManagement\GroupInfo.cs" />
|
<Compile Include="ContentManagement\GroupInfo.cs" />
|
||||||
<Compile Include="ContentManagement\Handlers\BuildShapeContext.cs" />
|
<Compile Include="ContentManagement\Handlers\BuildShapeContext.cs" />
|
||||||
<Compile Include="ContentManagement\Handlers\ExportContentContext.cs" />
|
<Compile Include="ContentManagement\Handlers\ExportContentContext.cs" />
|
||||||
|
Reference in New Issue
Block a user