Esempio n. 1
0
        protected override ViewModel EnrichModel(ViewModel sourceModel)
        {
            StaticWidget staticWidget = base.EnrichModel(sourceModel) as StaticWidget;

            if (staticWidget?.Keywords != null)
            {
                PublicContentApiClient pcaClient = new PublicContentApiClient();

                staticWidget.Topics = pcaClient.GetTridionDocsTopicsByKeywords(staticWidget.Keywords, staticWidget.MaxItems);
            }

            DynamicWidget dynamicWidget = base.EnrichModel(sourceModel) as DynamicWidget;

            if (dynamicWidget?.Keywords != null)
            {
                PublicContentApiClient pcaClient = new PublicContentApiClient();

                // There are multiple regions in a page.
                // Each region contains entities and every entity has a view.
                // We are looking for a product entity by its view name which is specified in the dynamicWidget.ProductViewModel .

                foreach (RegionModel regionModel in WebRequestContext.PageModel.Regions)
                {
                    Product product = regionModel.Entities?.FirstOrDefault(e => e.MvcData.ViewName == dynamicWidget.ProductViewModel) as Product;

                    if (product?.Keywords == null)
                    {
                        continue;
                    }
                    // When the product entity is found, we get its keywords.
                    // But we only collect those keywords specified in the dynamicWidget.Keywords .
                    // Then we are ready to get TridionDocs topics by the keywords values .

                    var keywords = new Dictionary <string, KeywordModel>();

                    foreach (var keywordName in dynamicWidget.Keywords)
                    {
                        KeyValuePair <string, KeywordModel> keyword = product.Keywords.FirstOrDefault(k => k.Key.Contains("." + keywordName + "."));

                        if (keyword.Value != null && !keywords.ContainsKey(keyword.Key))
                        {
                            keywords.Add(keyword.Key, keyword.Value);
                        }
                    }

                    if (keywords.Any())
                    {
                        dynamicWidget.Topics = pcaClient.GetTridionDocsTopicsByKeywords(keywords, dynamicWidget.MaxItems);
                    }

                    break;
                }
            }

            return(sourceModel);
        }
 public override void LoadOptions(Widget widget)
 {
     activeWidget = widget as StaticWidget;
     tOBackground.Initialize(activeWidget.Background, "Background Texture:");
 }