/// <summary> /// Find the <paramref name="mediaObject" /> in <see cref="Entity.GalleryData.Album.MediaItems" />, /// configure it, and return it as an instance of <see cref="MediaItem" />. The metadata are /// assigned to the <see cref="Entity.MediaItem.MetaItems" /> property. /// </summary> /// <param name="data">The gallery data. It is expected, though not necessary, for the /// <paramref name="mediaObject" /> to be represented as one of the items in /// <see cref="GalleryData.Album.MediaItems" />.</param> /// <param name="mediaObject">The media object.</param> /// <returns>Returns an instance of <see cref="MediaItem" />.</returns> private static MediaItem GetCurrentMediaItem(GalleryData data, IGalleryObject mediaObject) { MediaItem mediaItem = null; if (data.Album.MediaItems != null) mediaItem = data.Album.MediaItems.FirstOrDefault(mo => mo.Id == mediaObject.Id); if (mediaItem == null) mediaItem = GalleryObjectController.ToMediaItem(mediaObject, 0, MediaObjectHtmlBuilder.GetMediaObjectHtmlBuilderOptions(mediaObject)); mediaItem.MetaItems = GalleryObjectController.ToMetaItems(mediaObject.MetadataItems.GetVisibleItems(), mediaObject); return mediaItem; }
/// <summary> /// Gets the gallery data for the specified <paramref name="mediaObject" />. /// <see cref="GalleryData.Settings" /> is set to null because those values /// are calculated from control-specific properties that are not known at this time (it is /// expected that that property is assigned by subsequent code - including javascript - /// when that data is able to be calculated). Guaranteed to not return null. /// </summary> /// <param name="mediaObject">The media object.</param> /// <param name="mediaObjectContainer">The media object container.</param> /// <param name="options">Specifies options for configuring the return data. To use default /// settings, specify an empty instance with properties left at default values.</param> /// <returns>Returns an instance of <see cref="GalleryData" />.</returns> /// <exception cref="GallerySecurityException">Thrown when the current user does not have /// permission to access the <paramref name="mediaObject" />.</exception> public static GalleryData GetGalleryDataForMediaObject(IGalleryObject mediaObject, IAlbum mediaObjectContainer, GalleryDataLoadOptions options) { SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.ViewAlbumOrMediaObject, RoleController.GetGalleryServerRolesForUser(), mediaObject.Parent.Id, mediaObject.GalleryId, Utils.IsAuthenticated, mediaObject.Parent.IsPrivate, ((IAlbum)mediaObject.Parent).IsVirtualAlbum); var data = new GalleryData { App = GetAppEntity(), Settings = null, Album = AlbumController.ToAlbumEntity(mediaObjectContainer, options), ActiveMetaItems = null, // Assigned on client ActiveGalleryItems = null, // Assigned on client Resource = ResourceController.GetResourceEntity() }; data.MediaItem = GetCurrentMediaItem(data, mediaObject); // Assign user, but only grab the required fields. We do this to prevent unnecessary user data from traveling the wire. var user = UserController.GetUserEntity(Utils.UserName, mediaObject.GalleryId); data.User = new User() { UserName = user.UserName, IsAuthenticated = user.IsAuthenticated, CanAddAlbumToAtLeastOneAlbum = user.CanAddAlbumToAtLeastOneAlbum, CanAddMediaToAtLeastOneAlbum = user.CanAddMediaToAtLeastOneAlbum, UserAlbumId = user.UserAlbumId }; return data; }