public SiteDefinitionsProvider(BaseSiteContextFactory siteContextFactory)
 {
     this.sites = siteContextFactory.GetSites();
 }
Ejemplo n.º 2
0
        public EnterspeedSitecoreConfiguration GetConfiguration()
        {
            Item enterspeedConfigurationItem = _itemManager.GetItem(EnterspeedIDs.Items.EnterspeedConfigurationID, Language.Parse("en"), Version.Latest, _factory.GetDatabase("web"));

            if (enterspeedConfigurationItem == null || enterspeedConfigurationItem.Versions.Count == 0)
            {
                return(new EnterspeedSitecoreConfiguration());
            }

            string enabled = enterspeedConfigurationItem[EnterspeedIDs.Fields.EnterspeedEnabledFieldID] ?? string.Empty;

            if (enabled != "1")
            {
                return(new EnterspeedSitecoreConfiguration());
            }

            if (!IsConfigurationUpdated(enterspeedConfigurationItem, out Guid currentRevisionId))
            {
                return(_configuration);
            }

            var config = new EnterspeedSitecoreConfiguration
            {
                IsEnabled = true
            };

            string configApiBaseUrl = enterspeedConfigurationItem[EnterspeedIDs.Fields.EnterspeedApiBaseUrlFieldID];

            config.BaseUrl = (configApiBaseUrl ?? string.Empty).Trim();

            string configApiKey = enterspeedConfigurationItem[EnterspeedIDs.Fields.EnterspeedApiKeyFieldID];

            config.ApiKey = (configApiKey ?? string.Empty).Trim();

            config.ItemNotFoundUrl = GetItemNotFoundUrl(_settings);

            MultilistField enabledSitesField = enterspeedConfigurationItem.Fields[EnterspeedIDs.Fields.EnterspeedEnabledSitesFieldID];

            var enabledSites = enabledSitesField?.GetItems()?.ToList() ?? new List <Item>();

            if (enabledSites.Any())
            {
                List <SiteInfo> allSiteInfos = _siteContextFactory.GetSites();

                foreach (Item enabledSite in enabledSites)
                {
                    SiteInfo matchingSite = allSiteInfos.FirstOrDefault(x => x.RootPath.Equals(enabledSite.Paths.FullPath, StringComparison.OrdinalIgnoreCase));
                    if (matchingSite == null)
                    {
                        continue;
                    }

                    SiteContext siteContext = _siteContextFactory.GetSiteContext(matchingSite.Name);

                    Language siteLanguage = _languageManager.GetLanguage(siteContext.Language);

                    Item homeItem = _itemManager.GetItem(siteContext.StartPath, siteLanguage, Version.Latest, siteContext.Database);
                    if (homeItem == null || homeItem.Versions.Count == 0)
                    {
                        // TODO - KEK: throw exception here?
                        continue;
                    }

                    string name         = siteContext.SiteInfo.Name;
                    string startPathUrl = _linkManager.GetItemUrl(homeItem, new ItemUrlBuilderOptions
                    {
                        SiteResolving          = true,
                        Site                   = siteContext,
                        AlwaysIncludeServerUrl = true,
                        LowercaseUrls          = true,
                        LanguageEmbedding      = LanguageEmbedding.Never
                    });

                    var enterspeedSiteInfo = new EnterspeedSiteInfo
                    {
                        Name         = name,
                        BaseUrl      = startPathUrl,
                        HomeItemPath = siteContext.StartPath,
                        SiteItemPath = siteContext.RootPath
                    };

                    if (siteContext.Properties["scheme"] != null &&
                        siteContext.Properties["scheme"].Equals("https", StringComparison.OrdinalIgnoreCase))
                    {
                        enterspeedSiteInfo.IsHttpsEnabled = true;
                    }

                    config.SiteInfos.Add(enterspeedSiteInfo);
                }
            }

            // Settings caching values
            _configuration           = config;
            _configurationRevisionId = currentRevisionId;

            return(_configuration);
        }