private void RecursivelyBuildUrls(JObject taxonomy)
        {
            JArray?terms = _taxonomyHelper.GetTerms(taxonomy);

            if (terms != null)
            {
                foreach (JObject term in terms)
                {
                    PageLocations.Add(_taxonomyHelper.BuildTermUrl(term, taxonomy));
                    RecursivelyBuildUrls(term);
                }
            }
        }
Esempio n. 2
0
        private void RecursivelyBuildUrls(JObject taxonomy)
        {
            JArray?terms = _taxonomyHelper.GetTerms(taxonomy);

            if (terms == null)
            {
                return;
            }

            //todo: look at this
#pragma warning disable S3217
            foreach (JObject term in terms)
#pragma warning restore S3217
            {
                _pageLocations.Add(_taxonomyHelper.BuildTermUrl(term, taxonomy));
                RecursivelyBuildUrls(term);
            }
        }
        public async Task <(bool, string)> ValidateCreate(JObject term, JObject taxonomy)
        {
            if (!term.ContainsKey("PageLocation"))
            {
                return(true, string.Empty);
            }

            string url = _taxonomyHelper.BuildTermUrl(term, taxonomy);
            //TODO: check whether or not we only care about published pages, but I think we care about both
            IEnumerable <ContentItem> contentItems = await _session.Query <ContentItem, PageLocationPartIndex>().ListAsync();

            foreach (var contentItem in contentItems)
            {
                ContentItem?draftContentItem = await _contentManager.GetAsync(contentItem.ContentItemId, VersionOptions.Draft);

                ContentItem?publishedContentItem = await _contentManager.GetAsync(contentItem.ContentItemId, VersionOptions.Published);

                //TODO: use nameof, but doing so would introduce a circular dependency between the page location and taxonomies projects
                string?draftUrl = ((string?)draftContentItem?.Content.PageLocationPart.FullUrl)?.Trim('/') ?? null;
                string?pubUrl   = ((string?)publishedContentItem?.Content.PageLocationPart.FullUrl)?.Trim('/') ?? null;

                string[]? draftRedirectLocations     = draftContentItem?.Content.PageLocationPart.RedirectLocations?.ToObject <string?>()?.Split("\r\n");
                string[]? publishedRedirectLocations = publishedContentItem?.Content.PageLocationPart.RedirectLocations?.ToObject <string?>()?.Split("\r\n");

                if ((draftUrl?.Equals(url, StringComparison.OrdinalIgnoreCase) ?? false) || (pubUrl?.Equals(url, StringComparison.OrdinalIgnoreCase) ?? false))
                {
                    return(false, $"The generated URL for '{term["DisplayText"]}' has already been used as a {draftContentItem?.ContentType.CamelFriendly() ?? publishedContentItem!.ContentType.CamelFriendly()} URL.");
                }

                if ((draftRedirectLocations?.Any(x => x.Trim('/').Equals(url, StringComparison.OrdinalIgnoreCase)) ?? false) || (publishedRedirectLocations?.Any(x => x.Trim('/').Equals(url, StringComparison.OrdinalIgnoreCase)) ?? false))
                {
                    return(false, $"The generated URL for '{term["DisplayText"]}' has already been used as a {draftContentItem?.ContentType.CamelFriendly() ?? publishedContentItem!.ContentType.CamelFriendly()} Redirect Location");
                }
            }

            return(true, string.Empty);
        }
Esempio n. 4
0
        public async Task <bool> UpdatedAsync(ContentItem term, ContentItem taxonomy)
        {
            if (term.ContentType != ContentTypes.PageLocation)
            {
                return(true);
            }

            List <ContentItem> allPages = await _contentItemsService.GetActive(ContentTypes.Page);

            List <ContentItem> associatedPages = allPages.Where(x => x.Content.Page.PageLocations.TermContentItemIds[0] == term.ContentItemId).ToList();

            var groups = associatedPages.GroupBy(x => x.ContentItemId);

            foreach (var group in groups)
            {
                var pages = group.ToList();

                foreach (var page in pages)
                {
                    //rebuild the Full URL to ensure it matches the current state of the term
                    var pageUrlName = page.As <PageLocationPart>().UrlName;
                    var termUrl     = _taxonomyHelper.BuildTermUrl(JObject.FromObject(term), JObject.FromObject(taxonomy));

                    var fullUrl = string.IsNullOrWhiteSpace(termUrl)
                        ? $"/{pageUrlName}"
                        : $"/{termUrl}/{pageUrlName}";

                    page.Alter <PageLocationPart>(part => part.FullUrl = fullUrl);

                    _session.Save(page);
                }

                try
                {
                    if (pages.Count > 1)
                    {
                        var publishedPage = pages.Single(x => x.Published);
                        var draftPage     = pages.Single(x => x.Latest);

                        if (!await _syncOrchestrator.Update(publishedPage, draftPage))
                        {
                            _session.Cancel();
                        }
                    }
                    else
                    {
                        var page = pages.Single();

                        if (page.Published && !await _syncOrchestrator.Publish(page))
                        {
                            _session.Cancel();
                        }
                        else if (!page.Published && !await _syncOrchestrator.SaveDraft(page))
                        {
                            _session.Cancel();
                        }
                    }
                }
                catch (Exception)
                {
                    _session.Cancel();
                    return(false);
                }
            }

            return(true);
        }