BoxSet.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Querying;
  5. using MediaBrowser.Model.Users;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using MediaBrowser.Model.Serialization;
  10. namespace MediaBrowser.Controller.Entities.Movies
  11. {
  12. /// <summary>
  13. /// Class BoxSet
  14. /// </summary>
  15. public class BoxSet : Folder, IHasTrailers, IHasDisplayOrder, IHasLookupInfo<BoxSetInfo>, IHasShares
  16. {
  17. public List<Share> Shares { get; set; }
  18. public BoxSet()
  19. {
  20. RemoteTrailers = EmptyMediaUrlArray;
  21. LocalTrailerIds = EmptyGuidArray;
  22. RemoteTrailerIds = EmptyGuidArray;
  23. DisplayOrder = ItemSortBy.PremiereDate;
  24. Shares = new List<Share>();
  25. }
  26. [IgnoreDataMember]
  27. protected override bool FilterLinkedChildrenPerUser
  28. {
  29. get
  30. {
  31. return true;
  32. }
  33. }
  34. [IgnoreDataMember]
  35. public override bool SupportsInheritedParentImages
  36. {
  37. get
  38. {
  39. return false;
  40. }
  41. }
  42. public Guid[] LocalTrailerIds { get; set; }
  43. public Guid[] RemoteTrailerIds { get; set; }
  44. /// <summary>
  45. /// Gets or sets the remote trailers.
  46. /// </summary>
  47. /// <value>The remote trailers.</value>
  48. public MediaUrl[] RemoteTrailers { get; set; }
  49. /// <summary>
  50. /// Gets or sets the display order.
  51. /// </summary>
  52. /// <value>The display order.</value>
  53. public string DisplayOrder { get; set; }
  54. protected override bool GetBlockUnratedValue(UserPolicy config)
  55. {
  56. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  57. }
  58. public override double? GetDefaultPrimaryImageAspectRatio()
  59. {
  60. double value = 2;
  61. value /= 3;
  62. return value;
  63. }
  64. public override UnratedItem GetBlockUnratedType()
  65. {
  66. return UnratedItem.Movie;
  67. }
  68. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  69. {
  70. if (IsLegacyBoxSet)
  71. {
  72. return base.GetNonCachedChildren(directoryService);
  73. }
  74. return new List<BaseItem>();
  75. }
  76. protected override List<BaseItem> LoadChildren()
  77. {
  78. if (IsLegacyBoxSet)
  79. {
  80. return base.LoadChildren();
  81. }
  82. // Save a trip to the database
  83. return new List<BaseItem>();
  84. }
  85. [IgnoreDataMember]
  86. private bool IsLegacyBoxSet
  87. {
  88. get
  89. {
  90. if (string.IsNullOrWhiteSpace(Path))
  91. {
  92. return false;
  93. }
  94. if (LinkedChildren.Length > 0)
  95. {
  96. return false;
  97. }
  98. return !FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, Path);
  99. }
  100. }
  101. [IgnoreDataMember]
  102. public override bool IsPreSorted
  103. {
  104. get
  105. {
  106. return true;
  107. }
  108. }
  109. public override bool IsAuthorizedToDelete(User user)
  110. {
  111. return true;
  112. }
  113. public override bool IsSaveLocalMetadataEnabled()
  114. {
  115. return true;
  116. }
  117. /// <summary>
  118. /// Updates the official rating based on content and returns true or false indicating if it changed.
  119. /// </summary>
  120. /// <returns></returns>
  121. public bool UpdateRatingToContent()
  122. {
  123. var currentOfficialRating = OfficialRating;
  124. // Gather all possible ratings
  125. var ratings = GetLinkedChildren()
  126. .Select(i => i.OfficialRating)
  127. .Where(i => !string.IsNullOrEmpty(i))
  128. .Distinct(StringComparer.OrdinalIgnoreCase)
  129. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  130. .OrderBy(i => i.Item2 ?? 1000)
  131. .Select(i => i.Item1);
  132. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  133. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  134. StringComparison.OrdinalIgnoreCase);
  135. }
  136. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  137. {
  138. var children = base.GetChildren(user, includeLinkedChildren);
  139. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  140. {
  141. // Sort by name
  142. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  143. }
  144. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  145. {
  146. // Sort by release date
  147. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  148. }
  149. // Default sorting
  150. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  151. }
  152. public BoxSetInfo GetLookupInfo()
  153. {
  154. return GetItemLookupInfo<BoxSetInfo>();
  155. }
  156. public override bool IsVisible(User user)
  157. {
  158. var userId = user.Id.ToString("N");
  159. // Need to check Count > 0 for boxsets created prior to the introduction of Shares
  160. if (Shares.Count > 0 && Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)))
  161. {
  162. return true;
  163. }
  164. if (base.IsVisible(user))
  165. {
  166. return base.GetChildren(user, true).Count > 0;
  167. }
  168. return false;
  169. }
  170. public override bool IsVisibleStandalone(User user)
  171. {
  172. return IsVisible(user);
  173. }
  174. }
  175. }