public EnterspeedSitecoreIdentity Parse(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }

            Guid     itemId;
            Language language = _languageManager.GetDefaultLanguage();

            if (Guid.TryParse(id, out itemId))
            {
                return(new EnterspeedSitecoreIdentity
                {
                    ID = new ID(itemId),
                    Language = language
                });
            }

            if (id.Contains('-'))
            {
                string[] idAndCulture = id.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                if (idAndCulture.Length == 0)
                {
                    return(null);
                }

                if (!Guid.TryParse(idAndCulture[0], out itemId))
                {
                    return(null);
                }

                if (idAndCulture.Length == 2)
                {
                    itemId   = Guid.Parse(idAndCulture[0]);
                    language = _languageManager.GetLanguage(idAndCulture[1]);
                }

                return(new EnterspeedSitecoreIdentity
                {
                    ID = new ID(itemId),
                    Language = language
                });
            }

            if (!Guid.TryParse(id, out itemId))
            {
                return(null);
            }

            return(new EnterspeedSitecoreIdentity
            {
                ID = new ID(itemId),
                Language = language
            });
        }
Esempio 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);
        }