private string CreateFullPath(string path1, string path2, ActiveLocale locale)
        {
            string fullPath = null;

            if (string.IsNullOrWhiteSpace(path2))
            {
                fullPath = path1;
            }
            else if (path1.EndsWith("/"))
            {
                fullPath = path1 + path2;
            }
            else
            {
                fullPath = path1 + "/" + path2;
            }

            if (locale == null)
            {
                return(fullPath);
            }

            var localePath = "/" + locale.IETFLanguageTag.ToLowerInvariant();

            if (fullPath == "/")
            {
                return(localePath);
            }

            fullPath = localePath + fullPath;

            return(fullPath);
        }
예제 #2
0
        /// <summary>
        /// Parses a locale string from the site path and checks if
        /// it is an active locale, returning the ActiveLocale object if
        /// found.
        /// </summary>
        public async Task <ActiveLocale> ParseLocaleAsync(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            ActiveLocale locale = null;
            string       localeStr;

            if (path.Contains("/"))
            {
                localeStr = path.Split('/').First();
            }
            else
            {
                localeStr = path;
            }

            // Check the first part of the string matches the format for a locale
            if (Regex.Match(localeStr, @"^[a-zA-Z]{2}(-[a-zA-Z]{2})?$", RegexOptions.IgnoreCase).Success)
            {
                var query = new GetActiveLocaleByIETFLanguageTagQuery(localeStr);
                locale = await _queryExecutor.ExecuteAsync(query);
            }

            return(locale);
        }
예제 #3
0
        /// <summary>
        /// Standardizes a page path ensuring it
        /// - starts with a slash
        /// - doesn't end with a trailing slash
        /// - (optionally) does not contain the locale
        /// </summary>
        /// <param name="path">Path to standardize.</param>
        /// <param name="currentLocale">Locale of the path to remove if present.</param>
        public string StandardizePathWithoutLocale(string path, ActiveLocale currentLocale)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return(PATH_DELIMITER);
            }
            if (!path.StartsWith(PATH_DELIMITER))
            {
                path = PATH_DELIMITER + path;
            }

            // Remove the current locale if it's included in the path
            if (currentLocale != null && path.StartsWith(PATH_DELIMITER + currentLocale.IETFLanguageTag, StringComparison.OrdinalIgnoreCase))
            {
                path = path.Remove(0, currentLocale.IETFLanguageTag.Length + 1);

                // If we accidently removed the starting slash in the above operation, add it again.
                // Example case: path = "en-ca"
                if (!path.StartsWith(PATH_DELIMITER))
                {
                    path = PATH_DELIMITER + path;
                }
            }

            if (path == PATH_DELIMITER)
            {
                return(path);
            }

            // Remove trailing slash
            return(path.TrimEnd(PATH_DELIMITER[0]));
        }
예제 #4
0
        public CustomEntityRoute Map(
            CustomEntity dbCustomEntity,
            ActiveLocale locale
            )
        {
            if (dbCustomEntity == null)
            {
                throw new ArgumentNullException(nameof(dbCustomEntity));
            }
            if (dbCustomEntity.CustomEntityVersions == null)
            {
                throw new ArgumentNullException(nameof(dbCustomEntity.CustomEntityVersions));
            }

            var route = new CustomEntityRoute()
            {
                CustomEntityDefinitionCode = dbCustomEntity.CustomEntityDefinitionCode,
                CustomEntityId             = dbCustomEntity.CustomEntityId,
                UrlSlug         = dbCustomEntity.UrlSlug,
                Locale          = locale,
                PublishDate     = dbCustomEntity.PublishDate,
                LastPublishDate = dbCustomEntity.LastPublishDate,
                PublishStatus   = dbCustomEntity.PublishStatusCode == PublishStatusCode.Published ? PublishStatus.Published : PublishStatus.Unpublished,
                Ordering        = dbCustomEntity.Ordering
            };

            bool hasLatestPublishVersion = false;

            route.Versions = new List <CustomEntityVersionRoute>();

            foreach (var dbVersion in dbCustomEntity
                     .CustomEntityVersions
                     .OrderByLatest())
            {
                var version = new CustomEntityVersionRoute()
                {
                    CreateDate     = dbVersion.CreateDate,
                    Title          = dbVersion.Title,
                    VersionId      = dbVersion.CustomEntityVersionId,
                    WorkFlowStatus = (WorkFlowStatus)dbVersion.WorkFlowStatusId
                };

                if (!hasLatestPublishVersion && version.WorkFlowStatus == WorkFlowStatus.Published)
                {
                    version.IsLatestPublishedVersion = true;
                    hasLatestPublishVersion          = true;
                }
                route.Versions.Add(version);
            }

            route.HasDraftVersion     = route.Versions.Any(v => v.WorkFlowStatus == WorkFlowStatus.Draft);
            route.HasPublishedVersion = route.Versions.Any(v => v.WorkFlowStatus == WorkFlowStatus.Published);

            return(route);
        }
        private CustomEntityRenderSummary MapSingle(
            CustomEntityVersion dbResult,
            ICollection <PageRoutingInfo> allRoutings,
            ActiveLocale locale
            )
        {
            var entity = MapCore(dbResult);

            entity.Locale   = locale;
            entity.PageUrls = MapPageRoutings(allRoutings, dbResult);

            return(entity);
        }
        public CustomEntityRoute MapRoute(
            CustomEntity dbCustomEntity,
            Dictionary <int, ActiveLocale> allLocales
            )
        {
            ActiveLocale locale = null;

            if (dbCustomEntity.LocaleId.HasValue)
            {
                locale = allLocales.GetOrDefault(dbCustomEntity.LocaleId.Value);
            }

            return(_customEntityRouteMapper.Map(dbCustomEntity, locale));
        }
예제 #7
0
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            // Do some pre-init before the page action executes, to find the locale
            if ((string)requestContext.RouteData.Values["action"] == "Page")
            {
                string path = (string)requestContext.RouteData.Values["path"];

                _locale = _pageLocaleParser.ParseLocale(path);
                if (_locale != null)
                {
                    _cultureContextService.SetCurrent(_locale.IETFLanguageTag);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Maps an EF Locale record from the db into an ActiveLocale
        /// object. If the db record is null then null is returned.
        /// </summary>
        /// <param name="dbLocale">Locale record from the database.</param>
        public ActiveLocale Map(Locale dbLocale)
        {
            if (dbLocale == null)
            {
                return(null);
            }

            var locale = new ActiveLocale()
            {
                IETFLanguageTag = dbLocale.IETFLanguageTag,
                LocaleId        = dbLocale.LocaleId,
                Name            = dbLocale.LocaleName
            };

            return(locale);
        }
        /// <summary>
        /// Maps an EF CustomEntityVersion record from the db into a CustomEntityRenderSummary
        /// object. If the db record is null then null is returned.
        /// </summary>
        /// <param name="dbResult">CustomEntityVersion record from the database.</param>
        /// <param name="executionContext">Context to run any sub queries under.</param>
        public async Task <CustomEntityRenderSummary> MapAsync(
            CustomEntityVersion dbResult,
            IExecutionContext executionContext
            )
        {
            if (dbResult == null)
            {
                return(null);
            }

            var routingQuery = GetPageRoutingQuery(dbResult);
            var routing      = await _queryExecutor.ExecuteAsync(routingQuery, executionContext);

            ActiveLocale locale = null;

            if (dbResult.CustomEntity.LocaleId.HasValue)
            {
                var getLocaleQuery = new GetActiveLocaleByIdQuery(dbResult.CustomEntity.LocaleId.Value);
                locale = await _queryExecutor.ExecuteAsync(getLocaleQuery, executionContext);
            }

            return(MapSingle(dbResult, routing, locale));
        }
        private bool MatchesLocale(ActiveLocale locale, int?localeId)
        {
            var localeIdToCheck = locale == null ? (int?)null : locale.LocaleId;

            return(localeId == localeIdToCheck);
        }