Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions content-management/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/Sdl.Web.Templating/bin
/Sdl.Web.Templating/obj
/Sdl.Web.Templating/*.user
/Sdl.Web.Templating/*.suo
bin
obj
*.user
*.suo
_references/2013-sp1/*.dll
_references/ecl/*.dll
22 changes: 22 additions & 0 deletions content-management/Sdl.Web.Templating.MediaManager.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sdl.Web.Templating.MediaManager", "Sdl.Web.Templating.MediaManager\Sdl.Web.Templating.MediaManager.csproj", "{4B9444A9-D8C4-43FD-ABEA-02BFA7123B8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4B9444A9-D8C4-43FD-ABEA-02BFA7123B8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B9444A9-D8C4-43FD-ABEA-02BFA7123B8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B9444A9-D8C4-43FD-ABEA-02BFA7123B8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B9444A9-D8C4-43FD-ABEA-02BFA7123B8C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;

namespace Sdl.Web.Tridion.Common
{
public static class ItemFieldsExtensions
{
public static double GetNumberValue(this ItemFields fields, string fieldName)
{
return fields.GetNumberValues(fieldName).FirstOrDefault();
}

public static IEnumerable<double> GetNumberValues(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as NumberField).Values
: new double[0];
}

public static Keyword GetKeywordValue(this ItemFields fields, string fieldName)
{
return fields.GetKeywordValues(fieldName).FirstOrDefault();
}

public static IEnumerable<Keyword> GetKeywordValues(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as KeywordField).Values
: Enumerable.Empty<Keyword>();
}

public static Component GetComponentValue(this ItemFields fields, string fieldName)
{
return fields.GetComponentValues(fieldName).FirstOrDefault();
}

public static IEnumerable<Component> GetComponentValues(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as ComponentLinkField).Values
: Enumerable.Empty<Component>();
}

public static string GetExternalLink(this ItemFields fields, string fieldName)
{
return fields.GetExternalLinks(fieldName).FirstOrDefault() ?? string.Empty;
}

public static IEnumerable<string> GetExternalLinks(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as ExternalLinkField).Values
: new string[0];
}

public static Component GetMultimediaLink(this ItemFields fields, string fieldName)
{
return fields.GetMultimediaLinks(fieldName).FirstOrDefault();
}

public static IEnumerable<Component> GetMultimediaLinks(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as MultimediaLinkField).Values
: Enumerable.Empty<Component>();
}

public static ItemFields GetEmbeddedField(this ItemFields fields, string fieldName)
{
return fields.GetEmbeddedFields(fieldName).FirstOrDefault();
}

public static IEnumerable<ItemFields> GetEmbeddedFields(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as EmbeddedSchemaField).Values
: Enumerable.Empty<ItemFields>();
}

public static string GetTextValue(this ItemFields fields, string fieldName)
{
return GetTextValues(fields, fieldName).FirstOrDefault() ?? string.Empty;
}

public static IEnumerable<string> GetTextValues(this ItemFields fields, string fieldName)
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as TextField).Values
: new string[0];
}

public static DateTime? GetDateValue(this ItemFields fields, string fieldName = "date")
{
return fields.GetDateValues(fieldName).FirstOrDefault();
}

public static IEnumerable<DateTime?> GetDateValues(this ItemFields fields, string fieldName = "date")
{
return
null != fields && fields.Contains(fieldName)
? (fields[fieldName] as DateField).Values.Select(d => d == DateTime.MinValue ? null : (DateTime?)d)
: new DateTime?[0];
}

public static int GetFieldValueCount(this ItemFields fields, string fieldName)
{
if (null == fields)
{
return 0;
}

var field = fields[fieldName];

return
field is ComponentLinkField
? (field as ComponentLinkField).Values.Count
: field is TextField
? (field as TextField).Values.Count
: field is EmbeddedSchemaField
? (field as EmbeddedSchemaField).Values.Count
: 0;
}

/// <summary>
/// Manual unification of different field types logic to overcome native tridion implementation shortcoming,
/// which is not polymorphic.
/// </summary>
static readonly IDictionary<Type, Func<ItemFields, string, string>> ValueResolver =
new Dictionary<Type, Func<ItemFields, string, string>> {
{ typeof(KeywordField), (fields, name) => fields.GetKeywordValues(name).Select(k => k.Title).FirstOrDefault() },
{ typeof(ComponentLinkField), (fields, name) => fields.GetComponentValues(name).Select(c => c.Id).FirstOrDefault() },
{ typeof(ExternalLinkField), (fields, name) => fields.GetExternalLinks(name).FirstOrDefault() },
{ typeof(MultimediaLinkField), (fields, name) => fields.GetMultimediaLinks(name).Select(mc => mc.Title).FirstOrDefault() },
{ typeof(DateField), (fields, name) => ((DateTime)fields.GetDateValues(name).FirstOrDefault()).ToString("yyyy-MM-dd HH:mm:ss") }
};

/// <summary>
/// Gets a sensible string represntation of a field.
/// </summary>
public static string GetSingleFieldValue(this ItemFields fields, string fieldName)
{
ItemField field;
Type fieldType;

return
null == fields
|| !fields.Contains(fieldName)
? String.Empty
: ValueResolver.ContainsKey((fieldType = (field = fields[fieldName]).GetType()))
? ValueResolver[fieldType](fields, fieldName) ?? String.Empty
: field.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sdl.Web.Tridion.Common
{
public static class ObjectExtensions
{
public static IEnumerable<TOutput> IfNotNull<TInput, TOutput>(this TInput value, Func<TInput, IEnumerable<TOutput>> getResult)
{
// TODO possible compare of value type with null (http://confluence.jetbrains.com/display/ReSharper/Possible+compare+of+value+type+with+null)
return null != value ? getResult(value) : Enumerable.Empty<TOutput>();
}

public static TOutput IfNotNull<TInput, TOutput>(this TInput value, Func<TInput, TOutput> getResult)
{
// TODO possible compare of value type with null
return null != value ? getResult(value) : default(TOutput);
}

public static void IfNotNull<TInput>(this TInput value, Action<TInput> action)
{
// TODO possible compare of value type with null
if (null != value)
{
action(value);
}
}
}
}
Loading