BoxSet.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. [IgnoreDataMember]
  43. public override bool SupportsPeople
  44. {
  45. get { return true; }
  46. }
  47. public Guid[] LocalTrailerIds { get; set; }
  48. public Guid[] RemoteTrailerIds { get; set; }
  49. /// <summary>
  50. /// Gets or sets the remote trailers.
  51. /// </summary>
  52. /// <value>The remote trailers.</value>
  53. public MediaUrl[] RemoteTrailers { get; set; }
  54. /// <summary>
  55. /// Gets or sets the display order.
  56. /// </summary>
  57. /// <value>The display order.</value>
  58. public string DisplayOrder { get; set; }
  59. protected override bool GetBlockUnratedValue(UserPolicy config)
  60. {
  61. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  62. }
  63. public override double? GetDefaultPrimaryImageAspectRatio()
  64. {
  65. double value = 2;
  66. value /= 3;
  67. return value;
  68. }
  69. public override UnratedItem GetBlockUnratedType()
  70. {
  71. return UnratedItem.Movie;
  72. }
  73. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  74. {
  75. if (IsLegacyBoxSet)
  76. {
  77. return base.GetNonCachedChildren(directoryService);
  78. }
  79. return new List<BaseItem>();
  80. }
  81. protected override List<BaseItem> LoadChildren()
  82. {
  83. if (IsLegacyBoxSet)
  84. {
  85. return base.LoadChildren();
  86. }
  87. // Save a trip to the database
  88. return new List<BaseItem>();
  89. }
  90. [IgnoreDataMember]
  91. private bool IsLegacyBoxSet
  92. {
  93. get
  94. {
  95. if (string.IsNullOrWhiteSpace(Path))
  96. {
  97. return false;
  98. }
  99. if (LinkedChildren.Length > 0)
  100. {
  101. return false;
  102. }
  103. return !FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, Path);
  104. }
  105. }
  106. [IgnoreDataMember]
  107. public override bool IsPreSorted
  108. {
  109. get
  110. {
  111. return true;
  112. }
  113. }
  114. public override bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  115. {
  116. return true;
  117. }
  118. public override bool IsSaveLocalMetadataEnabled()
  119. {
  120. return true;
  121. }
  122. /// <summary>
  123. /// Updates the official rating based on content and returns true or false indicating if it changed.
  124. /// </summary>
  125. /// <returns></returns>
  126. public bool UpdateRatingToContent()
  127. {
  128. var currentOfficialRating = OfficialRating;
  129. // Gather all possible ratings
  130. var ratings = GetLinkedChildren()
  131. .Select(i => i.OfficialRating)
  132. .Where(i => !string.IsNullOrEmpty(i))
  133. .Distinct(StringComparer.OrdinalIgnoreCase)
  134. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  135. .OrderBy(i => i.Item2 ?? 1000)
  136. .Select(i => i.Item1);
  137. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  138. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  139. StringComparison.OrdinalIgnoreCase);
  140. }
  141. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  142. {
  143. var children = base.GetChildren(user, includeLinkedChildren);
  144. if (string.Equals(DisplayOrder, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))
  145. {
  146. // Sort by name
  147. return LibraryManager.Sort(children, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  148. }
  149. if (string.Equals(DisplayOrder, ItemSortBy.PremiereDate, StringComparison.OrdinalIgnoreCase))
  150. {
  151. // Sort by release date
  152. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  153. }
  154. // Default sorting
  155. return LibraryManager.Sort(children, user, new[] { ItemSortBy.ProductionYear, ItemSortBy.PremiereDate, ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  156. }
  157. public BoxSetInfo GetLookupInfo()
  158. {
  159. return GetItemLookupInfo<BoxSetInfo>();
  160. }
  161. public override bool IsVisible(User user)
  162. {
  163. var userId = user.Id.ToString("N");
  164. // Need to check Count > 0 for boxsets created prior to the introduction of Shares
  165. if (Shares.Count > 0 && Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)))
  166. {
  167. return true;
  168. }
  169. if (base.IsVisible(user))
  170. {
  171. return base.GetChildren(user, true).Count > 0;
  172. }
  173. return false;
  174. }
  175. public override bool IsVisibleStandalone(User user)
  176. {
  177. return IsVisible(user);
  178. }
  179. }
  180. }