/// <summary> /// Updates the model based on details page. /// </summary> /// <returns>True if this is a detail page with a content item</returns> private bool TryGetDetailsObjectByPageUrl(out OpengraphModuleConfig type) { //Required assignment type = null; //cultureName = cultureInfo.Name; var pageUrl = UrlPath.ResolveUrl(openGraphModel.pageHandler.Request.Path, true, true); //Removing host and pathnames //pageUrl = pageUrl.Replace(openGraphModel.page.GetDefaultUrl(), "");// pageUrl = pageUrl.Replace(openGraphModel.pageHandler.Request.Url.Host, ""); pageUrl = pageUrl.Replace(openGraphModel.pageHandler.Request.Url.Scheme + "://", ""); //Get current data item bool foundDetailContentItem = false; if (pageUrl != String.Empty) { using (var fluent = App.WorkWith()) { //Get the page controls var controls = openGraphModel.currentPageData.Controls; if (controls != null || controls.Count > 0) { //Loop the page controls to find out which type it is. foreach (PageControl pageControl in controls) { IEnumerable <ControlProperty> props = pageControl.GetProperties(true); string key = pageControl.Caption.ToLower(); //List found controls. openGraphModel.pageControlTypes.Add(key); type = globalConfig.Modules.Opengraph.Modules[key]; if (type != null) { foundDetailContentItem = GetDynamicItemFromPageUrl(pageUrl, type); if (foundDetailContentItem) { break; } } } } } } return(foundDetailContentItem); }
/// <summary> /// Gets an dynamic item from page url. This method automatically updates the opengraph model. /// </summary> /// <param name="pageUrl">The page url (Without host and single segment</param> /// <param name="finalCurrentModuleType">The used type</param> /// <returns></returns> private bool GetDynamicItemFromPageUrl(string pageUrl, OpengraphModuleConfig ogConfigElement) { Type itemsType = null; //Try catched this to avoid errors for build in modules try { bool returnStatement = false; //Remove white spaces ogConfigElement.ModuleType = ogConfigElement.ModuleType.Replace(" ", ""); //Check for multiple types if (ogConfigElement.ModuleType.Contains(seperator)) { //Loop all given types foreach (string stringType in ogConfigElement.ModuleType.Split(seperator)) { itemsType = TypeResolutionService.ResolveType(stringType); returnStatement = GetDataItemByType(itemsType, ogConfigElement, pageUrl); if (returnStatement) { return(returnStatement); } } } else { //Only one type, get it. itemsType = TypeResolutionService.ResolveType(ogConfigElement.ModuleType); returnStatement = GetDataItemByType(itemsType, ogConfigElement, pageUrl); if (returnStatement) { return(returnStatement); } } return(returnStatement); } catch (Exception e) { _logiszLogger.LogException(ModuleName, e); return(false); } }
public void Index() { CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; cultureName = cultureInfo.Name; //Defaults companyName = globalConfig.CompanyName; openGraphDefaultTitle = globalConfig.Modules.Opengraph.DefaultOGTitle; openGraphDefaultDescription = globalConfig.Modules.Opengraph.DefaultOGDescription; openGraphDefaultImage = String.Empty; OpengraphModuleConfig type = null; bool foundDetailContentItem = TryGetDetailsObjectByPageUrl(out type); //Check foundDetailContentItem to avoid list pages to take over another type if (type != null && foundDetailContentItem)// { openGraphModel.ogType = type.OgType; } try { openGraphModel.ogDescription = removeHtml(openGraphModel.ogDescription); openGraphModel.ogTitle = removeHtml(openGraphModel.ogTitle); } catch (Exception e) { _logiszLogger.LogException(ModuleName, e); } //Update page BuildPageHandler(); //Enable debug Debug(openGraphModel); }
/// <summary> /// Updates the OG item model. /// </summary> /// <param name="itemsType">Type of the item</param> /// <param name="ogModel">The open graph model</param> /// <param name="pageUrl">The url</param> /// <returns> true or false</returns> private bool GetDataItemByType(Type itemsType, OpengraphModuleConfig ogModel, string pageUrl) { switch (itemsType.FullName) { //Default type case "Telerik.Sitefinity.News.Model.NewsItem": NewsManager newsManager = NewsManager.GetManager(); List <Telerik.Sitefinity.News.Model.NewsItem> items = newsManager.GetNewsItems().Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true).ToList(); Telerik.Sitefinity.News.Model.NewsItem item = items.FirstOrDefault(newsItem => newsItem.ItemDefaultUrl == pageUrl); if (item == null) { return(false); } //Default openGraphModel.ogTitle = item.Title.ToString(); //Given property if (item.DoesFieldExist(ogModel.TitlePropertyName)) { if (!String.IsNullOrEmpty(item.GetValue <Lstring>(ogModel.TitlePropertyName).ToString())) { openGraphModel.ogTitle = item.GetValue <Lstring>(ogModel.TitlePropertyName).ToString(); } } //Default OG prop if (item.DoesFieldExist("OpenGraphTitle")) { if (!String.IsNullOrEmpty(item.GetValue <Lstring>("OpenGraphTitle").ToString())) { openGraphModel.ogTitle = item.GetValue <Lstring>("OpenGraphTitle").ToString(); } } //OG prop if (item.DoesFieldExist("LgszOpenGraphTitle")) { if (!String.IsNullOrEmpty(item.GetValue <Lstring>("LgszOpenGraphTitle").ToString())) { openGraphModel.ogTitle = item.GetValue <Lstring>("LgszOpenGraphTitle").ToString(); } } //Default openGraphModel.ogDescription = item.Summary != null?item.Summary.ToString() : openGraphDefaultDescription; //Given property if (item.DoesFieldExist(ogModel.DescriptionPropertyName)) { if (!String.IsNullOrEmpty(item.GetValue <Lstring>(ogModel.DescriptionPropertyName).ToString())) { openGraphModel.ogDescription = item.GetValue <Lstring>(ogModel.DescriptionPropertyName).ToString(); } } //Default OG prop if (item.DoesFieldExist("OpenGraphDescription")) { if (!String.IsNullOrEmpty(item.GetValue <Lstring>("OpenGraphDescription").ToString())) { openGraphModel.ogDescription = item.GetValue <Lstring>("OpenGraphDescription").ToString(); } } //OG prop if (item.DoesFieldExist("LgszOpenGraphDescription")) { if (!String.IsNullOrEmpty(item.GetValue <Lstring>("LgszOpenGraphDescription").ToString())) { openGraphModel.ogDescription = item.GetValue <Lstring>("LgszOpenGraphDescription").ToString(); } } Telerik.Sitefinity.Libraries.Model.Image image = null; IDataItem newsImage = null; openGraphModel.ogImage = openGraphDefaultImage; if (item.DoesFieldExist(ogModel.ImagePropertyName)) { if (item.GetRelatedItems(ogModel.ImagePropertyName).FirstOrDefault() != null) { newsImage = item.GetRelatedItems(ogModel.ImagePropertyName).FirstOrDefault(); } } if (item.DoesFieldExist("OpenGraphImage")) { if (item.GetRelatedItems("OpenGraphImage").FirstOrDefault() != null) { newsImage = item.GetRelatedItems("OpenGraphImage").FirstOrDefault(); } } if (item.DoesFieldExist("LgszOpenGraphImage")) { if (item.GetRelatedItems("LgszOpenGraphImage").FirstOrDefault() != null) { newsImage = item.GetRelatedItems("LgszOpenGraphImage").FirstOrDefault(); } } if (newsImage != null) { image = _liberariesManager.GetImages().FirstOrDefault(ogImg => ogImg.Id == newsImage.Id); } if (image != null) { openGraphModel.ogImage = image.Url; } return(true); default: DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(); try { string redirectUrl; DynamicContent currentDynamicItem = dynamicModuleManager.Provider.GetItemFromUrl(itemsType, pageUrl, true, out redirectUrl) as DynamicContent; if (currentDynamicItem == null) { return(false); } //Given property openGraphModel.ogTitle = openGraphDefaultTitle; if (!String.IsNullOrEmpty(ogModel.TitlePropertyName)) { openGraphModel.ogTitle = ParseFieldTags(ogModel.TitlePropertyName, currentDynamicItem); } //Default OG prop if (currentDynamicItem.DoesFieldExist("OpenGraphTitle")) { if (!String.IsNullOrEmpty(currentDynamicItem.GetValue <Lstring>("OpenGraphTitle").ToString())) { openGraphModel.ogTitle = currentDynamicItem.GetValue <Lstring>("OpenGraphTitle").ToString(); } } //OG prop if (currentDynamicItem.DoesFieldExist("LgszOpenGraphTitle")) { if (!String.IsNullOrEmpty(currentDynamicItem.GetValue <Lstring>("LgszOpenGraphTitle").ToString())) { openGraphModel.ogTitle = currentDynamicItem.GetValue <Lstring>("LgszOpenGraphTitle").ToString(); } } //Default openGraphModel.ogDescription = openGraphDefaultDescription; if (!String.IsNullOrEmpty(ogModel.DescriptionPropertyName)) { openGraphModel.ogDescription = ParseFieldTags(ogModel.DescriptionPropertyName, currentDynamicItem); } //Default OG prop if (currentDynamicItem.DoesFieldExist("OpenGraphDescription")) { if (!String.IsNullOrEmpty(currentDynamicItem.GetValue <Lstring>("OpenGraphDescription").ToString())) { openGraphModel.ogDescription = currentDynamicItem.GetValue <Lstring>("OpenGraphDescription").ToString(); } } //OG prop if (currentDynamicItem.DoesFieldExist("LgszOpenGraphDescription")) { if (!String.IsNullOrEmpty(currentDynamicItem.GetValue <Lstring>("LgszOpenGraphDescription").ToString())) { openGraphModel.ogDescription = currentDynamicItem.GetValue <Lstring>("LgszOpenGraphDescription").ToString(); } } Telerik.Sitefinity.Libraries.Model.Image img = null; IDataItem dynamicContentImage = null; openGraphModel.ogImage = openGraphDefaultImage; if (currentDynamicItem.DoesFieldExist(ogModel.ImagePropertyName)) { if (currentDynamicItem.GetRelatedItems(ogModel.ImagePropertyName).FirstOrDefault() != null) { dynamicContentImage = currentDynamicItem.GetRelatedItems(ogModel.ImagePropertyName).FirstOrDefault(); } } if (currentDynamicItem.DoesFieldExist("OpenGraphImage")) { if (currentDynamicItem.GetRelatedItems("OpenGraphImage").FirstOrDefault() != null) { dynamicContentImage = currentDynamicItem.GetRelatedItems("OpenGraphImage").FirstOrDefault(); } } if (currentDynamicItem.DoesFieldExist("LgszOpenGraphImage")) { if (currentDynamicItem.GetRelatedItems("LgszOpenGraphImage").FirstOrDefault() != null) { dynamicContentImage = currentDynamicItem.GetRelatedItems("LgszOpenGraphImage").FirstOrDefault(); } } if (dynamicContentImage != null) { img = _liberariesManager.GetImages().FirstOrDefault(ogImg => ogImg.Id == dynamicContentImage.Id); } if (img != null) { openGraphModel.ogImage = img.Url; } return(true); } catch (Exception) { return(false); } } }