2 using erminas.SmartAPI.CMS.Project.Pages;
3 using erminas.SmartAPI.Exceptions;
4 using erminas.SmartAPI.Utils;
6 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
8 internal class ReferencePreassignment
10 private readonly IReferencePreassignable _parent;
11 private bool _isInitialized;
12 private IReferencePreassignTarget _preassignTarget;
14 public ReferencePreassignment(IReferencePreassignable parent)
19 public IReferencePreassignTarget Target
25 return _preassignTarget;
28 var parent = (ContentClassElement) _parent;
29 parent.EnsureInitialization();
30 _isInitialized =
true;
32 var referenceGuidStr = parent.XmlReadWriteWrapper.GetAttributeValue(
"eltrefelementguid");
34 if (Guid.TryParse(referenceGuidStr, out referenceGuid))
36 var type = parent.XmlReadWriteWrapper.GetAttributeValue(
"eltrefelementtype");
42 parent.Project.LanguageVariants.Current,
46 return _preassignTarget;
49 IContentClassElement ccOut;
50 if (parent.ContentClass.Elements.TryGetByGuid(referenceGuid, out ccOut))
52 _preassignTarget = (IReferencePreassignTarget) ccOut;
53 return _preassignTarget;
56 _preassignTarget = null;
58 return _preassignTarget;
62 RemovePreassignedTarget();
65 SetPreassignedTarget(value);
66 _preassignTarget = value;
71 private void SetPreassignedTarget(IReferencePreassignTarget value)
73 const string SET_TARGET =
74 @"<CLIPBOARD action=""ReferenceToPage"" guid=""{0}"" type=""project.4145"" descent=""unknown"" addition="""">
75 <ENTRY guid=""{1}"" type=""page"" descent=""unknown"" />
77 var result = _parent.Project.ExecuteRQL(SET_TARGET.RQLFormat(_parent, value));
78 if (!result.InnerText.Contains(
"ok"))
81 _parent.Project.Session.ServerLogin,
82 string.Format(
"Could not set reference on {0} to {1}", _parent, value));
86 private void RemovePreassignedTarget()
93 const string REMOVE_TARGET =
94 @"<TEMPLATE><ELEMENT action=""unlink"" guid=""{0}""><ELEMENT guid=""{1}""/></ELEMENT></TEMPLATE>";
95 _parent.Project.ExecuteRQL(REMOVE_TARGET.RQLFormat(_parent, Target));
96 _isInitialized =
false;
97 ((ContentClassElement) _parent).IsInitialized =
false;
100 public void InvalidateCache()
102 _isInitialized =
false;