Advertisement
Guest User

Untitled

a guest
May 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. [FieldType(Name = "Page", Shorthand = "Page")]
  2. public class PageInfoField : IField, IEquatable<PageInfoField>
  3. {
  4. /// <summary>
  5. /// Gets/sets the media id.
  6. /// </summary>
  7. /// <returns></returns>
  8. public Guid? Id { get; set; }
  9.  
  10. /// <summary>
  11. /// Gets/sets the related page object.
  12. /// </summary>
  13. [JsonIgnore]
  14. public Piranha.Models.PageInfo Page { get; private set; }
  15.  
  16. /// <summary>
  17. /// Gets if the field has a page object available.
  18. /// </summary>
  19. public bool HasValue => Page != null;
  20.  
  21. /// <summary>
  22. /// Gets the list item title if this field is used in
  23. /// a collection regions.
  24. /// </summary>
  25. public virtual string GetTitle()
  26. {
  27. return Page?.Title;
  28. }
  29.  
  30. /// <summary>
  31. /// Initializes the field for client use.
  32. /// </summary>
  33. /// <param name="api">The current api</param>
  34. public virtual void Init(IApi api)
  35. {
  36. if (Id.HasValue)
  37. {
  38. Page = api.Pages.GetById<Piranha.Models.PageInfo>(Id.Value);
  39.  
  40. if (Page == null)
  41. {
  42. // The page has been removed, remove the
  43. // missing id.
  44. Id = null;
  45. }
  46. }
  47. }
  48.  
  49. /// <summary>
  50. /// Gets the referenced page.
  51. /// </summary>
  52. /// <param name="api">The current api</param>
  53. /// <returns>The referenced page</returns>
  54. public virtual T GetPageAsync<T>(IApi api) where T : Piranha.Models.GenericPage<T>
  55. {
  56. if (Id.HasValue)
  57. return api.Pages.GetById<T>(Id.Value);
  58. return null;
  59. }
  60.  
  61. /// <summary>
  62. /// Implicit operator for converting a Guid id to a field.
  63. /// </summary>
  64. /// <param name="str">The string value</param>
  65. public static implicit operator PageInfoField(Guid guid)
  66. {
  67. return new PageInfoField { Id = guid };
  68. }
  69.  
  70. /// <summary>
  71. /// Implicit operator for converting a page object to a field.
  72. /// </summary>
  73. /// <param name="page">The page object</param>
  74. public static implicit operator PageInfoField(Piranha.Models.PageBase page)
  75. {
  76. return new PageInfoField { Id = page.Id };
  77. }
  78.  
  79. /// <summary>
  80. /// Gets the hash code for the field.
  81. /// </summary>
  82. public override int GetHashCode()
  83. {
  84. return Id.HasValue ? Id.GetHashCode() : 0;
  85. }
  86.  
  87. /// <summary>
  88. /// Checks if the given object is equal to the field.
  89. /// </summary>
  90. /// <param name="obj">The object</param>
  91. /// <returns>True if the fields are equal</returns>
  92. public override bool Equals(object obj)
  93. {
  94. if (obj is PageInfoField field)
  95. {
  96. return Equals(field);
  97. }
  98. return false;
  99. }
  100.  
  101. /// <summary>
  102. /// Checks if the given field is equal to the field.
  103. /// </summary>
  104. /// <param name="obj">The field</param>
  105. /// <returns>True if the fields are equal</returns>
  106. public virtual bool Equals(PageInfoField obj)
  107. {
  108. if (obj == null)
  109. {
  110. return false;
  111. }
  112. return Id == obj.Id;
  113. }
  114.  
  115. /// <summary>
  116. /// Checks if the fields are equal.
  117. /// </summary>
  118. /// <param name="field1">The first field</param>
  119. /// <param name="field2">The second field</param>
  120. /// <returns>True if the fields are equal</returns>
  121. public static bool operator ==(PageInfoField field1, PageInfoField field2)
  122. {
  123. if ((object)field1 != null && (object)field2 != null)
  124. {
  125. return field1.Equals(field2);
  126. }
  127. return false;
  128. }
  129.  
  130. /// <summary>
  131. /// Checks if the fields are not equal.
  132. /// </summary>
  133. /// <param name="field1">The first field</param>
  134. /// <param name="field2">The second field</param>
  135. /// <returns>True if the fields are equal</returns>
  136. public static bool operator !=(PageInfoField field1, PageInfoField field2)
  137. {
  138. return !(field1 == field2);
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement