/// <summary> /// Initializes a new instance of the <see cref="UmbracoBannerSettingsService" /> class. /// </summary> /// <param name="content">An Umbraco page using the <see cref="BannersDocumentType"/> document type.</param> /// <param name="targetUrlReader">The URL list reader.</param> /// <exception cref="System.ArgumentNullException"> /// content /// or /// targetUrlReader /// </exception> public UmbracoBannerSettingsService(IPublishedContent content, IUrlListReader targetUrlReader) { if (content == null) throw new ArgumentNullException("content"); if (targetUrlReader == null) throw new ArgumentNullException("targetUrlReader"); _content = content; _targetUrlReader = targetUrlReader; }
/// <summary> /// Initializes a new instance of the <see cref="UmbracoBannerSettingsService" /> class. /// </summary> /// <param name="content">An Umbraco page using the 'Banners' document type.</param> /// <param name="targetUrlReader">The URL list reader.</param> /// <exception cref="System.ArgumentNullException"> /// targetUrlReader /// </exception> public UmbracoBannerSettingsService(IPublishedContent content, IUrlListReader targetUrlReader) { if (targetUrlReader == null) { throw new ArgumentNullException("targetUrlReader"); } _content = content; _targetUrlReader = targetUrlReader; }
/// <summary> /// Get banner settings from an Umbraco page using the <see cref="BannerDocumentType"/> document type /// </summary> /// <param name="bannerPage">The bannerPage.</param> /// <param name="urlListReader">The URL list reader.</param> private static Banner ReadBannerFromUmbraco(IPublishedContent bannerPage, IUrlListReader urlListReader) { var model = new Banner() { Inherit = bannerPage.GetPropertyValue <bool>("inherit_Content"), Cascade = bannerPage.GetPropertyValue <bool>("cascade_Content") }; // Minimum requirement is just somewhere to display it. Return null if missing. // An image is not required because it's useful to set up a banner with no image as a way of stopping other banners. var imageData = bannerPage.GetPropertyValue <IPublishedContent>("bannerImage_Content"); if (imageData != null) { model.BannerImage = new Image() { AlternativeText = imageData.Name, ImageUrl = new Uri(imageData.Url, UriKind.Relative), Width = imageData.GetPropertyValue <int>("umbracoWidth"), Height = imageData.GetPropertyValue <int>("umbracoHeight") }; } ((List <Uri>)model.TargetUrls).AddRange(urlListReader.ReadUrls(bannerPage, "whereToDisplayIt_Content", "whereElseToDisplayIt_Content")); if (model.TargetUrls.Count == 0) { return(null); } // URL to link to is optional, and can come from two possible fields var targetPage = bannerPage.GetPropertyValue <IPublishedContent>("targetPage_Content"); if (targetPage != null) { model.BannerLink = new Uri(targetPage.UrlWithDomain()); } else { try { var urlString = bannerPage.GetPropertyValue <string>("targetUrl_Content"); if (!String.IsNullOrEmpty(urlString)) { model.BannerLink = new Uri(urlString, UriKind.RelativeOrAbsolute); } } catch (UriFormatException) { // ignore invalid URLs } } return(model); }
/// <summary> /// Get banner settings from an Umbraco page using the <see cref="BannerDocumentType"/> document type /// </summary> /// <param name="bannerPage">The bannerPage.</param> /// <param name="urlListReader">The URL list reader.</param> private static Banner ReadBannerFromUmbraco(IPublishedContent bannerPage, IUrlListReader urlListReader) { var model = new Banner() { Inherit = bannerPage.GetPropertyValue<bool>("inherit_Content"), Cascade = bannerPage.GetPropertyValue<bool>("cascade_Content") }; // Minimum requirements are an image and somewhere to display it. Return null if either of those missing. var imageData = bannerPage.GetPropertyValue<IPublishedContent>("bannerImage_Content"); if (imageData == null) return null; model.BannerImage = new Image() { AlternativeText = imageData.Name, ImageUrl = new Uri(imageData.Url, UriKind.Relative), Width = imageData.GetPropertyValue<int>("umbracoWidth"), Height = imageData.GetPropertyValue<int>("umbracoHeight") }; ((List<Uri>)model.TargetUrls).AddRange(urlListReader.ReadUrls(bannerPage, "whereToDisplayIt_Content", "whereElseToDisplayIt_Content")); if (model.TargetUrls.Count == 0) return null; // URL to link to is optional, and can come from two possible fields var targetPage = bannerPage.GetPropertyValue<IPublishedContent>("targetPage_Content"); if (targetPage != null) { model.BannerLink = new Uri(targetPage.UrlWithDomain()); } else { try { var urlString = bannerPage.GetPropertyValue<string>("targetUrl_Content"); if (!String.IsNullOrEmpty(urlString)) { model.BannerLink = new Uri(urlString, UriKind.RelativeOrAbsolute); } } catch (UriFormatException) { // ignore invalid URLs } } return model; }
/// <summary> /// Get alerts from Umbraco pages which are children of this one, which should all be using the Alert document type /// </summary> /// <param name="model">The model.</param> /// <param name="alerts">The alerts.</param> /// <param name="urlListReader">The URL list reader.</param> private static void AddAlertsFromUmbraco(RenderModel model, List <AlertViewModel> alerts, IUrlListReader urlListReader) { foreach (var alertPage in model.Content.Children) { var alertModel = new AlertViewModel() { Alert = new HtmlString(alertPage.GetPropertyValue <string>("alert_Content")), Append = alertPage.GetPropertyValue <bool>("append_Content"), Cascade = alertPage.GetPropertyValue <bool>("cascade_Content") }; ((List <Uri>)alertModel.TargetUrls).AddRange(urlListReader.ReadUrls(alertPage, "whereToDisplayIt_Content", "whereElseToDisplayIt_Content")); if (alertModel.TargetUrls.Count > 0) { alerts.Add(alertModel); } } }