17 using System.Collections.Generic;
20 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
24 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
26 public interface IOptionList : IValueElement<IOptionListEntry>
28 bool HasDefaultValue {
get; }
29 IRDEnumerable<IOptionListEntry> PossibleValues {
get; }
30 string ValueString {
get; }
35 bool IsDefault {
get; }
41 public OptionListEntry(Guid defaultGuid, XmlElement element)
43 Guid = element.GetGuid(
"guid");
44 Name = element.GetAttributeValue(
"description");
45 Value = element.GetAttributeValue(
"value");
46 IsDefault = defaultGuid == Guid;
49 public Guid Guid {
get;
private set; }
50 public bool IsDefault {
get;
private set; }
51 public string Name {
get;
private set; }
52 public string Value {
get;
private set; }
56 internal class OptionList : PageElement,
IOptionList
58 private List<IOptionListEntry> _entries;
61 public OptionList(IProject project, Guid guid, ILanguageVariant languageVariant)
62 : base(project, guid, languageVariant)
66 internal OptionList(IProject project, XmlElement xmlElement) : base(project, xmlElement)
68 if (xmlElement.GetElementsByTagName(
"SELECTIONS").Count != 1)
70 IsInitialized =
false;
81 const string SAVE_VALUE =
82 @"<ELEMENTS translationmode=""0"" action=""save"" reddotcacheguid="""" ><ELT guid=""{0}"" extendedinfo="""" reddotcacheguid="""" value=""{1}"" ></ELT></ELEMENTS>";
85 string value = _value == null ? RQL.SESSIONKEY_PLACEHOLDER : _value.Guid.ToRQLString();
86 string query = SAVE_VALUE.RQLFormat(
this, value);
87 Project.ExecuteRQL(query);
91 public bool HasDefaultValue
93 get {
return Value != null &&
string.IsNullOrEmpty(XmlElement.GetAttributeValue(
"value")); }
96 public IRDEnumerable<IOptionListEntry> PossibleValues
98 get {
return LazyLoad(ref _entries).ToRDEnumerable(); }
103 get {
return LazyLoad(ref _value); }
104 set { _value = value; }
107 public string ValueString
109 get {
return Value == null ? null : Value.Value; }
112 protected override sealed
void LoadWholePageElement()
117 protected override sealed XmlElement RetrieveWholeObject()
121 const string RETRIEVE_OPTION_LIST =
122 @"<ELT action=""load"" subelements=""1"" guid=""{0}"" ><SELECTIONS action=""list""/></ELT>";
126 Project.ExecuteRQL(
string.Format(RETRIEVE_OPTION_LIST, Guid.ToRQLString()))
127 .GetElementsByTagName(
"ELT")[0];
131 private void LoadXml()
134 _xmlElement.TryGetGuid(
"eltdefaultselectionguid", out defaultGuid);
136 XmlNodeList elements = _xmlElement.GetElementsByTagName(
"SELECTION");
138 (from XmlElement curElement in elements
139 select (
IOptionListEntry) new OptionListEntry(defaultGuid, curElement)).ToList();
142 _value = _xmlElement.TryGetGuid("value", out selectedGuid)
143 ? _entries.FirstOrDefault(entry => entry.Guid == selectedGuid)
144 : _entries.FirstOrDefault(entry => entry.IsDefault);