Exemplo n.º 1
0
        /// <summary>
        /// Get a single image from a content.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns>
        /// Telerik.Sitefinity.Libraries.Model.Image object.
        /// </returns>
        public static DocumentModel GetDocument(this DynamicContent item, string fieldName)
        {
            if (!item.DoesFieldExist(fieldName))
            {
                return(null);
            }

            var sfContent = item.GetOriginal().GetRelatedItems <Document>(fieldName).FirstOrDefault();

            return(sfContent != null ? new DocumentModel(sfContent) : null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get a video from the given item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns>
        /// The video.
        /// </returns>
        public static VideoModel GetVideo(this DynamicContent item, string fieldName)
        {
            if (item == null || !item.DoesFieldExist(fieldName))
            {
                return(null);
            }

            Video sfContent = item.GetOriginal().GetRelatedItems <Video>(fieldName).FirstOrDefault();

            return(sfContent != null ? new VideoModel(sfContent) : null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get a single image from a content.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns>
        /// The image.
        /// </returns>
        public static ImageModel GetImage(this DynamicContent item, string fieldName)
        {
            //VALIDATE INPUT
            if (!item.DoesFieldExist(fieldName))
            {
                return(null);
            }

            var sfContent = item.GetOriginal().GetRelatedItems <Image>(fieldName).FirstOrDefault();

            return(sfContent != null ? new ImageModel(sfContent) : null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Parses string to field tags
        /// </summary>
        /// <param name="field">The field</param>
        /// <param name="currentDynamicItem">DC item</param>
        /// <returns>string parsed string</returns>
        private string ParseFieldTags(string field, DynamicContent currentDynamicItem)
        {
            var tokens = new List <string>();

            //Get fieldnames
            Regex regex = new Regex(@"\{\{(.+?)\}\}");

            foreach (Match match in regex.Matches(field))
            {
                tokens.Add(match.Groups[0].Value);
            }

            //Loop given fields
            string result = field;

            foreach (string key in tokens)
            {
                string fieldName = key;
                fieldName = fieldName.Replace("{{", string.Empty);
                fieldName = fieldName.Replace("}}", string.Empty);

                //Skip if it is a unknown field
                if (!currentDynamicItem.DoesFieldExist(fieldName))
                {
                    continue;
                }

                //get value of key
                string fieldValue = currentDynamicItem.GetValue <Lstring>(fieldName).ToString();
                //Append
                if (!String.IsNullOrEmpty(fieldValue))
                {
                    result = result.Replace(key, fieldValue);
                }
            }

            return(removeHtml(result));
        }
Exemplo n.º 5
0
        /// <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);
                }
            }
        }
Exemplo n.º 6
0
 public T GetFieldValue <T>(DynamicContent contentItem, string fieldName)
 {
     return(contentItem != null && contentItem.DoesFieldExist(fieldName) ? contentItem.GetValue <T>(fieldName) : default);
Exemplo n.º 7
0
        /// <summary>
        /// Adds the media.
        /// </summary>
        /// <param name="dataItem">The data item.</param>
        /// <param name="field">The field.</param>
        /// <param name="media">The media.</param>
        public static void SetRelation(this DynamicContent dataItem, string field, MediaModel media)
        {
            //HANDLE FIELD ONLY IF APPLICABLE
            if (!dataItem.DoesFieldExist(field))
            {
                return;
            }
            var manager = LibrariesManager.GetManager();

            if (media is ImageModel)
            {
                Image image;

                //GET AVAILABLE IMAGE
                if (media.Id != Guid.Empty)
                {
                    image = manager.GetImage(media.Id);
                }
                else
                {
                    image = manager.GetImages().LiveAndVisible().FirstOrDefault(i => i.UrlName == media.Slug);
                }

                //ADD IMAGE IF AVAILABLE
                if (image != null)
                {
                    dataItem.CreateRelation(image, field);
                }
            }
            else if (media is VideoModel)
            {
                Video video;

                //GET AVAILABLE VIDEO
                if (media.Id != Guid.Empty)
                {
                    video = manager.GetVideo(media.Id);
                }
                else
                {
                    video = manager.GetVideos().LiveAndVisible().FirstOrDefault(v => v.UrlName == media.Slug);
                }

                //ADD VIDEO IF AVAILABLE
                if (video != null)
                {
                    dataItem.CreateRelation(video, field);
                }
            }
            else if (media is DocumentModel)
            {
                Document document;

                //GET AVAILABLE DOCUMENT
                if (media.Id != Guid.Empty)
                {
                    document = manager.GetDocument(media.Id);
                }
                else
                {
                    document = manager.GetDocuments().LiveAndVisible().FirstOrDefault(d => d.UrlName == media.Slug);
                }

                //ADD DOCUMENT IF AVAILABLE
                if (document != null)
                {
                    dataItem.CreateRelation(document, field);
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Gets the images collection from content.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="fieldName">Name of the field.</param>
 /// <returns>
 /// IQueryable Telerik.Sitefinity.Libraries.Model.Image.
 /// </returns>
 public static List <DocumentModel> GetDocuments(this DynamicContent item, string fieldName)
 {
     return(item.DoesFieldExist(fieldName)
         ? item.GetOriginal().GetRelatedItems <Document>(fieldName).Select(x => new DocumentModel(x)).ToList()
         : Enumerable.Empty <DocumentModel>().ToList());
 }
Exemplo n.º 9
0
        private void AddMetaDataTagsFromArticle(DynamicContent dynamicContent)
        {
            Page.RemoveExistingPublishedTags();

            // add published date tags
            Page.AddCustomMetaTags("Published", dynamicContent.PublicationDate.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture));

            TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();

            if (dynamicContent.DoesFieldExist("Category"))
            {
                TrackedList <Guid> categoryIds = dynamicContent.GetValue("Category") as TrackedList <Guid>;
                if (categoryIds != null && categoryIds.Any())
                {
                    var categoryList = string.Join(",", categoryIds.Select(cid => taxonomyManager.GetTaxon(cid).Title));
                    Page.AddCustomMetaTags("webcategory", categoryList);
                }
            }

            if (dynamicContent.DoesFieldExist("resourcetypes"))
            {
                TrackedList <Guid> resourcetypesIds = dynamicContent.GetValue("resourcetypes") as TrackedList <Guid>;
                if (resourcetypesIds != null && resourcetypesIds.Any())
                {
                    var resourcetypesList = string.Join(",", resourcetypesIds.Select(cid => taxonomyManager.GetTaxon(cid).Title));
                    Page.AddCustomMetaTags("resourcetypes", resourcetypesList);
                }
            }

            Page.RemoveExistingModuleTags();
            string modulePage = dynamicContent.GetType().Name;

            try
            {
                if (!AppSettingsUtility.GetValue <string>("MetaDataTags.Article.Name").IsNullOrWhitespace())
                {
                    modulePage = AppSettingsUtility.GetValue <string>("MetaDataTags.Article.Name");
                }
            }
            catch (Exception)
            {
            }

            Page.AddCustomMetaTags("Module", modulePage);


            Type type;

            if (dynamicContent.SystemParentItem != null)
            {
                type = dynamicContent.SystemParentItem.GetType();
            }
            else
            {
                type = dynamicContent.GetType();
            }

            string articleType = type.FullName.Split('.')[4];

            //Could change this to work with On-Scene Articles...
            if (articleType.ToLower().Contains("healthprogress"))
            {
                Page.AddCustomMetaTags("Publication", "Health Progress");
                //Health Progress
            }

            if (dynamicContent.DoesFieldExist("author"))
            {
                var authorname = dynamicContent.Author;
                if (authorname != null)
                {
                    Page.AddCustomMetaTags("ContentAuthor", authorname);
                }
            }
            var authorsFieldName = "organizationalauthors";

            if (dynamicContent.DoesFieldExist(authorsFieldName))
            {
                TrackedList <Guid> authorIds = dynamicContent.GetValue(authorsFieldName) as TrackedList <Guid>;
                if (authorIds != null)
                {
                    var authorList = string.Join(",", authorIds.Select(cid => taxonomyManager.GetTaxon(cid).Title));
                    Page.AddCustomMetaTags("contentauthor", authorList);
                }
            }
        }