Exemple #1
0
        public async Task <IActionResult> Edit(EditSitemapIndexViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps))
            {
                return(Forbid());
            }

            var sitemap = await _sitemapManager.LoadSitemapAsync(model.SitemapId);

            if (sitemap == null)
            {
                return(NotFound());
            }

            var indexSource = sitemap.SitemapSources.FirstOrDefault() as SitemapIndexSource;

            model.SitemapIndexSource = indexSource;

            if (ModelState.IsValid)
            {
                await _sitemapService.ValidatePathAsync(model.Path, _updateModelAccessor.ModelUpdater, sitemap.SitemapId);
            }

            // Path validation may invalidate model state.
            if (ModelState.IsValid)
            {
                sitemap.Name    = model.Name;
                sitemap.Enabled = model.Enabled;
                sitemap.Path    = model.Path;

                indexSource.ContainedSitemapIds = model.ContainableSitemaps
                                                  .Where(m => m.IsChecked)
                                                  .Select(m => m.SitemapId)
                                                  .ToArray();

                await _sitemapManager.SaveSitemapAsync(sitemap.SitemapId, sitemap);

                // Always clear sitemap index cache when updated.
                await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path);

                _notifier.Success(H["Sitemap index updated successfully"]);

                return(View(model));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task ClearCacheAsync(SitemapCacheContext context)
        {
            var contentItem = context.CacheObject as ContentItem;

            var sitemapIndex = context.Sitemaps
                               .FirstOrDefault(s => s.GetType() == typeof(SitemapIndex));

            if (contentItem == null || sitemapIndex == null)
            {
                return;
            }

            var contentTypeName = contentItem.ContentType;

            var sitemaps = context.Sitemaps.OfType <Sitemap>();

            foreach (var sitemap in sitemaps)
            {
                foreach (var source in sitemap.SitemapSources.Select(x => x as ContentTypesSitemapSource))
                {
                    if (source == null)
                    {
                        continue;
                    }

                    if (source.IndexAll)
                    {
                        await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemapIndex.Path);

                        return;
                    }
                    else if (source.LimitItems && String.Equals(source.LimitedContentType.ContentTypeName, contentTypeName))
                    {
                        await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemapIndex.Path);

                        return;
                    }
                    else if (source.ContentTypes.Any(ct => String.Equals(ct.ContentTypeName, contentTypeName)))
                    {
                        await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemapIndex.Path);

                        return;
                    }
                }
            }
        }
        public async Task ClearCacheAsync(SitemapCacheContext context)
        {
            var contentItem = context.CacheObject as ContentItem;
            var sitemaps    = context.Sitemaps
                              .Where(s => s.GetType() == typeof(Sitemap));

            if (contentItem == null)
            {
                return;
            }

            var contentTypeName = contentItem.ContentType;

            foreach (var sitemap in sitemaps)
            {
                // Do not break out of this loop, as it must check each sitemap.
                foreach (var source in sitemap.SitemapSources
                         .Select(s => s as ContentTypesSitemapSource))
                {
                    if (source == null)
                    {
                        continue;
                    }

                    if (source.IndexAll)
                    {
                        await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path);

                        break;
                    }
                    else if (source.LimitItems && String.Equals(source.LimitedContentType.ContentTypeName, contentTypeName))
                    {
                        await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path);

                        break;
                    }
                    else if (source.ContentTypes.Any(ct => String.Equals(ct.ContentTypeName, contentTypeName)))
                    {
                        await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path);

                        break;
                    }
                }
            }
        }
        public async Task <IActionResult> Create(CreateSourceViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps))
            {
                return(Forbid());
            }

            var sitemap = await _sitemapManager.LoadSitemapAsync(model.SitemapId);

            if (sitemap == null)
            {
                return(NotFound());
            }

            var source = _factories.FirstOrDefault(x => x.Name == model.SitemapSourceType)?.Create();

            if (source == null)
            {
                return(NotFound());
            }

            dynamic editor = await _displayManager.UpdateEditorAsync(source, updater : _updateModelAccessor.ModelUpdater, isNew : true);

            editor.SitemapStep = source;

            if (ModelState.IsValid)
            {
                source.Id = model.SitemapSourceId;
                sitemap.SitemapSources.Add(source);

                // Clear sitemap cache when new source added.
                await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path);

                await _sitemapManager.SaveSitemapAsync(sitemap.SitemapId, sitemap);

                _notifier.Success(H["Sitemap source added successfully"]);
                return(RedirectToAction("Display", "Admin", new { sitemapId = model.SitemapId }));
            }

            model.Editor = editor;

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <IActionResult> Delete(string sitemapId)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps))
            {
                return(Forbid());
            }

            var sitemap = await _sitemapManager.LoadSitemapAsync(sitemapId);

            if (sitemap == null)
            {
                return(NotFound());
            }

            // Clear sitemap cache when deleted.
            await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path);

            await _sitemapManager.DeleteSitemapAsync(sitemapId);

            _notifier.Success(H["Sitemap deleted successfully"]);

            return(RedirectToAction(nameof(List)));
        }